2012-07-04 61 views
0

我使用此代码,以获得准确的用户的位置...如何获取用户当前位置的最大精度

watchID=navigator.geolocation.watchPosition(handleGetCurrentPosition, handleGetCurrentPositionError, {enableHighAccuracy:true}); 

function handleGetCurrentPosition(location) 
{ 
    document.getElementById("lat").value = location.coords.latitude; 
    document.getElementById("lon").value = location.coords.longitude; 
    document.getElementById("accu").value =location.coords.accuracy; 
    document.forms["myform"].submit(); 
} 

function handleGetCurrentPositionError(){ 
    alert("Location could not be found.") 
} 

BT的结果我得到是不准确的。 我使用的代码是否存在任何问题?

+0

你对“准确”的定义是什么? – acme

回答

1

精度取决于能力您的Geolocation设备。 navigator.geolocation仅适用于使用该设备的API。如果该设备不支持高精度,则无法对其执行任何操作。

0

我发现这个有用的链接在:http://www.geoplugin.com,提供了一个非常好的位置过滤API,并且在预测用户位置时具有非常可怕的精确度。

下面是如何被使用的例子:http://www.jquery4u.com/api-calls/geo-location-2-lines-javascript/#.T_QZrxdo3IU

+0

我只想要经度和纬度.. – user1482852

+0

它确实提供了包括Lat和Long在内的所有位置属性。请检查我提供的示例,它包含所有必要的方法,以便您准确获取所需的信息。 –

1

这下面的代码,以获得准确的位置。我已经用HTML 5写了这段代码。

  1. 在html文档中创建一个地图占位符。

<输出ID = “mapToYou”>搜索您的当前位置... < /输出>

  1. 调用此JS脚本在页面加载。
 

    window.addEventListener("load", function() { 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(success, error); 
     } else { 
      error('not supported'); 
     } 
    },false); 

    function success(position) { 
     var mapToYou = document.querySelector('#mapToYou'); 
     alert("Gotcha!"); 
     var latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); 
     var myOptions = { 
        zoom: 15, 
        center: latlng, 
      mapTypeControl: false, 
      navigationControlOptions: { 
       style: google.maps.NavigationControlStyle.SMALL 
      }, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     var googleMap = new google.maps.Map(mapToYou, myOptions); 
     var marker = new google.maps.Marker({ 
      position: latlng, 
      map: googleMap, 
      title:"You are here!" 
     }); 
    } 

    function error(msg) { 
     var mapToYou = document.querySelector('#mapToYou'); 
     mapToYou.innerHTML = typeof msg == 'string' ? msg : "failed"; 
     mapToYou.className = 'fail'; 

     // console.log(arguments); 
    } 

相关问题