我越來越指數爲數組例外的界限之外在這條線System.IndexOutOfRangeException:索引是該數組地理編碼的API
string strLat = myCoordenates.Results[0].Geometry.Location.Lat.ToString();
這應該是從地理編碼拉緯度的邊界之外請求並將其變成一個字符串。
這裏是我使用的地理編碼類,我是從這裏:How to store geocoded address information into the database
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Web;
public class GoogleMapsDll
{
public class GoogleMaps
{
/// <summary>
///
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public static GeoResponse GetGeoCodedResults(string address)
{
string url = string.Format(
"http://maps.google.com/maps/api/geocode/json?address={0}®ion=dk&sensor=false",
HttpUtility.UrlEncode(address)
);
var request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse));
var res = (GeoResponse)serializer.ReadObject(request.GetResponse().GetResponseStream());
return res;
}
}
[DataContract]
public class GeoResponse
{
[DataMember(Name = "status")]
public string Status { get; set; }
[DataMember(Name = "results")]
public CResult[] Results { get; set; }
[DataContract]
public class CResult
{
[DataMember(Name = "geometry")]
public CGeometry Geometry { get; set; }
[DataContract]
public class CGeometry
{
[DataMember(Name = "location")]
public CLocation Location { get; set; }
[DataContract]
public class CLocation
{
[DataMember(Name = "lat")]
public double Lat { get; set; }
[DataMember(Name = "lng")]
public double Lng { get; set; }
}
}
}
public GeoResponse()
{ }
}
}
我讀過這件事(What is an IndexOutOfRangeException/ArgumentOutOfRangeException and how do I fix it?)和它的部分多次,但我仍然不知道如何解決這個問題。我覺得也許這可能是因爲我沒有從我的api請求中得到任何結果,但我不確定如何確定。
第四個元素在引發錯誤,檢查你的局部變量,看看什麼是存儲在myCoordenates.Results的例子。看起來你的API沒有任何返回。 – Michael 2014-11-21 15:48:33