简体   繁体   中英

How to Cluster Xamarin.Forms.GoogleMaps Pin in PCL Project

Since I am doing a map based cross-platform project on Xamarin, And I'm stuck to the point where I need to cluster markers when zooming out. things I had tried:

  • GoogleMapUtility Project.
  • And tried to use ClusterManager of GoogleMapUtility through dependency. so that I can add a pin to cluster manager From PCL project. But to initialize cluster manager I need a Native Map instance ie Android.Gms.Maps.GoogleMap.(don't know how to get)

I know I can achieve it with that with custom map renderer, but I cant do that coz. a lot of code related to map is already written, I don't want to rewrite a Code. So Is there any possibility that I can get a native instance of the map that I can use on dependency service.

I created CustomRenderer For Map without creating a customMap class at PCL (inherit Map class) the code is below. but it's not get triggered. What wrong I am doing here...

custom renderer code:

[assembly: ExportRenderer(typeof(Map), typeof(MarkerClusterRenderer))]
namespace SamplingApp.Droid.CustomRenderers
{
    public class MarkerClusterRenderer : MapRenderer
    {
        ClusterManager _clusterManager;
        protected override void OnMarkerCreated(Pin outerItem, Marker innerItem)
        {
            base.OnMarkerCreated(outerItem, innerItem);
            AddToMarkerCluster();
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
        }
        protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
        {
            base.OnElementChanged(e);
        }

    public void AddToMarkerCluster()
    {
        _clusterManager = new ClusterManager(Android.App.Application.Context, NativeMap);
        //[do other things here]
    }

}
}

since I am is new to xamarin. little guidance is appreciated. am I in the right direction?

Thank you in advance. and sorry for my English.

I created CustomRenderer For Map without creating a customMap class at PCL (inherit Map class) the code is below. but it's not get triggered. What wrong I am doing here...

If you want to customize the map, you need to create a custom class for map control in PCL:

public class CustomMap:Map
{

}

And use it in xaml:

<local:CustomMap WidthRequest="320" HeightRequest="200"
        x:Name="MyMap"
        IsShowingUser="true"
        MapType="Hybrid"/>

And renderer:

[assembly:ExportRenderer(typeof(SamplingApp.CustomMap),
  typeof(MarkerClusterRenderer))]
namespace SamplingApp.Droid.CustomRenderers
{
    public class MarkerClusterRenderer:MapRenderer
    {
        ...

Then, your renderer's codes will be triggered correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM