2011-09-24 114 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扩展,并从任何浏览器浏览它的系统或移动,它将显示用户当前的位置。

相关问题