如何使用Angular 6从纬度和经度获取地址

3
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(pos => {
        x.value = "" + pos.coords.longitude;
        y.value = "" + pos.coords.latitude;

        this.locations.push({
          lng: pos.coords.longitude,
          lat: pos.coords.latitude,
          time: local.value,
          picture: picture.value,
          publicIp: publicIp.value,
          privateIp: privateIp.value
        });

        console.log(this.locations);

        console.log(local.value);

1
使用第三方API。例如谷歌API - Oscar R
2个回答

2
从纬度和经度获取地址,您将需要使用一些反向地理编码API。尝试搜索一些提供相同服务的免费API。
ArcGIS 提供了一个免费(和付费)的API。

https://developers.arcgis.com/rest/geocode/api-reference/geocoding-reverse-geocode.htm

它可以为给定的位置(纬度/经度)提供地址。甚至不需要API密钥,但建议您获取一个免费的以避免速率限制。祝您实现愉快 :)

嗨,谢谢你。顺便说一下,我有自己的API,并且我正在使用<agm>获取经度和纬度,但是我真的很难使用我得到的经纬度获取地址。 :( - Sed
@Sed - 很高兴它对你有用。如果它有帮助,请点赞答案。 - Mukul_Vashistha

0

您需要使用反向地理编码 API,例如以下链接: https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

以下是来自上述链接的示例:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Reverse Geocoding</title>
    <style>
      #map {
        height: 100%;
      }
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #floating-panel {
        position: absolute;
        top: 10px;
        left: 25%;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
        text-align: center;
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }
      #floating-panel {
        position: absolute;
        top: 5px;
        left: 50%;
        margin-left: -180px;
        width: 350px;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
      }
      #latlng {
        width: 225px;
      }
    </style>
  </head>
  <body>
    <div id="floating-panel">
      <input id="latlng" type="text" value="40.714224,-73.961452">
      <input id="submit" type="button" value="Reverse Geocode">
    </div>
    <div id="map"></div>
    <script>
      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 8,
          center: {lat: 40.731, lng: -73.997}
        });
        var geocoder = new google.maps.Geocoder;
        var infowindow = new google.maps.InfoWindow;

        document.getElementById('submit').addEventListener('click', function() {
          geocodeLatLng(geocoder, map, infowindow);
        });
      }

      function geocodeLatLng(geocoder, map, infowindow) {
        var input = document.getElementById('latlng').value;
        var latlngStr = input.split(',', 2);
        var latlng = {lat: parseFloat(latlngStr[0]), lng: parseFloat(latlngStr[1])};
        geocoder.geocode({'location': latlng}, function(results, status) {
          if (status === 'OK') {
            if (results[0]) {
              map.setZoom(11);
              var marker = new google.maps.Marker({
                position: latlng,
                map: map
              });
              infowindow.setContent(results[0].formatted_address);
              infowindow.open(map, marker);
            } else {
              window.alert('No results found');
            }
          } else {
            window.alert('Geocoder failed due to: ' + status);
          }
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>
  </body>
</html>

非常感谢你,真的很感激。我会尝试这个,希望能对我有用 :) - Sed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接