Ваша функция проверяет, может ли навигатор найти позицию GPS (это чисто функциональность устройства/браузера, а не служба Google). Но ваша функция не проверяет состояние геокодирования (это служба Google).
Посмотрите, что ваша консоль саис так:
navigator.geolocation.getCurrentPosition(
function (pos) {
var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
console.log('Just before geocode is called');
geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': latlng}, function (results, status) {
console.log('callback of geocoder called');
if (status == google.maps.GeocoderStatus.OK) {
console.log('Status is okay');
}
else {
console.log('Status is not okay, the reason: ' + status);
}
});
},
errorGetCurrentPositionCallback,
{enableHighAccuracy: true, timeout: 10000, maximumAge: 600000}
);
Вот функционирующий пример
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
var map;
function initialize() {
var latlng = new google.maps.LatLng(50.45, 4.45); // set your own default location. This gets called if the parameters are left blank.
var myOptions = {
zoom: 15,
center: latlng
};
map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
}
function errorGetCurrentPositionCallback() {
console.log('Your device cannot find your location');
}
// initiate Google Maps + request the position of the user.
function page_loaded() {
initialize();
navigator.geolocation.getCurrentPosition(
// success callback
function (pos) {
var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
var accuracy = pos.coords.accuracy;
map.setCenter(latlng);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "You are here! (at least within a " + accuracy + " meter radius)"
});
console.log('Just before geocode is called');
geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': latlng}, function (results, status) {
console.log('callback of geocoder called');
if (status == google.maps.GeocoderStatus.OK) {
console.log('Status is okay; you have the address of this location');
//
}
else {
console.log('Status is not okay, the reason: ' + status);
}
});
},
errorGetCurrentPositionCallback,
{enableHighAccuracy: true, timeout: 10000, maximumAge: 600000}
);
}
google.maps.event.addDomListener(window, 'load', page_loaded);
</script>
<style>
#map-canvas {
width: 500px;
height: 400px;
}
</style>
<div id="map-canvas"></div>
Привет, спасибо за ваш ответ, но это тот же самый код «кроме проверки состояния» что я специально пропустил (не полезно для примера). Проблема в том, что строка "console.log ('callback called')" не достигнута. Поэтому бесполезно проверять статус после. Что-то не так, но это не в коде. У вас есть идея? возможно ли иметь исходный код api? – user3515688
Я должен проверить его более тщательно. Кстати, вы что-то записываете в errorGetCurrentPositionCallback? Что-нибудь там интересное? –
Нет У меня ничего нет ни в errorGetCurrentPositionCallback. Я верю, что callback не запускается в коде google. Но почему ? – user3515688