简体   繁体   English

从 Lat/Lng 地理编码中获取 Google map 地名

[英]Get Google map place name from Lat/Lng geocoded

I am using Google API, I am trying to get Place name against pinning Lat Lng on the map.我正在使用 Google API,我正在尝试获取地名,以防止将 Lat Lng 固定在 map 上。 I understand Google don't have place names against Lat Longs but against Place ID What I am doing is Geocoding Lat longs to get the address, and send its Place Id to the Google Place Services to get specific name of the Place, I only get short/long name of the street like this.我了解 Google 没有针对 Lat Longs 的地名,但针对 Place ID 我正在做的是对 Lat longs 进行地理编码以获取地址,并将其 Place Id 发送到 Google Place Services 以获取该地方的特定名称,我只得到像这样的街道的短/长名称。

Plus If I click over the already tagged place on the map I get a difference address from that of the tagged Place另外,如果我单击 map 上已标记的地点,我会得到与标记地点不同的地址

 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);
          }
        });
      }

The Geocoder will find the nearest address.地理编码器将找到最近的地址。 If you want a "place" use the Google Places API nearbySearch .如果您想要一个“地方”,请使用 Google Places API附近搜索。

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概念证明小提琴

结果地图的屏幕截图

code snippet:代码片段:

 // 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>

latlng.toString() 

gives you the name and the country给你名字和国家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM