问题描述
因此,我已经在VS Xamarin项目上实现了Google Maps.我知道有一种方法可以简化我的代码,但是我不知道还能做些什么.我的地图上有一堆标记,每次创建标记时,我都会创建一个整体,因此我想简化此过程,并尽可能从excel文件中提取信息.
我的代码:
使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用System.Linq;使用System.Text;使用System.Threading.Tasks;使用Xamarin.Forms;使用Xamarin.Forms.Maps;命名空间------{[DesignTimeVisible(false)]公共局部类MainPage:ContentPage{公共MainPage(){InitializeComponent();头寸头寸=新头寸(36.9628066,-122.0194722);MapSpan mapSpan =新的MapSpan(position,0.01,0.01);地图map = new Map(mapSpan){MapType = MapType.Hybrid};内容=地图;引脚引脚=新引脚{标签="Santa HEY MAN",地址=有木板路的城市",类型= PinType.Place,职位=新职位(36.9628066,-122.0194722)};map.Pins.Add(pin);引脚引脚2 =新引脚{标签=美国",地址="2020",类型= PinType.Place,职位=新职位(36.9628066,-122.0194722)};map.Pins.Add(pin2);}}}
这里我只显示2个图钉,但实际上,我有30个图钉.我怎样才能使它更简单?非常感谢!:)
注意
首先,使用数据创建一个json文件,并将其包含在您的项目中
[{标签=美国"地址="2020",纬度="36.9628066"长="-122.0194722"},...{标签="Santa HEY MAN",地址=有木板路的城市",纬度="36.9628066".长=" -122.0194722}}]
然后,在您的代码中
//读取文件var json = File.ReadAllText(path);//使用NewtonSoft反序列化var place = DeserializeObject< List< Place>>>(json);//然后创建图钉foreach(地方上的各种地方){var pin = new Pin{标签= place.Label,地址=地点.地址,类型= PinType.Place,位置=新位置(place.Lat,place.Long)};map.Pins.Add(pin);}
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!:)
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 = "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);
}
这篇关于如何简化地图中的图钉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!