我已經嘗試下面的代碼它的鉻,Firefox瀏覽器的工作,但不能在桌面(有線連接)Safari瀏覽器上工作,也不能在Windows Phone(在諾基亞Lumia 625上測試)工作,但工作在我的iPad,iPhone wifi)用於Safari瀏覽器。
請給我一個解決這個問題的方法。如何在safari瀏覽器中獲取當前位置(城市,州,郵編)?
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"> </script>
在我的頁眉調用函數加載頁面。在js文件
$(document).ready(function() {
requestPosition();
});
寫函數只
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
}
}
看看這個鏈接http://stackoverflow.com/questions/3791442/geolocation-in-safari-5 – comrade