LoginSignup
1
1

More than 5 years have passed since last update.

MapKIT: マップのアノテーションを移動する

Posted at
  • アノテーションを選択直後に選択解除すると再描画してくれる模様

using System;

using UIKit;
using CoreGraphics;
using CoreLocation;

using Xamarin.Forms;
using Xamarin.Forms.Maps.iOS;
using Xamarin.Forms.Platform.iOS;

[assembly:ExportRenderer( // Xamarin.Forms Dependency Injection         
  typeof(HelloXamap.HelloMap),
  typeof(HelloXamap.iOS.HelloMapRenderer))
]
public class HelloMapRenderer : MapRenderer
{
  // MapKITのマップビュー
  private MKMapView _native_map {
    get { return (this.NativeView as MapRenderer).Control as MKMapView; } }

  // アノテーション(MKAnnotationサブクラス)
  public Bike _running_bike = new Bike (
    new CLLocationCoordinate2D(35.658987, 139.702776),
    "渋谷", "City of Light");

  // とりあえずタイマーをトリガーにする
  System.Timers.Timer _timer = new System.Timers.Timer (1000.0);

  public HelloMapRenderer():base(){

    _timer.Elapsed +=  (
      object sender,
      System.Timers.ElapsedEventArgs ev
    ) =>  {
      // 位置変更(北東に斜めに移動)
      _running_bike.SetCoordinate (new CLLocationCoordinate2D (
        _running_bike.Coordinate.Latitude + 0.001,
        _running_bike.Coordinate.Longitude + 0.001));

      BeginInvokeOnMainThread(delegate {  // UIの命令を呼ぶおまじない
        // 選択 & 解除 すると再描画される模様
        _native_map.SelectAnnotation(_running_bike, true);
        _native_map.DeselectAnnotation(_running_bike,true);
      });

    };
    _timer.Enabled = true;
    _timer.Start ();
  }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1