Javascript 点击 Google Maps API 并获取地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36892826/
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
Click on Google Maps API and get the address
提问by Basj
The following code allows to get the coordinates, on click. But how to get the address or city name or region name or country on click on the map, with Google Maps API?
以下代码允许在单击时获取坐标。但是如何使用 Google Maps API 在地图上点击获取地址或城市名称或地区名称或国家?
var myLatlng = new google.maps.LatLng(41.38,2.18);
var myOptions = { zoom: 13, center: myLatlng}
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
google.maps.event.addListener(map, 'click', function(event) {alert(event.latLng);});
html, body, #map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry,places&ext=.js"></script>
<div id="map-canvas"></div>
回答by putvande
You can pass the event.latLng
trough the Geocoder to get the address:
您可以通过event.latLng
Geocoder 来获取地址:
var myLatlng = new google.maps.LatLng(41.38, 2.18);
var myOptions = {
zoom: 13,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
var geocoder = new google.maps.Geocoder();
google.maps.event.addListener(map, 'click', function(event) {
geocoder.geocode({
'latLng': event.latLng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
alert(results[0].formatted_address);
}
}
});
});
回答by Ma Yubo
you cannot get only one result by only lat,lng from google api, but you could get some results by search lat,lng and a keyword in a radius.
您不能仅通过 lat,lng 从 google api 获得一个结果,但您可以通过搜索 lat,lng 和半径范围内的关键字获得一些结果。
the only way get specific place information you need search by its placeID
获取您需要通过 placeID 搜索的特定地点信息的唯一方法
var myLatlng = new google.maps.LatLng(41.38,2.18);
var myOptions = { zoom: 13, center: myLatlng}
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
var infoWindow;
var service;
google.maps.event.addListener(map, 'click', function(event) {
service = new google.maps.places.PlacesService(map);
infoWindow = new google.maps.InfoWindow();
var request = {
location: event.latLng,
keyword: 'food',
radius:500
};
service.radarSearch(request, callback);
});
function callback(results, status) {
if (status !== google.maps.places.PlacesServiceStatus.OK) {
console.error(status);
return;
}
for (var i = 0, result; result = results[i]; i++) {
addMarker(result);
}
}
function addMarker(place) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: {
url: 'http://maps.gstatic.com/mapfiles/circle.png',
anchor: new google.maps.Point(10, 10),
scaledSize: new google.maps.Size(10, 17)
}
});
google.maps.event.addListener(marker, 'click', function() {
service.getDetails(place, function(result, status) {
if (status !== google.maps.places.PlacesServiceStatus.OK) {
console.error(status);
return;
}
infoWindow.setContent(result.name+"<br>"+result.formatted_address+"<br>"+result.formatted_phone_number);
infoWindow.open(map, marker);
});
});
}