[英]How can i simplify pins in maps
So I have implemented Google Maps on my VS Xamarin project.所以我在我的 VS Xamarin 项目上实现了谷歌地图。 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.
我的 map 上有一堆标记,每次创建一个我都会创建一个整体,所以我想简化这个过程,如果可能的话,从 excel 文件中提取信息。
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.这里我只显示 2 个引脚,但实际上,我有 30 个引脚。 How could I make this simpler?
我怎样才能让这更简单? Thanks a lot: :)
非常感谢: :)
note this is all pseudocode, not syntactically correct json/C#请注意,这都是伪代码,在语法上不正确 json/C#
first, create a json file with your data and include it in your project首先,使用您的数据创建一个 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} }
]
then, in your code然后,在您的代码中
// read the file
var json = File.ReadAllText(path);
// deserialize using NewtonSoft
var places = DeserializeObject<List<Place>>(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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.