我試圖在谷歌地圖上使用谷歌自己的API創建標記。我的問題是,我正在從文本文件中讀取JSON數據,然後嘗試將其分解爲緯度和經度,以便我可以在地圖上放置標記。你可以在下面看到我的代碼。動態創建Google地圖LatLng類
var oReq = new XMLHttpRequest(); //New request object
oReq.onload = function() {
//This is where you handle what to do with the response.
//The actual data is found on this.responseText
//alert(this.responseText); //Will alert: the location latitude and longitude in JSON
locations = JSON.parse(this.responseText);
//alert(locations);
for(var i = 0; i < locations.length; i++){
var tempLocation = locations[i];
//alert(tempLocation);
var tempLat = tempLocation.latitude;
var tempLong = tempLocation.longitude;
tempLat = tempLat.toString();
tempLong = tempLong.toString();
pictureLocations[i] = new google.maps.LatLng(tempLat + ", " + tempLong);
}
var tempPictureLocation = pictureLocations.toString();
alert(tempPictureLocation);
};
我的問題是變量pictureLocations返回NaN。我相信他們在我的構建聲明中有些問題,但不確定究竟是什麼問題。如果我在某些座標中硬編碼,它也不起作用。謝謝您的幫助!
嘗試添加一個console.log()語句,當你從文件中獲得lat long值並查看它們傳遞給LatLng函數時它們是什麼。 – getbuckts 2014-12-05 01:46:00
[google.maps.LatLng](https://developers.google.com/maps/documentation/javascript/reference#LatLng)對象以兩個數字作爲參數,而不是包含由逗號分隔的兩個數字字符串的字符串。 – geocodezip 2014-12-05 01:47:46
[解析數據到谷歌地圖]可能的重複(http://stackoverflow.com/questions/18464174/parsing-data-onto-google-map/18466611#18466611) – geocodezip 2014-12-05 01:54:05