网页浏览器中的位置检测 [英] location detection in web browser
问题描述
我正在寻找一种方法在访问我的网站时查找用户位置,我试过Maxmind,但他们似乎在城市级别上不准确。
我想要的信息是(国家,城市,经度,纬度)我希望这个国家和城市如果可能的话非常准确。
我也有使用HTML5,但它问我的用户分享位置信息,这似乎对我来说是不好的解决方案。 (同样我得到了非常准确的结果)
任何解决方案?
注意:我发现谷歌搜索没有请求分享我的位置,但我没有找到任何api使用谷歌服务 解决方案 div> 这是我为使用Google地图的地理位置教程找到的代码。希望这有用。 这适用于您连接到网络。
将会更准确。
HTML5地理定位示例
<!DOCTYPE html>
< html>
< head>
< title> GeoLocation Demo< / title>
< meta charset =utf-8/>
< script src =http://maps.google.com/maps/api/js?sensor=falsetype =text / javascript>< / script>
< script>
var startPos;
var map;
函数init(){
if(navigator.geolocation){
document.getElementById(support)。innerHTML =< p style ='color:green' >太棒了!此浏览器支持HTML5地理位置< / p>;
navigator.geolocation.getCurrentPosition(updateLocation,handleLocationError,{
timeout:50000
});
//navigator.geolocation.watchPosition (updateCurrPosition,handleLocationError);
} else {
document.getElementById(support)。innerHTML =< p style ='color:red'>糟糕!此浏览器不支持HTML5地理位置< / p>中;
}
}
函数updateLocation(position){
startPos = position;
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
//document.getElementById(\"startLat\").innerHTML = latitude;
//document.getElementById(\"startLon\").innerHTML = longitude;
var coords = new google.maps.LatLng(latitude,longitude);
$ b $ var mapOptions = {
zoom:10,
center:coords,
mapTypeControl:false,
navigationControlOptions:{
style:google .maps.NavigationControlStyle.SMALL
},
mapTypeId:google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById(map_canvas),mapOptions);
var marker = new google.maps.Marker({
position:coords,
map:map,
title:Your current location!
});
$ b函数handleLocationError(error){
switch(error.code){
case 0:
updateStatus(There was检索您的位置时出错:+ error.message);
休息;
情况1:
updateStatus(用户阻止此页面检索位置。);
休息;
案例2:
updateStatus(浏览器无法确定您的位置:+ error.message);
break;
案例3:
updateStatus(浏览器在检索位置前超时);
break;
}
}
函数updateStatus(msg){
document.getElementById(divStatus)。innerHTML = msg;
}
< / script>
< / head>
< body onload =init()>
< div id =support>< / div>
< div id =divStatus>< / div>
< div id =map_canvasstyle =width:600px; height:400px;>< / div>
< / body>
< / html>
I am searching for a method to locate the user location while visiting my website, i have tried Maxmind but they seems to be inaccurate in the city level. the information i want is (Country, City, longitude, latitude) i want the country and the city to be very accurate if possible.
I have also used HTML5 but the problem that it asks my user to share location information which seems to me bad solution. (althougth i got very accurate results)
Any solutions?
Notice: i found google search got an accurate detection and without "ask for sharing my location", but i didn't find any api to use google service
This is a code i found for a tutorial for Geo Location using Google maps.. Hope this is useful.
This works on the way you connect to the network.
- if you use a Boradband ISP what gives you an fixed ip your location will be more accurate.
- if you use mobile device to connect to internet, the Location of the nearest Mobile tower from which your network gets the signal will be shown
Example for HTML5 Geolocation
<!DOCTYPE html>
<html>
<head>
<title>GeoLocation Demo</title>
<meta charset="utf-8"/>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script>
var startPos;
var map;
function init() {
if (navigator.geolocation) {
document.getElementById("support").innerHTML = "<p style='color:green'>Great! This browser supports HTML5 Geolocation</p>";
navigator.geolocation.getCurrentPosition(updateLocation, handleLocationError, {
timeout: 50000
});
//navigator.geolocation.watchPosition(updateCurrPosition,handleLocationError);
} else {
document.getElementById("support").innerHTML = "<p style='color:red'>Oops! This browser does not support HTML5 Geolocation</p>";
}
}
function updateLocation(position) {
startPos = position;
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
//document.getElementById("startLat").innerHTML = latitude;
//document.getElementById("startLon").innerHTML = longitude;
var coords = new google.maps.LatLng(latitude, longitude);
var mapOptions = {
zoom: 10,
center: coords,
mapTypeControl: false,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var marker = new google.maps.Marker({
position: coords,
map: map,
title: "Your current location!"
});
}
function handleLocationError(error) {
switch (error.code) {
case 0:
updateStatus("There was an error while retrieving your location: " + error.message);
break;
case 1:
updateStatus("The user prevented this page from retrieving the location.");
break;
case 2:
updateStatus("The browser was unable to determine your location: " + error.message);
break;
case 3:
updateStatus("The browser timed out before retrieving the location.");
break;
}
}
function updateStatus(msg) {
document.getElementById("divStatus").innerHTML = msg;
}
</script>
</head>
<body onload="init()">
<div id="support"></div>
<div id="divStatus"></div>
<div id="map_canvas" style="width:600px;height:400px;"></div>
</body>
</html>
这篇关于网页浏览器中的位置检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!