2014-04-08 36 views
0

我已經嘗試下面的代碼它的鉻,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 
    } 
} 
+1

看看這個鏈接http://stackoverflow.com/questions/3791442/geolocation-in-safari-5 – comrade

回答

0

Safari的地理位置時,帶wifi的連接工作。當與有線連接連接時,Safari會從地理定位功能中調用錯誤回調。

+2

safari瀏覽器有線連接的解決方案是什麼?如何獲取登錄用戶的城市,州,時區? –

+2

對不起,直到日期我還沒有找到有線連接的解決方案!如果我找到我會讓你知道的事情。 –

相關問題