[英]How to get place_id from longitude and latitude in google Map?
我可以使用此链接获取地点ID。 但问题是这是基于位置搜索。 我想使用draggeble标记获取place_id,所以我不能使用上面的链接。 我正在谷歌地图上移动标记,所以我如何在地图上获得place_id。
通过marker.getPosition()我们可以得到纬度和经度。 所以我想通过纬度和经度知道place_id 。 我怎么能得到,请告诉我
latitude=marker.getPosition().lat();
longitude=marker.getPosition().lng();
使用google.maps.Geocoder
进行Reverse Geocoding
如下所述: https : //developers.google.com/maps/documentation/javascript/examples/geocoding-reverse
您将收到包含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);
}
});
您可以使用Geocoder API发送请求,也可以只将HTTP请求发送到后面的Web服务。
您的样品申请:
https://maps.googleapis.com/maps/api/geocode/json?latlng= lat , lng &key = YOUR_API_KEY
然后,您可以通过Javascript JSON.parse()轻松解析JSON对象。
我有了解决这个问题的新方法。 在这里,我使用谷歌http服务获取基于经度和纬度的位置的总信息。 您只需要在url和api密钥中传递纬度和经度(例如:latlng = 21.1497409,79.08747970000002&key =您的API密钥)。 这是我在ExampleService类中的get服务
getService(url) {
return this.http.get(url).map((data: any) => data.json())
}
这可以放在你想要的任何地方,只需从需要位置数据的组件中调用以下服务即可
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']);
});
希望你会发现这很有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.