我试图在脚本的主要代码中使用getCurrentPosition的值(new_lat,new_lon)。我的一段代码:
var rememberLat = document.getElementById('id_latitude').value;
var rememberLong = document.getElementById('id_longitude').value;
function success(position) {
var lat = document.getElementById('id_latitude');
var lon = document.getElementById('id_longitude');
lat.value = position.coords.latitude;
lon.value = position.coords.longitude;
var new_lat = lat.value;
var new_lon = lon.value;
return {
new_lat: new_lat,
new_lon: new_lon
}
};
navigator.geolocation.getCurrentPosition(success);
if (!rememberLat || !rememberLong) {
rememberLat = new_lat;
rememberLong = new_lon;
}
var tileLayer = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x} /{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org /copyright">OpenStreetMap</a> Contributors'
});
var map = new L.Map('map', {
'center': [rememberLat, rememberLong],
'zoom': 8,
'layers': [tileLayer]
});
但我可以返回并将它们分配出回调函数。它引发了以下错误:
未捕获的ReferenceError:未定义new_lat