the*_*ide 5 javascript safari mobile geolocation ios
我的目标是在 iOS 设备上使用 Safari 返回我的地理位置坐标。在我的桌面浏览器上,我tryGeolocation()使用 Google Maps Geolocation API 密钥调用下面的函数 。
这适用于桌面 Chrome,但在 Safari(桌面和 iOS)和 Chrome(iOS)中,我返回了"Position Unavailable"错误。我尝试过 5 秒和 10 秒的超时,但同样发生了。
在 iOS 中调用 Google Maps Geolocation 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();
Run Code Online (Sandbox Code Playgroud)
我是 iOS 浏览器的新手,所以任何建议将不胜感激!
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);
});
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试此方法来获取当前位置[在iOS(浏览器模拟器-ionic)上测试]
| 归档时间: |
|
| 查看次数: |
10126 次 |
| 最近记录: |