从纬度/经度地理编码获取谷歌地图地名

问题描述 投票:0回答:3

我正在使用 Google API,我正在尝试获取地名以防止在地图上固定 Lat Lng。 我知道 Google 没有针对 Lat Longs 的地名,而是针对 Place ID 我正在做的是 Geocoding Lat 渴望获取地址,并将其 Place Id 发送到 Google Place Services 以获取该地点的特定名称,我只能像这样获得街道的短/长名称。

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

 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);
          }
        });
      }
javascript google-maps geocoding google-places
3个回答
0
投票

地理编码器将找到最近的地址。如果您想要一个“地点”,请使用 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");
  }
});

概念证明

代码片段:

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


0
投票

您可以使用以下代码段,它通过单击地图上的某个位置获取 placeid,然后通过将 placeid 发送到该地点的服务来检索坐标。

  const { Map } = await google.maps.importLibrary("maps");
 
  const {PlacesService} = await google.maps.importLibrary("places");

  googleMap = new Map(mapLocation, {center: { lat: latitude, lng: longitude }, zoom: 17, mapId: '9056811779c66d19',});

    const marker = new Marker({map: googleMap, position: { lat: latitude, lng: longitude }, title: mapsDeliveryLocationTextField.value, animation:Animation.BOUNCE});


   googleMap.addListener('click', function (ev) {
       //alert(ev.placeId);
       console.log(ev);

       const place = new PlacesService(googleMap);
       place.getDetails({placeId:ev.placeId}, (result, status)=>{

           let  placename = result.name;
           let address = result.formatted_address;
           let lat = result.geometry.location.lat();
           let long = result.geometry.location.lng();

           alert(placename + " "+ address + " "+lat + " "+long);


       });
  


-3
投票
latlng.toString() 

给你名字和国家

© www.soinside.com 2019 - 2024. All rights reserved.