Convert “[52.43242, 4.43242]” to google LatLng

情到浓时终转凉″ 提交于 2019-12-18 09:47:59

问题


i have a very simple question,

I try to let google calculate the distance between an adress and a gsp-variable containing

[53.57532, 10.01534]

i´m not able to get variable ${center} to an google.maps.LatLng-Object

heres the function

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 
  }
 }
}
}
});

in the "source code" above google throws error

 Uncaught TypeError: Cannot read property 'text' of undefined 

i tryed alot of senseless shit, but i just don get it,

maybe someone can give a hint what i am doing wrong? thanks in advance

according to the comments here´s my gmap3 also failing, but i woulkd rather this one to put to work but i guess it´s the same problem here, that converting to lat lon does´nt work

  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);
}
}
});

}

here i´m parsing an adress as a string to the function as adress


回答1:


How about

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

google.maps.LatLng doesn't accept an array. Arguments it accepts are: number, number, bool.

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



来源:https://stackoverflow.com/questions/15315722/convert-52-43242-4-43242-to-google-latlng

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!