在Google Maps API Places库中,我使用以下代码。但是,当用户单击id =“find”的按钮时,虽然地图将自身居中到用户的位置,但不会返回附近的地方。我正在混合使用地方库和地理定位API的代码,但我看不出我做错了什么。
任何帮助将不胜感激!
<script>
var map, infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 51.516401, lng: -0.152218},
zoom: 17
});
infoWindow = new google.maps.InfoWindow;
$("#find").click(function(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
infoWindow.open(map);
map.setCenter(pos);
var pyrmont = new google.maps.LatLng(pos.lat,pos.lng);
var request = {
location: pyrmont,
radius: '1000',
query: 'restaurant'
};
service = new google.maps.places.PlacesService(map)
service.textSearch(request, callback);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
handleLocationError(false, infoWindow, map.getCenter());
}
});
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
}
</script>
答案 0 :(得分:0)
添加createMarker功能
function createMarker(place) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
})
}