我有代码:
var watchID;
var geoLoc;
var geoService = navigator.geolocation;
if (geoService) {
geoService.getCurrentPosition(showCurrentLocation,errorHandler,{enableHighAccuracy: true});
} else {
alert("Your Browser does not support GeoLocation.");
}
function showCurrentLocation(position){
var latLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
//Google Map options
var myOptions = {zoom: 16,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var usermarker = new google.maps.Marker({position: latLng, map: map, title: "You are here!"});
map.setCenter(latLng);
usermarker.setPosition(latLng);
getLocationUpdate(usermarker, latLng)
}
function errorHandler(error){
alert("Error while retrieving current position. Error code: " + error.code + ",Message: " + error.message);
}
function getLocationUpdate(usermarker, latLng){
if(navigator.geolocation){
var options = {timeout:60000};
geoLoc = navigator.geolocation;
watchID = geoLoc.watchPosition(showLocation,
errorHandler,
options);
}else{
alert("Sorry, browser does not support geolocation!");
}
}
function showLocation(position){
op = document.getElementById("output");
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
op.innerHTML = "Latitude : " + latitude + "<br />Longitude: " + longitude;
}
它设置了地图并获取了我当前的位置。我正在尝试使地图居中并移动标记。getLocationUpdate 当前触发 watchLocation 但我不知道如何更新标记和地图中心。
任何帮助表示赞赏。