2017-08-08 16 views
0

MKMapViewに赤い四角形を描こうとしています。地図は見えますが、矩形は見えません。私のコード:MKMapViewで矩形を描く

public override void ViewDidLoad() 
{ 
    base.ViewDidLoad(); 

    var areaMapView = new AreaMapView(); 

    areaMapView.SetTarget(45.5399396, -73.6534612); 

    areaMapView.AddZone(new List<Geolocation>() 
    { 
     new Geolocation() { Latitude = 25.774, Longitude = -80.190}, 
     new Geolocation() { Latitude = 18.466, Longitude = -66.118}, 
     new Geolocation() { Latitude = 32.321, Longitude = -64.757}, 
     new Geolocation() { Latitude = 25.774, Longitude = -80.190}, 
    }); 

    View = areaMapView; 
} 

public class AreaMapView : MKMapView 
{ 
    public AreaMapView() : base(UIScreen.MainScreen.Bounds) 
    { 
     this.ShowsUserLocation = true; 
     this.MapType = MKMapType.Satellite; 
    } 

    public void SetTarget(double longitude, double latitude) 
    { 
     this.AddAnnotations(new MKPointAnnotation() 
     { 
      Title = "Target", 
      Coordinate = new CLLocationCoordinate2D(longitude, latitude) 
     }); 
    } 

    public void AddZone(List<Geolocation> longitudeAndLatitudePoints) 
    { 
     var coords = new CLLocationCoordinate2D[longitudeAndLatitudePoints.Count]; 

     for (int i = 0; i < longitudeAndLatitudePoints.Count; i++) 
     { 
      double longitude = longitudeAndLatitudePoints[i].Longitude; 
      double latitude = longitudeAndLatitudePoints[i].Latitude; 

      coords[i] = new CLLocationCoordinate2D(longitude, latitude); 
     } 

     this.AddOverlay(MKPolyline.FromCoordinates(coords)); 
    } 
} 
+0

私は迅速に解決策を投稿できますか? @Darius –

+0

速報を使用している人は誰でも投稿できます。 – Darius

+0

あなたの問題を解決する答えはありますか? @Darius –

答えて

2

私はあなたが四角形を追加するMKPolygonMKPolygonRendererを使うべきだと思います。

こちらを参照:ここhttps://developer.xamarin.com/recipes/ios/content_controls/map_view/add_an_overlay_to_a_map/

サンプル:https://github.com/xamarin/recipes/tree/master/ios/content_controls/map_view/add_an_overlay_to_a_map

がサンプルに従ってください。これは、結果 ある

mapView.OverlayRenderer = (m, o) => 
     { 
      if (polygonRenderer == null) 
      { 
       polygonRenderer = new MKPolygonRenderer(o as MKPolygon); 
       polygonRenderer.FillColor = UIColor.Red; 
       polygonRenderer.StrokeColor = UIColor.Black; 
       polygonRenderer.Alpha = 0.5f; 
      } 
      return polygonRenderer; 
     }; 


     var coord = new CLLocationCoordinate2D[4]; 

     coord[0] = new CLLocationCoordinate2D(29.976111, 31.132778); 
     coord[1] = new CLLocationCoordinate2D(29.976111, 31.032778); 
     coord[2] = new CLLocationCoordinate2D(29.876111, 31.032778); 
     coord[3] = new CLLocationCoordinate2D(29.876111, 31.132778); 

     mapView.AddOverlay(MKPolygon.FromCoordinates(coord)); 

によってコード

mapView.OverlayRenderer = (m, o) => 
     { 
      if (circleRenderer == null) 
      { 
       circleRenderer = new MKCircleRenderer(o as MKCircle); 
       circleRenderer.FillColor = UIColor.Purple; 
       circleRenderer.Alpha = 0.5f; 
      } 
      return circleRenderer; 
     }; 

     circleOverlay = MKCircle.Circle(coords, 400); 
     mapView.AddOverlay(circleOverlay); 

を交換します10