2011-09-24 80 views
3

我需要的是經/緯客戶端(通過瀏覽)如何從瀏覽器獲取客戶端的位置?

發現在網絡上的一些文章中,發現了一些在堆棧溢出本身(老款)Get GPS location from the web browser。它幾乎在18個月前被回答 - 想知道是否有任何其他(更有效的)方式從瀏覽器獲取用戶位置的信息。

洙到目前爲止,使用找到2

的MaxMind

其他,使用谷歌的API

w3c的geo api會有向下兼容性問題:http://dev.w3.org/geo/api/spec-source.html,所以不要選擇那個。

發現了一個網站,www.drumaroo.com - 對於位置的要求時,我們落入類似的site.Needed東西要共享的第一時間

+0

W3C的GeoLocaiton API是爲了做到這一點,而不依賴於基於IP的位置數據庫(如谷歌的)的唯一途徑。您舉例的網站使用這兩種API。 –

+0

看到這個問題: http://stackoverflow.com/questions/3940767/how-to-get-client-location-using-google-maps-api-v3 – mre666

回答

3

用戶可以位於通過以下方式: -

  1. 使用IP地址。 由於網絡地址/ 拓撲的不確定性,最容易實現但最不可靠。
  2. 使用緯度,經度對 最準確的解決方案,但大多數最終用戶不知道他們的緯度, 經度值。
  3. 使用郵編 很好思考,但很難實現,雖然ZipCodes被用於
    相當長的時間,但這還不是一個標準呢。 (見鏈接
    http://en.wikipedia.org/wiki/Postal_code)。 ZipCode單獨不足以計算距離,我們需要將其轉換爲經度,緯度,這是一個開銷的
  4. 使用用戶的地址 最引人注目的思想,因爲每個用戶至少有一個地理地址。 地址本身並不足以計算距離,我們需要將它轉換爲經度,緯度,這是一個開銷。

最好的選擇將是使用選項2,3.4在一起,你可以做,在兩個步驟: -

  1. 首先確定經度,緯度從地址。
  2. 然後使用此lat,long值進行進一步處理,即存儲在 數據庫等中

有關此可用的各種選項看到的this question

+0

請參閱http://stackoverflow.com的答案/ questions/8800267/get-longitude-latitude-combination-based-distance-and-digree/8800334#8800334代碼.... – aProgrammer

1

答案嘗試HTML5地理位置

function init() { 


var mapOptions = { 
    zoom: 8, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 


map = new google.maps.Map(document.getElementById('map-canvas'), 
    mapOptions); 



if(navigator.geolocation) { 
navigator.geolocation.getCurrentPosition(function(position) { 
    var pos = new google.maps.LatLng(position.coords.latitude, 
            position.coords.longitude); 

    var infowindow = new google.maps.InfoWindow({ 
    map: map, 
    position: pos, 
    content: 'Location found using HTML5.' 
    }); 

    map.setCenter(pos); 
} 

} else { 
alert('Geolocation not detected'); 
} 
} 
0

。只需點擊javascript + html並將代碼複製到html文件中即可。

0

<!DOCTYPE html> 
 
<html> 
 
<head> 
 

 
<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) { 
 
    var location_timeout = setTimeout("geolocFail()", 10000); 
 

 
    navigator.geolocation.getCurrentPosition(function(position) { 
 
     clearTimeout(location_timeout); 
 

 
     var lat = position.coords.latitude; 
 
     var lng = position.coords.longitude; 
 

 
     geocodeLatLng(lat, lng); 
 
    }, function(error) { 
 
      alert("inside error "); 
 
     clearTimeout(location_timeout); 
 
     geolocFail(); 
 
    }); 
 
} else { 
 
     alert("Turn on the location service to make the deposit"); 
 
    // Fallback for no geolocation 
 
    geolocFail(); 
 
} 
 

 
function geolocFail(){ 
 
alert("Turn on the location service to make the deposit"); 
 
document.write("Turn on the location service to make the deposit"); 
 
} 
 

 
/*if (navigator.geolocation) { 
 
navigator.geolocation.getCurrentPosition(successFunction, errorFunction); 
 
} 
 
//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 geocodeLatLng(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 
 
var add= results[0].formatted_address 
 
alert(add); 
 

 
//city data 
 
//alert(city.short_name + " " + city.long_name) 
 

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

+0

Couls你爲OP添加一些解釋關於這段代碼的作用?謝謝 – Paco

0

這是HTML代碼來跟蹤設備(瀏覽器/手機)的位置。只是上面的文件保存爲html擴展,並從任何瀏覽器瀏覽它的系統或移動,它將顯示用戶當前的位置。

相關問題