javascript 如何从谷歌地图中的经度和纬度获取place_id?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33146448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get place_id from longitude and latitude in google Map?
提问by Neelabh Singh
I can get place Id by using This Link. 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. I am moving the marker on Google Map, So how I can get place_id on Map.
我可以使用此链接获取地点 ID 。但问题是这是基于位置搜索。我想使用 draggeble 标记获取 place_id,所以我不能使用上面的链接。我正在移动 Google 地图上的标记,那么如何在地图上获取 place_id。
Through marker.getPosition() we can get the latitude and longitude. So I want to know the place_idthrough latitude and longitude. How can I get, please tell me
通过marker.getPosition()我们可以得到纬度和经度。所以我想通过纬度和经度知道place_id。我怎样才能得到,请告诉我
latitude=marker.getPosition().lat();
longitude=marker.getPosition().lng();
回答by Ivan Jovovi?
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
.
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);
}
});
回答by Man Coding
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= lat, lng&key= YOUR_API_KEY
You can then easily parse the JSON object by Javascript JSON.parse().
然后,您可以通过 Javascript JSON.parse() 轻松解析 JSON 对象。
回答by sajal rajabhoj
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. You just need to pass latitude and longitude in url and your api key (ex : latlng=21.1497409, 79.08747970000002 & key=YOUR API KEY). Here is my get service in ExampleService Class
我得到了解决这个问题的新方法。在这里,我使用 google http 服务根据经度和纬度获取位置的总信息。您只需要在 url 和您的 api 密钥中传递纬度和经度(例如:latlng=21.1497409, 79.08747970000002 & key=YOUR API KEY)。这是我在 ExampleService 类中的获取服务
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
希望你会发现这很有用