2016-05-30 29 views
0

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




    } 
} 

回答

1

你得到一個腳,因爲你要添加一個引腳。 你需要知道一些編程的基礎知識「如更改對象的屬性不會創建新的對象」

試試這個

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

    var pin1 = new CustomPin(); 
    pin1.MapPin.Label = "Test2"; 
    pin1.MapPin.Position = new Position(33, 10); 
    pin1.MapPin.Label = "2"; 
    pin1.MapPin.Address = "394 Pacific Ave, San Francisco CA"; 
    pin1.Id = "Xamarin"; 

    mymap.CustomPins = new List<CustomPin> { pin,pin1 }; 
    mymap.Pins.Add (pin.MapPin); 
    mymap.Pins.Add (pin1.MapPin); 

解決在評論中提到您的問題,App.Items是名單充分利用數據庫或Web服務人口(在我的情況下,網絡服務)`使用

System.Collections.Generic; 
using Xamarin.Forms; 
using Xamarin.Forms.Maps; 
using System; 

namespace App 
{ 
    public class MapPage : ContentPage 
    { 
     public CustomMap customMap1{ get; set;} 
     public MapPage() 
     { 
      Title ="Stores"; 
      Icon = "flag_map_marker4.png"; 


      var customMap = new CustomMap { 
       MapType = MapType.Street, 
       WidthRequest = App.ScreenWidth, 
       HeightRequest = App.ScreenHeight 
      }; 

      customMap.CustomPins = new List<CustomPin>(); 
      if (App.Items != null && App.Items.Count > 0) { 
       foreach (var t in App.Items) { 
        var temp = new CustomPin() { 
         Pin = new Pin() { 
          Label = t.Name, 
          Type = PinType.Place, 
          Position = new Position (t.Lat, t.Lon), 
          Address = t.Address1 
         }, 
         Url = t.Link 
        }; 
        customMap.CustomPins.Add (temp); 
       } 
       foreach (var pin in customMap.CustomPins) { 
        customMap.Pins.Add (pin.Pin); 
       } 
       // dont delete below code ,they will save you if timer doesnt work . 

       //var temp1 = new MapSpan(customMap.CustomPins [0].Pin.Position, 
//    if(Device.OS == TargetPlatform.iOS) 
//    customMap.MoveToRegion (MapSpan.FromCenterAndRadius (customMap.CustomPins [0].Pin.Position, Distance.FromMiles (0.20))); 

       if(Device.OS == TargetPlatform.Android) 
       customMap.MoveToRegion (MapSpan.FromCenterAndRadius (customMap.CustomPins [0].Pin.Position, Distance.FromMiles (55.0))); 
       if (Device.OS == TargetPlatform.iOS) { 
        Device.StartTimer (TimeSpan.FromMilliseconds (500),() => { 
         customMap.MoveToRegion (MapSpan.FromCenterAndRadius (customMap.CustomPins [0].Pin.Position, Distance.FromMiles (55.0))); 
         return false; 
        }); 
       } 
      } 

      Content = customMap; 
     } 

    } 


} 

` 不理會不關心你行!

+0

如果我有一個正常的地圖,並使用當前的代碼,我已然後它的工作。如果我從我的數據庫收集數據會怎麼樣?那麼當我提取數據時,如果我無法手動創建不同的var pin1,2,3等,我將如何解決它? – medvedo

+0

現在看到答案 –

0

我與Xamarin自定義地圖渲染器有同樣的問題。 我發現問題的原因是在OnMapElementClick函數中檢查mapOverlay == null

對此進行評論,現在所有功能都適用於多個針腳。 不知道爲什麼這是擺在首位。