iOS Safari 上 javascript 中的地理位置
Geolocation in javascript on iOS Safari
我的 objective 是 return 我在 iOS 设备上使用 Safari 的地理定位坐标。在我的桌面浏览器上,我使用 Google 地图地理定位 API 键调用下面的函数 tryGeolocation()
。
这 在桌面 Chrome 上有效 ,但在 Safari(桌面和 iOS)和 Chrome (iOS) 中有效我 return 遇到了 "Position Unavailable"
错误。我尝试过 5 秒和 10 秒的超时,但同样的情况发生了。
在 iOS 中调用 Google 地图地理定位 API 是否有已知的解决方法?
var apiGeolocationSuccess = function(position) {
alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var tryAPIGeolocation = function() {
jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key="MY API KEY", function(success) {
apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
})
.fail(function(err) {
alert("API Geolocation error! \n\n"+err);
});
};
var browserGeolocationSuccess = function(position) {
alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var browserGeolocationFail = function(error) {
switch (error.code) {
case error.TIMEOUT:
alert("Browser geolocation error !\n\nTimeout.");
break;
case error.PERMISSION_DENIED:
if(error.message.indexOf("Only secure origins are allowed") == 0) {
tryAPIGeolocation();
}
break;
case error.POSITION_UNAVAILABLE:
alert("Browser geolocation error !\n\nPosition unavailable.");
break;
}
};
var tryGeolocation = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
browserGeolocationSuccess,
browserGeolocationFail,
{maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
}
};
tryGeolocation();
我刚开始使用 iOS 浏览器,如有任何建议,我们将不胜感激!!
尝试不使用 getCurrentPosition() 的第三个参数,因为我们在 error.POSITION_UNAVAILABLE 上遇到问题。
var apiGeolocationSuccess = function(position) {
alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var tryAPIGeolocation = function() {
jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key="MY API KEY", function(success) {
apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
})
.fail(function(err) {
alert("API Geolocation error! \n\n"+err);
});
};
var browserGeolocationSuccess = function(position) {
alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var browserGeolocationFail = function(error) {
switch (error.code) {
case error.TIMEOUT:
alert("Browser geolocation error !\n\nTimeout.");
break;
case error.PERMISSION_DENIED:
if(error.message.indexOf("Only secure origins are allowed") == 0) {
tryAPIGeolocation();
}
break;
case error.POSITION_UNAVAILABLE:
alert("Browser geolocation error !\n\nPosition unavailable.");
break;
}
};
var tryGeolocation = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
browserGeolocationSuccess,
browserGeolocationFail);
}
};
tryGeolocation();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
latt=pos.lat;
lngg=pos.lng;
console.log("point :"+latt+","+lngg);
}, function() {
});
} else {
// Browser doesn't support Geolocation
}
this.restapiService.getAllSearchResults()
.then(data => {
this.map = new google.maps.Map(this.mapElement.nativeElement, {
zoom: 100,
center: {lat: parseFloat(pos.lat), lng: parseFloat(pos.lng)}
});
var position = new google.maps.LatLng(parseFloat(pos.lat), parseFloat(pos.lng));
var sMarker = new google.maps.Marker({position: position,animation: google.maps.Animation.DROP, title: "My Location"});
sMarker.setMap(this.map);
});
}
您可以试试这个来获取当前位置[在 iOS(浏览器模拟器-ionic)上测试]
这是涉及 Permissions and Navigation 个 API 的示例
记住,它只有在主机使用HTTPS时才有效。
这可能是由于您对以下答案的评论而导致权限警报未显示的原因。
请记住,如果您的主机不使用 HTTPS,它将超时并出现有关身份验证失败的错误。使用错误回调来检查。
if ( navigator.permissions && navigator.permissions.query) {
//try permissions APIs first
navigator.permissions.query({ name: 'geolocation' }).then(function(result) {
// Will return ['granted', 'prompt', 'denied']
const permission = result.state;
if ( permission === 'granted' || permission === 'prompt' ) {
_onGetCurrentLocation();
}
});
} else if (navigator.geolocation) {
//then Navigation APIs
_onGetCurrentLocation();
}
function _onGetCurrentLocation () {
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition( function (position) {
//use coordinates
const marker = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
}, function (error) {
//error handler here
}, options)
}
我的 objective 是 return 我在 iOS 设备上使用 Safari 的地理定位坐标。在我的桌面浏览器上,我使用 Google 地图地理定位 API 键调用下面的函数 tryGeolocation()
。
这 在桌面 Chrome 上有效 ,但在 Safari(桌面和 iOS)和 Chrome (iOS) 中有效我 return 遇到了 "Position Unavailable"
错误。我尝试过 5 秒和 10 秒的超时,但同样的情况发生了。
在 iOS 中调用 Google 地图地理定位 API 是否有已知的解决方法?
var apiGeolocationSuccess = function(position) {
alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var tryAPIGeolocation = function() {
jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key="MY API KEY", function(success) {
apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
})
.fail(function(err) {
alert("API Geolocation error! \n\n"+err);
});
};
var browserGeolocationSuccess = function(position) {
alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var browserGeolocationFail = function(error) {
switch (error.code) {
case error.TIMEOUT:
alert("Browser geolocation error !\n\nTimeout.");
break;
case error.PERMISSION_DENIED:
if(error.message.indexOf("Only secure origins are allowed") == 0) {
tryAPIGeolocation();
}
break;
case error.POSITION_UNAVAILABLE:
alert("Browser geolocation error !\n\nPosition unavailable.");
break;
}
};
var tryGeolocation = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
browserGeolocationSuccess,
browserGeolocationFail,
{maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
}
};
tryGeolocation();
我刚开始使用 iOS 浏览器,如有任何建议,我们将不胜感激!!
尝试不使用 getCurrentPosition() 的第三个参数,因为我们在 error.POSITION_UNAVAILABLE 上遇到问题。
var apiGeolocationSuccess = function(position) {
alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var tryAPIGeolocation = function() {
jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key="MY API KEY", function(success) {
apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
})
.fail(function(err) {
alert("API Geolocation error! \n\n"+err);
});
};
var browserGeolocationSuccess = function(position) {
alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var browserGeolocationFail = function(error) {
switch (error.code) {
case error.TIMEOUT:
alert("Browser geolocation error !\n\nTimeout.");
break;
case error.PERMISSION_DENIED:
if(error.message.indexOf("Only secure origins are allowed") == 0) {
tryAPIGeolocation();
}
break;
case error.POSITION_UNAVAILABLE:
alert("Browser geolocation error !\n\nPosition unavailable.");
break;
}
};
var tryGeolocation = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
browserGeolocationSuccess,
browserGeolocationFail);
}
};
tryGeolocation();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
latt=pos.lat;
lngg=pos.lng;
console.log("point :"+latt+","+lngg);
}, function() {
});
} else {
// Browser doesn't support Geolocation
}
this.restapiService.getAllSearchResults()
.then(data => {
this.map = new google.maps.Map(this.mapElement.nativeElement, {
zoom: 100,
center: {lat: parseFloat(pos.lat), lng: parseFloat(pos.lng)}
});
var position = new google.maps.LatLng(parseFloat(pos.lat), parseFloat(pos.lng));
var sMarker = new google.maps.Marker({position: position,animation: google.maps.Animation.DROP, title: "My Location"});
sMarker.setMap(this.map);
});
}
您可以试试这个来获取当前位置[在 iOS(浏览器模拟器-ionic)上测试]
这是涉及 Permissions and Navigation 个 API 的示例
记住,它只有在主机使用HTTPS时才有效。 这可能是由于您对以下答案的评论而导致权限警报未显示的原因。
请记住,如果您的主机不使用 HTTPS,它将超时并出现有关身份验证失败的错误。使用错误回调来检查。
if ( navigator.permissions && navigator.permissions.query) {
//try permissions APIs first
navigator.permissions.query({ name: 'geolocation' }).then(function(result) {
// Will return ['granted', 'prompt', 'denied']
const permission = result.state;
if ( permission === 'granted' || permission === 'prompt' ) {
_onGetCurrentLocation();
}
});
} else if (navigator.geolocation) {
//then Navigation APIs
_onGetCurrentLocation();
}
function _onGetCurrentLocation () {
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition( function (position) {
//use coordinates
const marker = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
}, function (error) {
//error handler here
}, options)
}