简体   繁体   English

如何从google Map中的经度和纬度获取place_id?

[英]How to get place_id from longitude and latitude in google Map?

I can get place Id by using This Link . 我可以使用此链接获取地点ID。 But problem is this is based on location search. 但问题是这是基于位置搜索。 I want to get place_id using draggeble marker so I can't use above link. 我想使用draggeble标记获取place_id,所以我不能使用上面的链接。 I am moving the marker on Google Map, So how I can get place_id on Map. 我正在谷歌地图上移动标记,所以我如何在地图上获得place_id。

Through marker.getPosition() we can get the latitude and longitude. 通过marker.getPosition()我们可以得到纬度和经度。 So I want to know the place_id through latitude and longitude. 所以我想通过纬度和经度知道place_id How can I get, please tell me 我怎么能得到,请告诉我

latitude=marker.getPosition().lat();               
longitude=marker.getPosition().lng();

Use google.maps.Geocoder for Reverse Geocoding as explained here: https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse 使用google.maps.Geocoder进行Reverse Geocoding如下所述: https//developers.google.com/maps/documentation/javascript/examples/geocoding-reverse

You will recieve data containing results[1].place_id . 您将收到包含results[1].place_id数据results[1].place_id

var geocoder = new google.maps.Geocoder;

latitude=marker.getPosition().lat();               
longitude=marker.getPosition().lng();
var latlng = {lat: parseFloat(latitude), lng: parseFloat(longitude)};

geocoder.geocode({'location': latlng}, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      if (results[1]) {
        console.log(results[1].place_id);
      } else {
        window.alert('No results found');
      }
    } else {
      window.alert('Geocoder failed due to: ' + status);
    }
  });

You can either use the Geocoder API to send request, or simply send the HTTP request to the web service behind. 您可以使用Geocoder API发送请求,也可以只将HTTP请求发送到后面的Web服务。

Your sample request: 您的样品申请:

https://maps.googleapis.com/maps/api/geocode/json?latlng= lat , lng &key= YOUR_API_KEY https://maps.googleapis.com/maps/api/geocode/json?latlng= latlng &key = YOUR_API_KEY

You can then easily parse the JSON object by Javascript JSON.parse(). 然后,您可以通过Javascript JSON.parse()轻松解析JSON对象。

I got new way to solve this issue. 我有了解决这个问题的新方法。 Here I have used google http service to get total information of location based on longitude and latitude. 在这里,我使用谷歌http服务获取基于经度和纬度的位置的总信息。 You just need to pass latitude and longitude in url and your api key (ex : latlng=21.1497409, 79.08747970000002 & key=YOUR API KEY). 您只需要在url和api密钥中传递纬度和经度(例如:latlng = 21.1497409,79.08747970000002&key =您的API密钥)。 Here is my get service in ExampleService Class 这是我在ExampleService类中的get服务

 getService(url) {

    return this.http.get(url).map((data: any) => data.json())

}

this you can put anywhere you want and just call below service from component where you need location data 这可以放在你想要的任何地方,只需从需要位置数据的组件中调用以下服务即可

this._exampleService.getService("https://maps.googleapis.com/maps/api/geocode/json?latlng=21.1497409, 79.08747970000002&key=YOUR API KEY").subscribe(getplaceData => {
            var placeDataDest: any;
            placeDataDest = getplaceData;
            console.log("i got place id yeee " + placeDataDest['results'][0]['place_id']);
            console.log("i got place details yeee " + placeDataDest['results']);
        });

hope you will find this useful 希望你会发现这很有用

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

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