【问题标题】:Add a JSON layer in a Button click - Xamarin.Forms在按钮单击中添加 JSON 层 - Xamarin.Forms
【发布时间】:2018-08-07 18:13:09
【问题描述】:

我是 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();
}

【问题讨论】:

  • 您没有引用“屏幕上”GoogleMap 实例,为什么不向自定义地图渲染器子类添加方法
  • @SushiHangover 实际上,当我在渲染器中添加代码时,我想通过单击按钮将 geojson 图层添加到地图中,它在页面第一次加载时执行

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


【解决方案1】:

CustomRenderer.CS

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

类.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();
    }
}

【讨论】:

    猜你喜欢
    • 2018-06-16
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    相关资源
    最近更新 更多