다른 기능에서 사용자 위치 정보 탐색 기능을 실행하는 방법을 찾고 있습니다 mapInit()
. 거의 작동하지만, 제대로 된 지 확인하기 위해 getCurrentPosition()
의 적절한 콜백을 가질 수 없습니다. 매번 정의되지 않은 값을 반환합니다.Geolocation에서 확인을 반환 할 수 없습니까?
내 지리적 위치 객체가 다른 작업을 수행해야하므로 트리거하지 않으려합니다. mapInit()
. 그것은 사용자 위치를 얻고, 그것을 기록하고 true
또는 false
를 돌려 보내야한다. 어떤 추측?
감사합니다 :)
// Get user current position, return true or false
//
var geolocation = {
get: function() {
if (alert(navigator.geolocation.getCurrentPosition(this.success, this.error, {
enableHighAccuracy: true,
maximumAge: 5000
})) {
return true;
} else {
return false;
}
},
success: function(position) {
this.last = position; // record last position
return true;
},
error: function() {
alert('code: ' + error.code + 'n' + 'message: ' + error.message + 'n')
return false;
},
last: undefined,
}
// Initialize leaflet map and get user location if coords are undefined
//
var mapInit = function(latitude, longitude) {
if (!latitude && !longitude) { // if no latlng is specified, try to get user coords
if (geolocation.get()) {
latitude = geolocation.last.coords.latitude;
longitude = geolocation.last.coords.longitude;
} else {
alert('oups!');
}
}
var map = L.map('map').setView([latitude, longitude], 15);
L.tileLayer('http://{s}.tile.cloudmade.com/#APIKEY#/68183/256/{z}/{x}/{y}.png', {
minZoom: 13,
maxZoom: 16,
}).addTo(map);
var marker = L.marker([latitude, longitude]).addTo(map);
}
'getCurrentPosition()'이 반환 된 것처럼 보였고 API 사양에 W3C가 쓴 것처럼 비동기 적으로 사용자 위치를 얻습니다. "호출되면 즉시 반환해야하며 비동기 적으로 장치의 현재 위치를 가져와야합니다." [(출처)] (http://dev.w3.org/geo/api/spec-source.html#geolocation). 누구는 단서가 있습니까? – Antoine