从经纬度地理编码获取Google地图地名

时间:2019-10-15 11:08:52

标签: javascript google-maps geocoding google-places

我正在使用Google API,我试图获取地名以防止将Lat Lng固定在地图上。 我了解Google没有针对Lat Longs的地名,但针对Place ID 我正在做的是对Longs进行地理编码以获取地址,并将其Place ID发送到Google Place Services以获取Place的特定名称,而我只会得到这条街道的简称。

另外,如果我点击地图上已经标记的地方,我会得到与标记的地方不同的地址

 geocodeLatLng(geocoder : any, myLatLng: { lat: number, lng: number }) {
        var latlng = {lat: myLatLng.lat, lng: myLatLng.lng};
        let self = this;
        var service = new google.maps.places.PlacesService(self.map);
        this.geocoder.geocode({'location': latlng}, function(results : any , status : any) {
          if (status === 'OK') {

            var request = {
                placeId: results[0]['place_id']
            };
            service.getDetails(request, function (place: any, status: any) {
                if (status === google.maps.places.PlacesServiceStatus.OK) {
                    console.log(place);
                }
            });
            if (results[0]) {

            } else {
              window.alert('No results found');
            }
          } else {
            window.alert('Geocoder failed due to: ' + status);
          }
        });
      }

2 个答案:

答案 0 :(得分:0)

Geocoder将找到最近的地址。如果您想要一个“地方”,请使用Google Places API nearbySearch

var searchOrigin = new google.maps.LatLng(40.6892494, -74.0445); // Statue of Liberty
var request = {
  location: searchOrigin,
  fields: ['name', 'geometry'],
  radius: 100
};

service = new google.maps.places.PlacesService(map);

service.nearbySearch(request, function(results, status) {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    var distance = 10000;
    for (var i = 0; i < results.length; i++) {
      var marker = createMarker(results[i], i);
      // find closest result
      var thisDist = google.maps.geometry.spherical.computeDistanceBetween(searchOrigin, marker.getPosition());
      if (thisDist < distance) {
        closest = marker;
        distance = thisDist;
      }
    }

    map.setCenter(closest.getPosition());
    google.maps.event.trigger(closest, "click");
  }
});

proof of concept fiddle

screenshot of resulting map

代码段:

// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

var map;
var service;
var infowindow;

function initMap() {
  var sydney = new google.maps.LatLng(-33.867, 151.195);

  infowindow = new google.maps.InfoWindow();

  map = new google.maps.Map(
    document.getElementById('map'), {
      center: sydney,
      zoom: 15
    });
  var searchOrigin = new google.maps.LatLng(40.6892494, -74.0445);
  var request = {
    location: searchOrigin,
    fields: ['name', 'geometry'],
    radius: 100
  };

  service = new google.maps.places.PlacesService(map);

  service.nearbySearch(request, function(results, status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
      var distance = 10000;
      for (var i = 0; i < results.length; i++) {
        console.log(results[i]);
        var marker = createMarker(results[i], i);
        var thisDist = google.maps.geometry.spherical.computeDistanceBetween(searchOrigin, marker.getPosition());
        if (thisDist < distance) {
          closest = marker;
          distance = thisDist;
        }
      }

      map.setCenter(closest.getPosition());
      google.maps.event.trigger(closest, "click");
    }
  });
}

function createMarker(place, i) {
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });

  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
  return marker;
}
html,
body,
#map {
  height: 100%;
  margin: 0;
  padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places,geometry&callback=initMap" async defer></script>

答案 1 :(得分:-1)

[ 3, 3, 9, 7, 9, 7, 6, 7, 6 ]
[ 9, 7, 9, 7, 6, 7, 6 ]
[ 7, 7, 6, 7, 6 ]
[ 6, 7, 6 ]
6


expected:

[ 3, 3, 9, 7, 9, 7, 6, 7, 6 ]
[ 9, 7, 9, 7, 6, 7, 6 ]
[ 7, 7, 6, 7, 6 ]
[ 6, 7, 6 ]
[7]
7

提供名称和国家/地区