我有以下UIViewController实现:
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.MapKit;
using MonoTouch.CoreLocation;
namespace App.UI
{
public partial class ProductsMapScreen : UIViewController
{
private MapDelegate mapDelegate;
public ProductsMapScreen() : base("ProductsMapScreen", null)
{
TabBarItem = new UITabBarItem
{
Title = "Map",
Image = UIImage.FromFile("Images/tabmap.png")
};
}
public Func<CLLocationCoordinate2D> GetCurrentLocation;
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.mapDelegate = new MapDelegate();
this.mapView.Delegate = this.mapDelegate;
SetVisibleRegion();
AnnotateUsersCurrentLocation();
}
public override void ViewDidUnload()
{
base.ViewDidUnload();
ReleaseDesignerOutlets();
}
public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation)
{
return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
}
private void AnnotateUsersCurrentLocation()
{
var location = GetCurrentLocation();
this.mapView.AddAnnotation(new UserAnnotation(location));
}
private void SetVisibleRegion()
{
this.mapView.SetRegion(GetVisibleRegion(), true);
}
private MKCoordinateRegion GetVisibleRegion()
{
var currentLocation = GetCurrentLocation();
var span = new MKCoordinateSpan(0.2,0.2);
var region = new MKCoordinateRegion(currentLocation,span);
return region;
}
public class MapDelegate : MKMapViewDelegate
{
public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, NSObject annotation)
{
var userAnnotation = annotation as UserAnnotation;
if(userAnnotation != null)
return getViewForUserAnnotation(mapView, userAnnotation);
throw new Exception();
}
private MKAnnotationView getViewForUserAnnotation(MKMapView mapView, UserAnnotation annotation)
{
var annotationId = "userAnnotation";
var annotationView = mapView.DequeueReusableAnnotation (annotationId) as MKPinAnnotationView;
if (annotationView == null)
annotationView = new MKPinAnnotationView (annotation, annotationId);
annotationView.PinColor = MKPinAnnotationColor.Green;
annotationView.CanShowCallout = true;
annotationView.Draggable = true;
//annotationView.RightCalloutAccessoryView = UIButton.FromType (UIButtonType.DetailDisclosure);
return annotationView;
}
}
}
}
永远不会调用MapViewDelegate来解析我添加的单个注释的视图。我不知道为什么。
答案 0 :(得分:1)
我有一个类似的问题 - 在我的情况下,我混淆了坐标(地球上的纬度/长点)和MKMapPoints(地图上的2d xy点)。我不确定你遇到的问题是否相同,但这是我要检查的第一件事。如果委托认为它不在地图的可见区域
,则不会被调用你可以用MKMapPointForCoordinate&amp; MKCoordinateForMapPoint
答案 1 :(得分:1)
好的我不确定为什么MapDelegate类不起作用,但这个实现对我有用。我正在使用MT 5.2.2。
using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.MapKit;
using MonoTouch.CoreLocation;
namespace App.UI
{
public partial class ProductsMapScreen : UIViewController
{
public Func<CLLocationCoordinate2D> GetCurrentLocation;
public ProductsMapScreen() : base("ProductsMapScreen", null)
{
TabBarItem = new UITabBarItem
{
Title = "Map",
Image = UIImage.FromFile("Images/tabmap.png")
};
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
this.mapView.GetViewForAnnotation += GetViewForAnnotation;
SetVisibleRegion();
AnnotateUsersCurrentLocation();
}
public override bool ShouldAutorotateToInterfaceOrientation(UIInterfaceOrientation toInterfaceOrientation)
{
return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
}
private void AnnotateUsersCurrentLocation()
{
var location = GetCurrentLocation();
this.mapView.AddAnnotation(new[] {new UserAnnotation(location)});
}
private void SetVisibleRegion()
{
this.mapView.SetRegion(GetVisibleRegion(), true);
}
private MKCoordinateRegion GetVisibleRegion()
{
var currentLocation = GetCurrentLocation();
var span = new MKCoordinateSpan(0.2,0.2);
var region = new MKCoordinateRegion(currentLocation,span);
return region;
}
private MKAnnotationView GetViewForAnnotation(MKMapView mapView, NSObject annotation)
{
var userAnnotation = annotation as UserAnnotation;
if(userAnnotation != null)
return getViewForUserAnnotation(mapView, userAnnotation);
throw new Exception();
}
private MKAnnotationView getViewForUserAnnotation(MKMapView mapView, UserAnnotation annotation)
{
var annotationId = "userAnnotation";
var annotationView = mapView.DequeueReusableAnnotation (annotationId) as MKPinAnnotationView;
if (annotationView == null)
annotationView = new MKPinAnnotationView (annotation, annotationId);
annotationView.PinColor = MKPinAnnotationColor.Green;
annotationView.CanShowCallout = true;
annotationView.Draggable = true;
annotationView.RightCalloutAccessoryView = UIButton.FromType (UIButtonType.DetailDisclosure);
return annotationView;
}
}
}