我的代码收到此错误。我不确定我为什么会得到它,尽管我在地理定位方面没有太多经验。我认为问题可能在于获得 lat 和 lng 的位置,但我不确定它是什么。如果有人对我如何修改我的代码/想法以解释为什么这不起作用有任何建议,我们将不胜感激。
function callGeo() {
console.log("you called findLocation");
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getLocation, locationError);
}
else { console.log("no geolocation support");
return;
}
}
function getLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
if (!map) {
showMap(latitude, longitude);
}
addMarker(latitude, longitude);
}
function showMap(lat, long) {
var googleLatLong = new google.maps.LatLng(lat, long);
var mapOptions = {
zoom: 12,
center: googleLatLong,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var mapDiv = document.getElementById("map");
map = new google.maps.Map(mapDiv, mapOptions);
map.panTo(googleLatLong);
}
function addMarker(lat, long) {
var googleLatLong = new google.maps.LatLng(lat, long);
var markerOptions = {
position: googleLatLong,
map: map,
title: "Where I'm thinking today"
}
var marker = new google.maps.Marker(markerOptions);
}
function locationError(error) {
var errorTypes = {
0: "Unknown error",
1: "Permission denied by user",
2: "Position not available",
3: "Request timed out"
};
var errorMessage = errorTypes[error.code];
if (error.code == 0 || error.code == 2) {
errorMessage += " " + error.message;
}
console.log(errorMessage);
alert(errorMessage);
}