【发布时间】:2017-04-20 06:22:54
【问题描述】:
我正在使用 google maps api 地点搜索来获取附近地点的详细信息。在这个 google 的例子中,我们必须在函数 initMap() 中指定中心:lat,lng。如何从我当前的位置提供它们?
jsfiddle 链接:https://jsfiddle.net/mayureshbakshi/xj3n2ecc/2/
<!DOCTYPE html>
<html>
<head>
<title>Place details</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
#map {
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -33.866, lng: 151.196},
zoom: 15
});
var infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
}, function(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
'Place ID: ' + place.place_id + '<br>' +
place.formatted_address + '</div>');
infowindow.open(map, this);
});
}
});
}
</script>
</head>
<body>
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap">
</script>
</body>
</html>
【问题讨论】:
-
@JaromandaX 你评论时我正在编辑问题。
-
好的,我不可能知道你会发布一个问题,然后在 10 分钟后添加代码
标签: javascript google-maps-api-3