我有一個很簡單的問題,轉換「[52.43242,4.43242]」谷歌經緯度
我嘗試讓谷歌計算ADRESS和含有GSP-變量之間的距離
[53.57532, 10.01534]
我我沒能獲得可變${center}
到google.maps.LatLng-Object
繼承人的功能
jQuery(document).ready(function() {
var result = '${center}';
console.log(result);// [53.57532, 10.01534]
var origin = new google.maps.LatLng(55.930385, -3.118425);
var destination = new google.maps.LatLng(result);
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false,
avoidTolls: false
}, callback);
function callback(response, status) {
if (status == google.maps.DistanceMatrixStatus.OK) {
console.log('status OK!');//status OK!
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.text;
var duration = element.duration.text;
var from = origins[i];
var to = destinations[j];
console.log(distance);//Uncaught TypeError: Cannot read property 'text' of undefined
}
}
}
}
});
上面谷歌的「源代碼」拋出錯誤
Uncaught TypeError: Cannot read property 'text' of undefined
我tryed無謂狗屎了很多,但我只是不明白,
也許有人可以給一個暗示什麼,我做錯了什麼? 在此先感謝
根據評論here's我的gmap3也失敗,但我woulkd而是這一個投入工作,但我想這是同樣的問題在這裏,轉換爲經緯度does'nt工作
function getDistance(adress){
var to;
var result = '${center}';
$("#getdistance").gmap3({
getdistance:{
options:{
origins:adress,
destinations:result ,
travelMode: google.maps.TravelMode.DRIVING
},
callback: function(results, status){
var html = "";
if (results){
for (var i = 0; i < results.rows.length; i++){
var elements = results.rows[i].elements;
for(var j=0; j<elements.length; j++){
switch(elements[j].status){
case "OK":
html += elements[j].distance.text + " (" + elements[j].duration.text + ")<br />";
break;
case "NOT_FOUND":
html += "The origin and/or destination of this pairing could not be geocoded<br />";
break;
case "ZERO_RESULTS":
html += "No route could be found between the origin and destination.<br />";
break;
}
}
}
} else {
html = "error";
}
console.log('start '+adress);
console.log('start '+latte);
console.log('start '+html);
}
}
});
}
這裏i'm解析ADRESS作爲一個字符串功能adress
不是一個真正的直接的答案,但爲什麼不與抽象一些的本地Gmaps API的東西圖書館去了?很多你正在寫的代碼..試試Gmaps.js或jquery gmaps ui – 2013-03-09 20:46:05
未定義?你從哪裏得到element.distance?元素是否設置在某個地方?對不起,元素,element.distance – Rafe 2013-03-09 20:49:31