映射背景清晰

时间:2018-06-19 20:59:05

标签: ios xamarin xamarin.ios mapkit

我的应用程序需要地图功能,但是我希望能够清除图块并在其后面显示背景。我看不到主视图的背景,如下所示:

partial class ViewController : UIViewController
{
  public ViewController(IntPtr handle) : base(handle)
  {
  }

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

     var map = new MKMapView(View.Bounds);
     View.Add(map);

     // set map center and region
     const double lat = 0;
     const double lon = 0;
     var mapCenter = new CLLocationCoordinate2D(lat, lon);
     var mapRegion = MKCoordinateRegion.FromDistance(mapCenter, 200000, 200000);
     map.CenterCoordinate = mapCenter;
     map.Region = mapRegion;

     // set the map delegate
     map.Delegate = new MyDelegate();

     var tiles = new MKTileOverlay
     {
        CanReplaceMapContent = true
     };
     map.AddOverlay(tiles);

     View.BackgroundColor = UIColor.Blue; //something is drawn over this
  }

  class MyDelegate : MKMapViewDelegate
  {
     public override MKOverlayRenderer OverlayRenderer(MKMapView mapView, IMKOverlay overlay)
     {
        return new MyOverlayRenderer(overlay);
     }
  }

  class MyOverlayRenderer : MKOverlayRenderer
  {
     public MyOverlayRenderer(IMKOverlay overlay) : base(overlay)
     { }

     public override void DrawMapRect(MKMapRect mapRect, nfloat zoomScale, CGContext context)
     {
        //base.DrawMapRect(mapRect, zoomScale, context); //appears to do the same thing as below

        var rect = new CGRect(mapRect.MinX, mapRect.MinY, mapRect.Width, mapRect.Height);
        context.SetFillColor(UIColor.Clear.CGColor);
        context.FillRect(rect);
     }
  }
}

非常感谢您对此的见解。该代码应按原样运行。

0 个答案:

没有答案