【问题标题】:Getting coordinates from Yandex Map API Geocoder via multi-geocoder in NodeJs通过 NodeJs 中的多地理编码器从 Yandex Map API Geocoder 获取坐标
【发布时间】:2020-04-22 12:28:34
【问题描述】:

我需要获取一组地址的坐标,以便在 Yandex 地图小部件上显示它们。有很多地址,所以我要在nodejs服务器端获取坐标。我找到了包multi-geocoder,它看起来正是我的解决方案。所以我写了这个例子:

import MultiGeocoder from "multi-geocoder"

const geocoder = new MultiGeocoder({        
    provider: 'yandex',
    coordorder: 'latlong',
    lang: 'ru-RU',
    apikey: 'My API key from https://developer.tech.yandex.ru/'
});

geocoder.geocode(['Москва'], {
    apikey: 'My API key from https://developer.tech.yandex.ru/'
})
.then(function (res) {
    console.log(res);
});

我收到了回复:

{
   result: { type: 'FeatureCollection', features: [] },
   errors: [ { request: 'Москва', index: 0, reason: 'Forbidden' } ]
}

我认为 apiKey 出了点问题,但无法弄清楚究竟是什么。如何从 nodejs 脚本中正确获取坐标?有可能\合法吗?

谢谢。

【问题讨论】:

    标签: node.js geocoding yandex-maps yandex-api


    【解决方案1】:

    如果您对 API 密钥有任何问题,请联系 Yandex。地图支持。问题可能出在密钥本身,或您的 IP/域。只有 Yandex 可以确定确切的原因。

    如果您需要将地理编码器中的点添加到地图中,那么在 JS API 中使用地理编码会更容易。简单地顺序处理地址数组的元素就足够了:

    var searchArr = ['Москва, Фадеева, 5','Москва, Трубная, 31','Москва, Маросейка, 11'];
    
    searchArr.forEach(function(item) {
        ymaps.geocode(item, {
        results: 1
    }).then(function (res) {
            var firstGeoObject = res.geoObjects.get(0),
                coords = firstGeoObject.geometry.getCoordinates();
            myMap.geoObjects.add(firstGeoObject);
        });
    });
    

    如果您在 Yandex 地图上显示从数据中接收的数据并遵守其他使用条款,则可以免费使用 Yandex.Map API。 如果必须至少违反其中一项条件,则应切换到商业许可证。在这里查看费用:https://tech.yandex.com/maps/tariffs/doc/jsapi/prices/index-docpage/

    【讨论】:

      猜你喜欢
      • 2016-08-31
      • 1970-01-01
      • 2020-01-16
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多