2013-08-16 41 views
1

從谷歌標準的地理位置代碼在移動瀏覽器工作(Android版Chrome瀏覽器,Android標準瀏覽器):谷歌地理位置doesn't工作

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
<title>Reverse Geocoding</title> 

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript"> 
    var geocoder; 

    if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction, {enableHighAccuracy: true}); 
} 
//Get the latitude and the longitude; 
function successFunction(position) { 
    var lat = position.coords.latitude; 
    var lng = position.coords.longitude; 
    codeLatLng(lat, lng) 
} 

function errorFunction(){ 
    alert("Geocoder failed"); 
} 

    function initialize() { 
    geocoder = new google.maps.Geocoder(); 



    } 

    function codeLatLng(lat, lng) { 

    var latlng = new google.maps.LatLng(lat, lng); 
    geocoder.geocode({'latLng': latlng}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     console.log(results) 
     if (results[1]) { 
     //formatted address 
     alert(results[2].address_components[0].long_name) 

     } else { 
      alert("No results found"); 
     } 
     } else { 
     alert("Geocoder failed due to: " + status); 
     } 
    }); 
    } 
</script> 
</head> 
<body onload="initialize()"> 

</body> 
</html> 

但在桌面瀏覽器正常工作...爲什麼?我確信代碼已經在幾天前工作了。我沒有改變任何東西。有可能谷歌已經改變了他們的代碼中的東西?

編輯:這對我來說很重要,以獲得與GPS,WLAN和位置!移動網絡

回答

1

嘗試將sensor參數更改爲true,如下所示。

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script> 

經過谷歌地圖API的Geolocation documentation

指定傳感器參數

使用谷歌地圖API的要求表明您 應用程序是否使用傳感器(例如GPS定位器)來確定 用戶的位置。這是對移動設備特別重要的。包括地圖API時JavaScript代碼,指示應用程序是否 使用的傳感器裝置 應用必須通過一個所需的傳感器參數向標籤 。

通過傳感器確定用戶位置的應用程序在加載Maps API JavaScript時必須通過 sensor = true。

希望你明白。

+0

是的,我明白了,現在它可以與GPS傳感器一起使用,並且只有當我打開GPS時。但必須有可能通過WLAN或移動信號獲取位置。 – m1crdy

+0

@ m1crdy地理位置完全取決於用戶。你無法強迫它,對。這就是爲什麼如果你在桌面瀏覽器中檢查,它會顯示一個共享當前位置的彈出窗口。而在移動中它使用GPS跟蹤器。無線或移動信號。希望你明白我的觀點。 – Praveen

+0

,但如果用戶使用移動信號或WiFi激活位置,則必須可以在沒有GPS的情況下找到用戶。例如。 Facebook應用程序完全是這樣! – m1crdy