我试图通过调用返回此字符串的 ASP.net 方法将以下 JSON 文本的坐标映射到 Mapquest Map 上,但我无法弄清楚如何从中提取纬度和经度。
{"d":"{\"results\":[{\"fullName\":\"Albert Pinto\",\"callType\":\"Other - See Comments\",\"comments\":\"He was not happy with our dealer\u0027 s service. Had to be pacified.\",\"geolatitude\":38.9661791,\"geolongitude\":-94.7185354,\"geolocation\":\"{\\\"places\\\":[{\\\"street1\\\":\\\"Bond St\\\",\\\"postalCode\\\":\\\"66214\\\",\\\"address\\\":\\\"8951 Bond St, Overland Park, KS 66214, , United States\\\",\\\"displayAddress\\\":\\\"8951 Bond St, Overland Park, KS 66214, , United States\\\",\\\"street\\\":\\\"Bond St\\\",\\\"countryCode\\\":\\\"US\\\",\\\"region2\\\":\\\"\\\",\\\"longitude\\\":\\\"-94.718535\\\",\\\"region1\\\":\\\"\\\",\\\"latitude\\\":\\\"38.966179\\\",\\\"country_code\\\":\\\"US\\\",\\\"country\\\":\\\"United States\\\",\\\"city\\\":\\\"Overland Park\\\"}],\\\"source\\\":{\\\"locationServicesEnabled\\\":true,\\\"hasCompass\\\":true,\\\"purpose\\\":\\\"Get Current Location\\\"},\\\"success\\\":true}\",\"address\":\"8951 Bond St, Overland Park, KS 66214, , United States\",\"createdAt\":\"2012-01-18T05:57:58.923Z\",\"updatedAt\":\"2012-01-18T05:57:58.923Z\",\"objectId\":\"cqJK1nF1sB\"}]}"}
我已经搜索了几个小时并尝试了几种不同的方法(尝试从 asp.net 返回一个 Lat/Long 数组,尝试使用 JQuery 解析 JSON)但我对 JavaScript 的有限知识似乎阻碍了任何进展。
任何帮助或指示将不胜感激。这是我的 JavaScript 到目前为止的样子......
<script type="text/javascript">
MQA.EventUtil.observe(window, 'load', function () {
$.ajax({
url: "Default.aspx/SendAnSMSMessage", // This method returns the JSON string
type: "POST", // data has to be POSTed
contentType: "application/json", // posting JSON content
dataType: "text",
//dataType: "JSON", // type of data is JSON (must be upper case!)
timeout: 10000, // AJAX timeout
success: function (result) {
console.log(result.toString()); //The JSON string is copied from the console here..
alert(result);
//NOT SURE WHAT TO DO NEXT HERE TO EXTRACT LAT/LONG.
/*Create a new POI collection.*/
var sc = new MQA.ShapeCollection(), poi, latlng = { lat: 39.0, lng: -82.0 }, i;
/*Create additional POIs and add them to the shape collection.*/
for (i = 0; i < 5; i++) {
latlng.lat = latlng.lat + 0.01;
latlng.lng = latlng.lng - 0.01;
poi = new MQA.Poi(latlng);
sc.add(poi);
}
/*Construct an instance of MQA.TileMap with the shape collection in the map constructor.*/
window.map = new MQA.TileMap({
elt: document.getElementById('map'),
collection: sc,
bestFitMargin: 100
});
},
error: function (xhr, status) {
alert(status + " - " + xhr.responseText);
}
});
});
</script>
这是调用 REST API 的 ASP.Net 代码。
[WebMethod] public static string SendAnSMSMessage()
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("//myurl");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Credentials = new NetworkCredential("name", "pwd");
httpWebRequest.Method = "GET";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
return responseText;}
}