使用google-maps

时间:2016-06-03 08:17:20

标签: javascript express google-maps-api-3 nodes geocoding

我希望获得存储在我的json中的几个位置的纬度和经度。 我正在获得经度和经度,但问题是他们被收到的顺序是荒谬的。

实施例

x= [ { "city" : "Mumbai" , "location" : "India"},{ "city" : "Mumbai" , "location" : "India"},{ "city" : "New Delhi" , "location" : "India"},{ "city" : "Mumbai" , "location" : "India"} ]

预期lt : [ "19.075" ,"19.075" ,"28.613" ,"19.075" ]

但我的输出顺序不同。 输出:

lt : [ "19.075" ,"19.075" ,"19.075" ,"28.613"]

lt : [ "19.075" , "28.613" ,"19.075" ,"19.075" ]

lt : [ "19.075" , "28.613","19.075" ,"19.075" ]

lt : [ "28.613" ,"19.075" ,"19.075" ,"19.075" ]

代码

lt=[];
lng=[];
for(k=0;k<x.length;k++)
{
  console.log(x[k].city);
  request({ url : 'https://maps.googleapis.com/maps/api/geocode/json?  address='+x[k].city+'&region='+x[k].country+'&key=AIzaSyCdZL4cxbxZrLf2El5FeNT2Kh_uqw3b61Y' , json: true },function (error, response, body) { 
      lt.push(body.results[0].geometry.location.lat);
      console.log(lt);
      lg.push(body.results[0].geometry.location.lng);
    });
}

0 个答案:

没有答案