我试过下面的代码,它的工作为chrom,firefox浏览器,但不能在桌面上的safari浏览器上工作(有线连接),也不能在windows手机上工作(在诺基亚lumia 625上测试),但在我的ipad,iphone上工作(通过wifi)用于safari浏览器 请给我一个解决这个问题的方法。
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"> </script>
在我的页面调用函数中加载页面。
$(document).ready(function() {
requestPosition();
});
在js文件中写入函数
function requestPosition() {
if (typeof navigator.geolocation == "undefined") {
alert("Your browser doesn't support the Geolocation API");
return;
}
navigator.geolocation.getCurrentPosition(function(position){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"location": new google.maps.LatLng(position.coords.latitude,position.coords.longitude)
},
function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
j = 0;
for (var i = 0; i < results[j].address_components.length; i++) {
if (results[j].address_components[i].types[0] == "locality") {
//this is the object you are looking for
city = results[j].address_components[i];
}
if (results[j].address_components[i].types[0] == "administrative_area_level_1") {
//this is the object you are looking for
region = results[j].address_components[i];
}
if (results[j].address_components[i].types[0] == "country") {
//this is the object you are looking for
country = results[j].address_components[i];
}
}
city=city.long_name;
state=region.long_name;
country=country.long_name;
$("#_state").val(state);
$("#_country").val(country);
$("#_city").val(city);
}
else
alert("Unable to retrieve your address");
});
},
function (positionError) {
alert("Error: " + positionError.message);
},
{
enableHighAccuracy: true,
timeout: 10 * 1000 // 10 seconds
}
}
答案 0 :(得分:0)
Safari地理位置仅在与wifi连接时才有效。当使用有线连接连接时,Safari会从地理定位函数调用错误回调。