代码之家  ›  专栏  ›  技术社区  ›  Mizlul

如何在google geocoding调用中传递一些参数

  •  0
  • Mizlul  · 技术社区  · 7 年前

    我有一个带有四个字段地址的对象数组,field1是当前位置(存储),其余是地址信息。

    我的问题是如何确保在结尾处有key:value(store:cords)的结果,这样我就知道哪个商店有哪些cords。

    这是我迄今为止写的代码:

    var NodeGeocoder = require('node-geocoder')
    const csv=require('csvtojson')
    const csvFilePath='location.csv'
    csv({noheader:true})
    .fromFile(csvFilePath)
    .then((jsonObj)=>{
        convertAddressesToCoords(jsonObj, function(coords){
            console.log('converting finished.');
            console.log(coords.length);
            console.log(coords);
        });
    });
    
    function convertAddressesToCoords(addresses, callback) {
        var coords = [];
        var options = {
          provider: 'google',
          httpAdapter: 'https', 
          apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxx', 
          formatter: null        
        };
    
        var geocoder = NodeGeocoder(options);
    
        for(var i = 0; i < addresses.length; i++) {
            currAddress = addresses[i].field2 + ' ' + addresses[i].field3 + ' ' + addresses[i].field4;
            geocoder.geocode(currAddress, function(err, results) {
                    coords.push(results);
                        if(coords.length == addresses.length) {
                            if( typeof callback == 'function' ) {
                                callback(coords);
                            }
                        }
            });
        }
    }
    

    目前的代码工作得很好,我可以得到每个地址的坐标,但问题是在最后我不知道哪个商店有哪个坐标,因为谷歌地理编码CAL是异步的,所以我找不到一种方法如何使这工作。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Jonas Wilms    7 年前

    const getCoords = geocoder => address => new Promise((resolve, reject) => {
       address = [address.field2, address.field3, address.field4].join(" ");
       geocoder.geocode(address, function(err, coords) {
          if(err) reject(err) else resolve({ address, coords });
       });
    });
    

    getCoords(NodeGeocoder(options))({field2: "Unknown Location"})
      .then(({ address, coords }) => console.log(adress + " is at " + coords));
    

    csv({noheader:true})
    .fromFile(csvFilePath)
    .then(data => Promise.all(data.map(getCoords(NodeGeocoder(options)))
    .then(positions => {
      //...
     })
    

    positions


    const currAddress = addresses[i].field2 + ' ' + addresses[i].field3 + ' ' + addresses[i].field4;
    

    coords.push({ coords: results, address: currAddress });
    
        2
  •  0
  •   Vasyl Moskalov    7 年前

    function convertAddressesToCoords(addresses, callback) {
      var coords = [];
      var options = {
        provider: 'google',
        httpAdapter: 'https', 
        apiKey: 'xxxxxxxxxxxxxxxxxxxxxxxx', 
        formatter: null        
      };
    
      var geocoder = NodeGeocoder(options);
      return Promise.all(
        adresses.map(
          (address)=>new Promise((resolve,reject)=>{
            geocoder.geocode(addresses[i].field2 + 
               ' ' + addresses[i].field3 + 
               ' ' + addresses[i].field4, 
               function(err, results) {
                 if (err) {
                   return reject(err);
                 }
                 return resolve(results);
               }
            );
          })
        )
      );
    }
    

    csv({noheader:true})
    .fromFile(csvFilePath)
    .then((jsonObj)=>convertAddressesToCoords(addresses))
    .then((coords)=>console.log(coords))
    
    推荐文章