2017-08-08 208 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

我可以在swift中發佈解決方案嗎? @Darius –

+0

將它發佈給任何使用swift的人,它會被他們提升。 – 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 (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); 

通過

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)); 

這是結果 enter image description here