我想显示用户地理位置附近的所有博物馆。当我想用 user_lat_long 替换 init 中的 latLng 时遇到问题。由于某种原因,在 init 中使用 alert 时 user_lat_long 值未定义。你能向我解释我做错了什么吗?我仍然是 JS 的新手。谢谢!
function init() {
var latLng = new google.maps.LatLng(44.43, 26.11);
Demo.map = new google.maps.Map(Demo.mapContainer, {
zoom: 13,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP});
var request = {
location: latLng,
radius: 50000,
types: ['museum']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(Demo.map);
service.nearbySearch(request, function (results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
{
var placeLoc = results[i].geometry.location;
var marker = new google.maps.Marker({
map: Demo.map,
position: placeLoc
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(restults[i].name);
infowindow.open(map, this);
});
}
}
}
});
}
google.maps.event.addDomListener(window, 'load', init);
function pozitie() {
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(currentPositionCallback);
} else {
alert('The browser does not support geolocation');
}
function currentPositionCallback(position) {
user_lat_long = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var marker = new google.maps.Marker({
position: user_lat_long,
map: Demo.map,
icon: image
});
Demo.map.setCenter(user_lat_long);
Demo.map.setZoom(15);
}