iOS Safari上javascript中的地理位置
我的目标是在iOS设备上使用Safari返回我的地理位置坐标。在我的桌面浏览器上,我使用Google Maps Geolocation API键调用下面的函数iOS Safari上javascript中的地理位置,javascript,ios,mobile,safari,geolocation,Javascript,Ios,Mobile,Safari,Geolocation,我的目标是在iOS设备上使用Safari返回我的地理位置坐标。在我的桌面浏览器上,我使用Google Maps Geolocation API键调用下面的函数tryGeolocation() 这适用于桌面Chrome,但在Safari(桌面和iOS)和Chrome(iOS)中,我返回了“位置不可用”错误。我尝试了5秒和10秒的超时,但同样的情况也发生了 在iOS中调用谷歌地图地理定位API是否有已知的解决方法 var apiGeolocationSuccess = function(positi
tryGeolocation()
这适用于桌面Chrome,但在Safari(桌面和iOS)和Chrome(iOS)中,我返回了“位置不可用”
错误。我尝试了5秒和10秒的超时,但同样的情况也发生了
在iOS中调用谷歌地图地理定位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\u不可用的问题
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();
您可以尝试此操作获取当前位置[在iOS(浏览器模拟器)上测试]以下是涉及到的示例和API
请记住,只有当主机使用HTTPS时,它才起作用。
这可能是由于您对下面的答案发表评论而导致权限警报未显示的原因
请记住,如果您的主机不使用HTTPS,它将超时以显示身份验证失败的错误。使用错误回调来检查这一点
if(navigator.permissions&&navigator.permissions.query){
//请先尝试权限API
navigator.permissions.query({name:'geolocation'})。然后(函数(结果){
//将返回[“已授予”、“提示”、“拒绝”]
const permission=result.state;
如果(权限==‘已授予’| |权限===‘提示’){
_onGetCurrentLocation();
}
});
}else if(navigator.geolocation){
//然后是导航API
_onGetCurrentLocation();
}
函数_onGetCurrentLocation(){
常量选项={
EnableHighAccurance:正确,
超时:5000,
最大值:0
};
navigator.geolocation.getCurrentPosition(函数(位置){
//使用坐标
常量标记={
纬度:位置坐标纬度,
lng:position.coords.longitude
};
},函数(错误){
//这里的错误处理程序
},选项)
}
@黑暗面当然谢谢,但这并不能解决问题。我试图在Safari中获得地理位置
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);
});
}