将JSON对象发送到google distanceMatrixService

时间:2014-10-22 18:52:22

标签: json google-maps-api-3

我正尝试以下列形式向Google Distance Matrix Service发送JSON对象:

{
   "1":{
      "title":"PLACE NAME",
      "zip":"14212",
      "lat":"42.654321",
      "lng":"-78.654654",
      "addr":"ADDRESS",
      "cID":"1"
   },
   "2":{
      "title":"PLACE NAME",
      "zip":"14225",
      "lat":"42.687432",
      "lng":"-78.365485",
      "addr":"ADDRESS",
      "cID":"2"
   },
   "3":{
      "title":"PLACE NAME",
      "zip":"14226",
      "lat":"42.321687",
      "lng":"-78.654968",
      "addr":"ADDRESS",
      "cID":"3"
   },
   "4":{
      "title":"PLACE NAME",
      "zip":"14226",
      "lat":"42.3268732",
      "lng":"-78.6546873",
      "addr":"ADDRESS",
      "cID":"4"
   },
   "5":{
      "title":"PLACE NAME",
      "zip":"14228",
      "lat":"43.216875",
      "lng":"-78.43243574",
      "addr":"ADDRESS",
      "cID":"5"
   },
   "6":{
      "title":"PLACE NAME",
      "zip":"14226",
      "lat":"42.584687",
      "lng":"-78.254987",
      "addr":"ADDRESS",
      "cID":"6"
   },
   "7":{
      "title":"PLACE NAME",
      "zip":"14223",
      "lat":"42.265847",
      "lng":"-78.369587",
      "addr":"ADDRESS",
      "cID":"7"
   },
   "8":{
      "title":"PLACE NAME",
      "zip":"14201",
      "lat":"42.785698",
      "lng":"-78.234565",
      "addr":"ADDRESS",
      "cID":"8"
   },
   "9":{
      "title":"PLACE NAME",
      "zip":"14225",
      "lat":"42.111111",
      "lng":"-78.222222",
      "addr":"ADDRESS",
      "cID":"9"
   },
   "10":{
      "title":"PLACE NAME",
      "zip":"14203",
      "lat":"42.333333",
      "lng":"-78.444444",
      "addr":"ADDRESS",
      "cID":"10"
   },
   "11":{
      "title":"PLACE NAME",
      "zip":"14220",
      "lat":"42.555555",
      "lng":"-78.555555",
      "addr":"ADDRESS",
      "cID":"11"
   }
}

我编辑了一些正在发送的数据(标题,地址和加密/纬度),但我发送的真实数据是合法的谷歌地图地址信息。

将此信息发送到服务时,我不断收到错误

InvalidValueError: in property destinations: at index 0: not a string; and not a LatLng or LatLngLiteral: in property lat: not a number

是否无法将JSON对象发送到服务?

如果必须将其转换为数组,我应该怎么做呢?

这是调用服务的函数:

function calculateDistances(){

var origin = parseDeliveryAddress();
var destination = locations;

    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix({
        origins: [origin],
        destinations: [destination],
        travelMode: google.maps.TravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
        avoidHighways: false,
        avoidTolls: false
    }, callback);

}

parseDeliveryAddress获取用户输入地址,并将其转换为字符串。

locations是从数据库中获取的json对象。

感谢您的任何见解!

1 个答案:

答案 0 :(得分:0)

通过简单地将json数据推送到具有以下内容的数组来解决这个问题:

var addresses = [];

    jQuery.each(destination, function(){
        addresses.push(this.addr);          
    });

距离矩阵服务现在接受此。