简体   繁体   English

如何用很多图钉填充 xamarin.forms.maps - Xamarin

[英]How to fill xamarin.forms.maps with a lot of pins - Xamarin

I have 1919 coordinates with latitude and longitude on txt file with this format:我在 txt 文件中有 1919 坐标和经纬度,格式如下:

    44.05215, 27.03266,
    44.05056, 27.13236,
    44.04887, 27.23206,
    44.04709, 27.33175,
    44.04522, 27.43143,
    44.04326, 27.5311,
    44.0412, 27.63077,
    44.03906, 27.73043,
    44.0345, 27.92973,
    44.10396, 22.64161,
    44.10638, 22.74137,
    44.10871, 22.84114,
    44.11095, 22.94092,
    44.1131, 23.0407,

Is there a way to fill xamarin.forms.maps with this coordinates and if it's possible how to set the coordinates on the position property and each (index + 1) in label property?有没有办法用这个坐标填充xamarin.forms.maps ,如果可能的话,如何在position属性和label属性中的每个(索引 + 1)上设置坐标?

For now I set one pin on the map like that (just for test):现在我像这样在 map 上设置了一个引脚(仅用于测试):

map.MoveToRegion(MapSpan.FromCenterAndRadius(
                        new Position(42.1499994, 24.749997),
                        Distance.FromMiles(100)));

        Pin pin = new Pin
        {
            Label = "Santa Cruz",
            Address = "The city with a boardwalk",
            Type = PinType.Place,
            Position = new Position(42.1499994, 24.749997)
        };

        map.Pins.Add(pin);

       

Since I don't want the data to be read via a text file on a mobile device, how would it be best to write static data and then to be submitted as coordinates of the pins?由于我不希望通过移动设备上的文本文件读取数据,因此最好写入 static 数据然后作为引脚坐标提交?

Can I get an example of how to set the coordinates in List or Array or something else and after how to set like a coordinates to the pins and set each (index + 1) on the label property?我能举个例子说明如何在 List 或 Array 或其他东西中设置坐标,以及如何为引脚设置坐标并在 label 属性上设置每个(索引 + 1)吗?

As one of the comments pointed out though, there are almost always better options than hardcoding.正如其中一条评论指出的那样,几乎总是有比硬编码更好的选择。 The preferable alternative would be an API call to get the positions so the app would not need to be recompiled when the positions need updated.更好的选择是调用 API 来获取位置,这样当位置需要更新时应用程序就不需要重新编译。 The next best alternative would be an file or resource that a non-technical member of the team could update if needed.下一个最佳选择是团队中的非技术成员可以根据需要更新的文件或资源。

However, If you're absolutely set on hardcoding the positions, I would do it in a static class with a static list:但是,如果您完全决定对位置进行硬编码,我会在 static class 和 static 列表中进行:

public static class HardcodedLocations
{
    public static List<Position> Positions = new List<Position>
    {
        new Position(44.05215, 27.03266),
        new Position(44.05056, 27.13236),
        new Position(44.04887, 27.23206)
    };
}

Then where you need to add the pins, you can use a For loop to keep the index while adding the pins:然后在需要添加引脚的地方,可以使用 For 循环在添加引脚的同时保留索引:

for (int i = 0; i < HardcodedLocations.Positions.Count; i++)
{
    var pin = new Pin
    {
        Label = $"Santa Cruz - {i+1}",
        Address = "The city with a boardwalk",
        Type = PinType.Place,
        Position = HardcodedLocations.Positions[i]
     };

     map.Pins.Add(pin);
}

在此处输入图像描述

Note that I have no idea on what the performance of adding almost 2000 pins in a loop would look like.请注意,我不知道在一个循环中添加近 2000 个引脚的性能会是什么样子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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