我有一个非常简单的问题,
我尝试让google计算地址与包含
的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
}
}
}
}
});
在google投放错误
上方的“源代码”中 Uncaught TypeError: Cannot read property 'text' of undefined
我尝试了很多无知的狗屎,但我只是不明白,
也许有人可以暗示我做错了什么? 提前谢谢
根据这里的评论,我的gmap3也失败了,但是我觉得这个还要投入工作,但我想这是同样的问题,转换为lat lon无法正常工作
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);
}
}
});
}
这里我正在将地址解析为函数的字符串adress
答案 0 :(得分: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