我有代码:
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,但我不知道如何更新标记和地图中心。
任何帮助表示感谢。
答案 0 :(得分:0)
试试这个:
map.panTo(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
它以“移动地图”为中心......
答案 1 :(得分:0)
您以错误的方式将位置设置为标记,请尝试:
position: new google.maps.LatLng(latLng)
你可以用:
更新标记的位置newLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
marker.setPosition(newLatlng);
在showLocation(position)中调用
希望它有所帮助...