2016-01-20 101 views
1

我正在按照本教程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)); 

如果這能幫助,這是調試的結果:

enter image description here

所以請我怎麼能糾正我的代碼,根據其位置 把圖標感謝您的幫助

回答

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

+0

感謝您的答覆,我解決了這個錯誤通過將我的變量轉換爲模型類的雙打:) – user3821206

2

調試並檢查rootObject.locals[i].local_latitrootObject.locals[i].local_longi出現錯誤。如果它們最終變爲null而不是聲明的字符串,它會拋出這個錯誤。

+0

感謝LeChosenOne您的回覆,但在調試的時候我已經verifried是rootObject.locals [I] .local_latit和rootObject.locals [I] .local_longi containe字符串值 – user3821206