如何简化地图上的图钉?

huangapple go评论87阅读模式
英文:

How can i simplify pins in maps

问题

所以我已经在我的VS Xamarin项目上实现了Google Maps。
我知道有一种方法可以简化我的代码,但我不知道如何更进一步。
我的地图上有很多标记,每次我创建一个标记,我都会创建一个完整的标记,所以我想简化这个过程,如果可能的话,从Excel文件中提取信息。

我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Maps;

namespace ------
{
    
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            Position position = new Position(36.9628066, -122.0194722);
            MapSpan mapSpan = new MapSpan(position, 0.01, 0.01);
            Map map = new Map(mapSpan)
            {
               MapType = MapType.Hybrid
            
            };
            Content = map;
            Pin pin = new Pin
            {
                Label = "Santa HEY MAN",
                Address = "The city with a boardwalk",
                Type = PinType.Place,
                Position = new Position(36.9628066, -122.0194722)
            };
            map.Pins.Add(pin);

            Pin pin2 = new Pin
            {
                Label = "USA",
                Address = "2020",
                Type = PinType.Place,
                Position = new Position(36.9628066, -122.0194722)
            };
            map.Pins.Add(pin2);


        }

    }
}

在这里,我只展示了2个标记,但实际上,我有30个标记。
我该如何简化这个过程?
非常感谢! 如何简化地图上的图钉?

英文:

So I have implemented Google Maps on my VS Xamarin project.
I know there is a way to simplify my code but I don't know how more could I do it.
I have a bunch of markers on my map and each time I create one I create at whole so I want to simplify this process and if possible extract the information from an excel file.

My code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Maps;

namespace ------
{
    
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            Position position = new Position(36.9628066, -122.0194722);
            MapSpan mapSpan = new MapSpan(position, 0.01, 0.01);
            Map map = new Map(mapSpan)
            {
               MapType = MapType.Hybrid
            
            };
            Content = map;
            Pin pin = new Pin
            {
                Label = "Santa HEY MAN",
                Address = "The city with a boardwalk",
                Type = PinType.Place,
                Position = new Position(36.9628066, -122.0194722)
            };
            map.Pins.Add(pin);

            Pin pin2 = new Pin
            {
                Label = "USA",
                Address = "2020",
                Type = PinType.Place,
                Position = new Position(36.9628066, -122.0194722)
            };
            map.Pins.Add(pin2);


        }

    }
}

Here I only show 2 pins but in reality, I have 30 pins.
How could I make this simpler?
Thanks a lot!
如何简化地图上的图钉?

答案1

得分: 0

以下是翻译好的部分:

首先,创建一个包含您的数据并将其包含在项目中的 JSON 文件。

[ 
  { "Label": "USA", "Address": "2020", "Lat": "36.9628066", "Long": "-122.0194722" },
  ... 
  { "Label": "Santa HEY MAN", "Address": "The city with a boardwalk", "Lat": "36.9628066", "Long": "-122.0194722" }
]

然后,在您的代码中:

// 读取文件
var json = File.ReadAllText(path);

// 使用 NewtonSoft 进行反序列化
var places = DeserializeObject<List<Place>>(json);

// 然后创建标记点
foreach (var place in places)
{
   var pin = new Pin
        {
            Label = place.Label,
            Address = place.Address,
            Type = PinType.Place,
            Position = new Position(place.Lat, place.Long)
        };
   map.Pins.Add(pin);
}

请注意,上述代码是伪代码示例,不是 JSON 或 C# 的正确语法。

英文:

note this is all pseudocode, not syntactically correct json/C#

first, create a json file with your data and include it in your project

[ 
  { Label = &quot;USA&quot; Address = &quot;2020&quot;, Lat = &quot;36.9628066&quot; Long = &quot;-122.0194722&quot; },
  ... 
  { Label = &quot;&quot;Santa HEY MAN&quot;, Address = &quot;The city with a boardwalk&quot;, Lat = &quot;36.9628066&quot; Long = &quot;-122.0194722} }
]

then, in your code

// read the file
var json = File.ReadAllText(path);

// deserialize using NewtonSoft
var places = DeserializeObject&lt;List&lt;Place&gt;&gt;(json);

// then create pins
foreach (var place in places)
{
   var pin = new Pin
        {
            Label = place.Label,
            Address = place.Address,
            Type = PinType.Place,
            Position = new Position(place.Lat, place.Long)
        };
   map.Pins.Add(pin);
}

huangapple
  • 本文由 发表于 2020年8月7日 19:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63300677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定