【问题标题】:How to get position (latitude and longitude) from a postal (zip) code in react?如何在反应中从邮政(邮政编码)获取位置(纬度和经度)?
【发布时间】:2021-07-29 12:35:30
【问题描述】:

我需要知道在 react 中从邮政 (zip) 代码中获取纬度和经度的最佳方法是什么?

我尝试使用来自 google API 的地理编码(使用 npm 包 react-geocode),但我无法仅根据邮政编码进行搜索。

所以我需要你对如何做到这一点的意见。源代码示例也将不胜感激。

提前感谢您的所有 cmets。问候...

【问题讨论】:

  • 你为什么认为这是可能的?邮政编码,AFAIK,对应的不是一对坐标,而是整个区域,有时是不相交的。
  • 1.限制问题:世界各地的邮政编码系统差异很大(格式、粒度等)。你在追求某个特定的国家吗? 2. 对图书馆的建议与 SO 无关,对地理定位数据库的建议更是如此——我投票结束这个问题。
  • @Cerberus :没错,但那我该怎么办?我希望用户输入他们的邮政编码,然后我想获得一个大致的地理坐标。

标签: reactjs geolocation latitude-longitude google-geolocation


【解决方案1】:

也许尝试这样的事情并根据您的需要进行调整,因为只给出了很少的上下文:

 var lat = '';
 var lng = '';

// depends on whether you really only have the zip code
 var address = {zipcode} or {city and state};

 geocoder.geocode({ 'address': address}, function(results, status) {
   if (status == google.maps.GeocoderStatus.OK) {
      lat = results[0].geometry.location.lat();
      lng = results[0].geometry.location.lng();
     });
   } else {
     console.log("Geocode was not successful for the following reason: " + status);
   }
 });
 console.log('Latitude: ' + lat + ' Longitude: ' + lng);

【讨论】:

    猜你喜欢
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多