我一直在这几个小时,但我似乎无法找到正确的对象进到setCenter这个特定的场景:setCenter似乎并不接受我送入它的任何对象
var map,
currentPositionMarker,
mapCenter = new google.maps.LatLng(14.668626, 121.24295)
function initializeMap(){
map = new google.maps.Map(_('map'), {
zoom: 18,
center: mapCenter,
mapTypeId: 'roadmap',
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
});
}
function locError(error) { alert("current position not be found");}
function setCurrentPosition(pos) {
currentPositionMarker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
),
title: "Current Position"
});
map.panTo(new google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
));
}
function displayAndWatch(position) {
setCurrentPosition(position);
watchCurrentPosition(position);
}
function watchCurrentPosition(pos) {
var positionTimer = navigator.geolocation.watchPosition(
function (position) {
setMarkerPosition(
currentPositionMarker,
position
);
**map.setCenter(google.maps.LatLng(pos.coords.latitude,pos.coords.latitude));**
});
}
function setMarkerPosition(marker, position) {
marker.setPosition(
new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude)
);
}
function initLocationProcedure() {
initializeMap();
infoWindow = new google.maps.InfoWindow({ maxWidth: 400 });
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayAndWatch, locError);
} else { alert("Geolocation API not supported"); }
}
(这是特别令人沮丧的,因为我不得不绕着这个街区去验证每个变化是否奏效!)。 任何人都知道我在上面的map.setCenter行中做错了什么?
你不必'new'在'google.maps.LatLng'面前的时候,你正在做的'setCenter'通话,让你AREN得到一个LatLng对象,你会得到'undefined',因为对LatLng的非构造函数调用不会返回任何东西。 –