XAML我CustomMap添加多個customPins:試圖在Xamarin.Forms.iOS
<local:CustomMap x:Name="mymap" MapType="Street" IsShowingUser="true"/>
代碼:
var pin = new CustomPin();
pin.MapPin.Label = "Test";
pin.MapPin.Position = new Position(32, 10);
pin.MapPin.Label = "1";
pin.MapPin.Address = "394 Pacific Ave, San Francisco CA";
pin.Id = "Xamarin";
pin = new CustomPin();
pin.MapPin.Label = "Test2";
pin.MapPin.Position = new Position(33, 10);
pin.MapPin.Label = "2";
pin.MapPin.Address = "394 Pacific Ave, San Francisco CA";
pin.Id = "Xamarin";
mymap.CustomPins = new List<CustomPin> { pin };
mymap.Pins.Add (pin.MapPin);
有了這個代碼,我只得到了地圖上的一個引腳,引腳我在這種情況下創建的最新版本是「Test2」。 如何調整代碼以便添加多個引腳?
CustomMap:
public class CustomMap : Map
{
public MapContentType ContentType { get; set; }
public double CircleRadius { get; set; }
public List<Position> Positions { get; set; }
public List<CustomPin> CustomPins { get; set;}
public CustomCircle Circle { get; set; }
public CustomMap()
{
this.ContentType = MapContentType.Normal;
this.CircleRadius = 500;
this.Positions = new List<Position>();
}
}
CustomPin:
public class CustomPin
{
public CustomPin() {
MapPin = new Pin();
}
public string Id { get; set; }
public EventHandler Url { get; set; }
public Pin MapPin { get; set; }
}
自定義渲染:
public class CustomMapRenderer : MapRenderer
{
MKPolylineRenderer polylineRenderer;
UIView customPinView;
List<CustomPin> customPins;
MKCircleRenderer circleRenderer;
protected override void OnElementChanged (ElementChangedEventArgs<View> e)
{
base.OnElementChanged (e);
if (e.OldElement != null) {
var nativeMap = Control as MKMapView;
nativeMap.OverlayRenderer = null;
nativeMap.GetViewForAnnotation = null;
nativeMap.GetViewForAnnotation = null;
nativeMap.DidSelectAnnotationView -= OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView -= OnDidDeselectAnnotationView;
}
if (e.NewElement != null) {
var formsMap = (CustomMap)e.NewElement;
var nativeMap = Control as MKMapView;
customPins = formsMap.CustomPins;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
nativeMap.DidSelectAnnotationView += OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView += OnDidDeselectAnnotationView;
nativeMap.OverlayRenderer = GetOverlayRenderer;
CLLocationCoordinate2D[] coords = new CLLocationCoordinate2D[formsMap.Positions.Count];
int index = 0;
foreach (var position in formsMap.Positions) {
coords [index] = new CLLocationCoordinate2D (position.Latitude, position.Longitude);
index++;
}
var routeOverlay = MKPolyline.FromCoordinates (coords);
nativeMap.AddOverlay (routeOverlay);
}
}
MKOverlayRenderer GetOverlayRenderer (MKMapView mapView, IMKOverlay overlay)
{
if (polylineRenderer == null) {
polylineRenderer = new MKPolylineRenderer (overlay as MKPolyline);
polylineRenderer.FillColor = UIColor.Blue;
polylineRenderer.StrokeColor = UIColor.Black;
polylineRenderer.LineWidth = 10;
polylineRenderer.Alpha = 0.4f;
}
return polylineRenderer;
}
string pId = "PinAnnotation";
MKAnnotationView GetViewForAnnotation (MKMapView mapView, IMKAnnotation annotation)
{
MKAnnotationView annotationView = null;
if (annotation is MKUserLocation)
return null;
var anno = annotation as MKPointAnnotation;
var customPin = GetCustomPin (anno);
if (customPin == null) {
throw new Exception ("Custom pin not found");
}
MKAnnotationView pinView = (MKPinAnnotationView)mapView.DequeueReusableAnnotation (pId);
annotationView = mapView.DequeueReusableAnnotation (customPin.Id);
if (annotationView == null) {
annotationView = new CustomMKPinAnnotationView (annotation, customPin.Id);
annotationView.Image = UIImage.FromFile ("pin.png");
//((MKPinAnnotationView)pinView).PinColor = MKPinAnnotationColor.Green;
annotationView.CalloutOffset = new CGPoint (0, 0);
annotationView.LeftCalloutAccessoryView = new UIImageView (UIImage.FromFile ("monkey.png"));
annotationView.RightCalloutAccessoryView = UIButton.FromType (UIButtonType.DetailDisclosure);
((CustomMKPinAnnotationView)annotationView).Id = customPin.Id;
((CustomMKPinAnnotationView)annotationView).Url = customPin.Url;
}
annotationView.CanShowCallout = true;
return annotationView;
}
void OnDidSelectAnnotationView (object sender, MKAnnotationViewEventArgs e)
{
var customView = e.View as CustomMKPinAnnotationView;
customPinView = new UIView();
if (customView.Id == "Xamarin") {
customPinView.Frame = new CGRect (0, 0, 200, 84);
var image = new UIImageView (new CGRect (0, 0, 200, 84));
image.Image = UIImage.FromFile ("xamarin.png");
customPinView.AddSubview (image);
customPinView.Center = new CGPoint (0, -(e.View.Frame.Height + 75));
e.View.AddSubview (customPinView);
}
}
void OnDidDeselectAnnotationView (object sender, MKAnnotationViewEventArgs e)
{
if (!e.View.Selected) {
customPinView.RemoveFromSuperview();
customPinView.Dispose();
customPinView = null;
}
}
CustomPin GetCustomPin (MKPointAnnotation annotation)
{
var position = new Position (annotation.Coordinate.Latitude, annotation.Coordinate.Longitude);
foreach (var pin in customPins) {
if (pin.MapPin.Position == position) {
return pin;
}
}
return null;
}
}
}
如果我有一個正常的地圖,並使用當前的代碼,我已然後它的工作。如果我從我的數據庫收集數據會怎麼樣?那麼當我提取數據時,如果我無法手動創建不同的var pin1,2,3等,我將如何解決它? – medvedo
現在看到答案 –