【问题标题】:How to cluster Google Maps in Xamarin Forms如何在 Xamarin 表单中集群 Google 地图
【发布时间】:2019-05-23 12:23:18
【问题描述】:

我正在尝试在我的 xamarin 表单应用程序中对 Google 地图上的标记进行聚类。 这是我的模型:

public class MapModel
{
    public Field[] Fields { get; set; }
}

public class Field
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Node[] Nodes { get; set; }
    public string Icon { get; set; }
}

public class Node
{
    public float Long { get; set; }
    public float Lat { get; set; }
}

这是我用来获取经度和纬度值的服务

class MapService
    {
        public static async Task<MapModel> GetMapData(string token, string lngCode)
        {
            try
            {
                MapModel mapModels = new MapModel();
                string url = DataURL.BASE_URL + "agronetmobile/mapdata?lngCode=" + lngCode;
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                    using (HttpResponseMessage message = await client.GetAsync(url))
                    {
                        if (message.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            string rawResponse = await message.Content.ReadAsStringAsync();
                            mapModels = JsonConvert.DeserializeObject<MapModel>(rawResponse);
                        }
                    }
                }
                return mapModels;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

服务返回正确的值,但我什至不知道如何使用它们。 我安装了 Xamarin.Forms.GoogleMaps.Clustering 并试图找到一些好的分步教程,但没有找到任何有用的东西。许多教程都是关于 Xamarin.Android 的。 我正在使用 MVVM 模式。

【问题讨论】:

标签: google-maps xamarin.forms markerclusterer


【解决方案1】:

这是一个链接 how to use Mapsin Xamarin.Forms

1.安装 Xamarin.Forms.Maps NuGet 包

2.Xamarin.Forms.Forms.Init方法在每个应用项目中都需要

3.平台配置(参考link

4.当您从服务中获取位置时,因此在获得正确的值后,您可以这样做:

//new Position(37,-122) is your postion center
var map = new Map(
 MapSpan.FromCenterAndRadius(new Position(37,-122), Distance.FromMiles(0.3))) {
            IsShowingUser = true,
            HeightRequest = 100,
            WidthRequest = 960,
            VerticalOptions = LayoutOptions.FillAndExpand
        };
    var stack = new StackLayout { Spacing = 0 };
    stack.Children.Add(map);
    Content = stack;

如果你想在地图上添加图钉,你可以使用

var position = new Position(37,-122); // Latitude, Longitude
var pin = new Pin {
        Type = PinType.Place,
        Position = position,
        Label = "custom pin",
        Address = "custom detail info"
    };
map.Pins.Add(pin);

【讨论】:

  • 我得到的“地图”不包含采用 1 个参数错误的构造函数。应该在哪里添加你的代码部分?
  • 我添加了地图所在页面的图像。所以我的主页有三个选项卡和选项卡地图应该显示地图(如图像上)但带有别针。
  • 你安装NugetPackage了吗?
  • 是的,我安装了。
  • @NitrusBrio 不是 1 个参数,它需要 2 个参数
猜你喜欢
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
相关资源
最近更新 更多