2013-03-09 77 views
0

我有一個很簡單的問題,轉換「[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

+0

不是一個真正的直接的答案,但爲什麼不與抽象一些的本地Gmaps API的東西圖書館去了?很多你正在寫的代碼..試試Gmaps.js或jquery gmaps ui – 2013-03-09 20:46:05

+0

未定義?你從哪裏得到element.distance?元素是否設置在某個地方?對不起,元素,element.distance – Rafe 2013-03-09 20:49:31

回答

5

如何

var result = ${center}; 
var destination = new google.maps.LatLng(result[0], result[1]); 

google.maps.LatLng不接受數組。它接受的論據是:數字,數字,布爾。

https://developers.google.com/maps/documentation/javascript/reference#LatLng

+0

我已經嘗試了這個,但也得到了相同的錯誤 – 2013-03-09 21:20:51

+0

未捕獲TypeError:無法讀取行中未定義的屬性「文本」var distance = element.distance.text; – 2013-03-09 21:21:10

+0

@johnSmith您應該檢查元素的狀態以查看它是否成功,if(element.status =='OK')' – Musa 2013-03-09 22:07:50