在Button单击中添加JSON层-Xamarin.Forms

时间:2018-08-07 03:54:15

标签: c# google-maps xamarin xamarin.forms dependencies

我是Xamarin.Forms的新手,我想在地图上单击按钮时添加JSON图层。我该怎么办?

我正在使用Xamarin.Forms.GoogleMaps创建地图。

我尝试添加JSON层的依赖项。我找到了android的解决方案,但我被困在ios上

这是我的代码:

private void Close_Tapped(object sender, EventArgs e)
{
    try
    {
        var _IIGeoJsonLayer = DependencyService.Get<IGeoJsonLayer>();
        _IIGeoJsonLayer.AddLayerJson();
    }
    catch (Exception ex)
    {

    }
}

这是我的依赖服务:

public void AddLayerJson()
{
    GoogleMap gmap =null;
    GeoJsonLayer layer = new GeoJsonLayer(gmap, Resource.Raw.jsonFile, Android.App.Application.Context);
    layer.AddLayerToMap();
}

1 个答案:

答案 0 :(得分:0)

CustomRenderer.CS

  protected override void OnMapReady(GoogleMap map)
    {
        base.OnMapReady(map); 
         gmap = map;
        KMLClass _class = new KMLClass(gmap);
    }

Class.cs

  public class KMLClass
  {
  public static  GoogleMap gmap;

    public KMLClass(GoogleMap map)
    {
        if(gmap==null)
        {
            if(map!=null)
            {
                gmap = map;
            }
        }
    }

    public void AddKML()
    {
        GeoJsonLayer layer = new GeoJsonLayer(gmap, Resource.Raw.jsonFile, Android.App.Application.Context);
        layer.AddLayerToMap();
    }

依赖类

 public class JsonLayerAdd : IGeoJsonLayer
{
    GoogleMap map = null;
    public void AddLayerJson()
    {
        KMLClass _map = new KMLClass(map);
        _map.AddKML();
    }
}