我正在关注本教程enter link description here,根据此网站服务的经度和纬度,为地图中的每个位置添加图标:(json格式)
{
success: 1,
total: 2,
locals: [
{
id_local: "59",
local_longi: "20",
local_latit: "25894"
},
{
id_local: "60",
local_longi: "10.33699",
local_latit: "25.997745"
}
]
}
这是我的代码:
private async void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
await
this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(
async () =>
{
UriS = "MyURL";
var http = new HttpClient();
http.MaxResponseContentBufferSize =Int32.MaxValue;
var response = await http.GetStringAsync(UriS);
var rootObject = JsonConvert.DeserializeObject<NvBarberry.Models.RootObject>(response);
for(int i=0;i< int.Parse(rootObject.total); i++)
{
//Get the current location
Location[] location = new Location[2];
location[i] = new Location(double.Parse(rootObject.locals[i].local_latit), double.Parse(rootObject.locals[i].local_longi)); //I get the error here
//Update the position of the GPS pushpin
MapLayer.SetPosition(GpsIcon, location[i]);
//Set the radius of the Accuracy Circle
GpsIcon.SetRadius(args.Position.Coordinate.Accuracy);
//Make GPS pushpin visible
GpsIcon.Visibility = Windows.UI.Xaml.Visibility.Visible;
//Update the map view to the current GPS location
MyMap.SetView(location[i], 17);
}
}));
}
我收到此错误:
Input string was not in a correct format
在这一行:
location[i] = new Location(double.Parse(rootObject.locals[i].local_latit), double.Parse(rootObject.locals[i].local_longi));
如果这有帮助,这是调试的结果:
所以请问我如何更正我的代码,根据其位置放置图标 谢谢你的帮助
答案 0 :(得分:2)
调试并检查错误中rootObject.locals[i].local_latit
和rootObject.locals[i].local_longi
的内容。如果它们最终为null而不是声明的字符串,则会抛出此错误。
答案 1 :(得分:1)
我在MSDN上做了一点挖掘。试试这个:
location[i] = new Location(
double.Parse(
rootObject.locals[i].local_latit,
System.Globalization.NumberStyles.Float
),
double.Parse(
rootObject.locals[i].local_longi,
System.Globalization.NumberStyles.Float
)
);
以下是MSDN链接:https://msdn.microsoft.com/en-us/library/system.globalization.numberstyles(v=vs.110).aspx