2017-07-26 76 views
0

这是我的代码减去我的API密钥来获得用户的当前位置和它的作品准确地在Chrome和Firefox,但不是在边缘:谷歌地图API和提高精度MS-边缘

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> 
 
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=[my api key]=3.exp"></script> 
 
<script type="text/javascript"> 
 
function getMyLocation(){ 
 
\t /* Chrome need SSL! */ 
 
\t var is_chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); 
 
\t var is_ssl = 'https:' == document.location.protocol; 
 
\t if(is_chrome && ! is_ssl){ 
 
\t \t return false; 
 
\t } 
 
\t /* HTML5 Geolocation */ 
 
\t navigator.geolocation.getCurrentPosition(
 
\t \t function(position){ // success cb 
 
\t \t \t /* Current Coordinate */ 
 
\t \t \t var lat = position.coords.latitude; 
 
\t \t \t var lng = position.coords.longitude; 
 
\t \t \t var google_map_pos = new google.maps.LatLng(lat, lng); 
 
\t \t \t /* Use Geocoder to get address */ 
 
\t \t \t var google_maps_geocoder = new google.maps.Geocoder(); 
 
\t \t \t google_maps_geocoder.geocode(
 
\t \t \t \t { 'latLng': google_map_pos }, 
 
\t \t \t \t function(results, status) { 
 
\t \t \t \t \t if (status == google.maps.GeocoderStatus.OK && results[0]) { 
 
\t \t \t \t \t \t document.getElementById("myplace").innerHTML = results[0].formatted_address; 
 
\t \t \t \t \t \t var str = results[0].formatted_address; 
 
\t \t \t \t \t \t var res = str.split(", "); 
 
\t \t \t \t \t \t /*Street*/ 
 
\t \t \t \t \t \t document.getElementById('input_14_10').value = res[0]; 
 
\t \t \t \t \t \t /*City*/ 
 
\t \t \t \t \t \t document.getElementById('input_14_12').value = res[1]; 
 
\t \t \t \t \t \t /*State*/ 
 
\t \t \t \t \t \t document.getElementById('input_14_13').value = res[2].substr(0,2); 
 
\t \t \t \t \t \t /*Zip*/ 
 
\t \t \t \t \t \t document.getElementById('input_14_15').value = res[2].substr(3); 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t); 
 
\t \t }, 
 
\t \t function(){ // fail cb 
 
\t \t } 
 
\t); 
 
}; 
 
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
 
<head> 
 
\t <title>title</title> 
 
\t <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
\t <meta name="description" content="" /> 
 
\t <meta name="keywords" content="" /> 
 
\t <meta name="robots" content="index,follow" /> 
 
\t <link rel="stylesheet" type="text/css" href="styles.css" /> 
 
</head> 
 
<body> 
 
<input type="button" id='calcmiles' onclick='getMyLocation()' value='Push to Get My Location' ><br> 
 
<font style="font-size: 200% !important"><b><a id='myplace'></a></b></font> 
 
    <input type="hidden" class="field" id="street_number" disabled="true"></input> 
 
    <input type="hidden" class="field" id="route" disabled="true"></input> 
 
    <input type="hidden" class="field" id="country" disabled="true"></input> 
 
<table id="address"> 
 
    <tr> 
 
     <td class="label">Street address</td> 
 
     <td class="wideField" colspan="2"><input class="field" id="input_14_10" disabled="true"></input></td> 
 
     </tr> 
 
     <tr> 
 
     <td class="label">City</td> 
 
     <td class="wideField" colspan="3"><input class="field" id="input_14_12" disabled="true"></input></td> 
 
     </tr> 
 
     <tr> 
 
     <td class="label">State</td> 
 
     <td class="slimField"><input class="field" id="input_14_13" disabled="true"></input></td> 
 
     <td class="label">Zip code</td> 
 
     <td class="wideField"><input class="field" id="input_14_15" disabled="true"></input></td> 
 
     <tr> 
 
     <td class="label">Phone Number</td> 
 
     <td class="wideField"><input class="field" id="phone"></input></td> 
 
     </tr> 
 
     </tr> 
 
</table> 
 
</body> 
 
</html>

在Chrome和Firefox中,它始终如一地正确表示用户的当前位置,但在边缘它由多个地址关闭。可以做些什么来提高Edge的准确性?

+0

确保您在使用Edge的Web应用程序时登录到您的Google帐户...如果地理位置查询来自匿名Google用户,则可能是Google地图api,这会降低准确性。使用浏览器的InPrivate和隐身版进行测试。 –

回答

0

你知道吗,如果你从Geolocation.getCurrentPosition()得到不同的纬度/经度,或者你从地理编码得到不同的结果?

如果您获得不同的经纬度,一种可能的解决方法是将可选PositionOptions对象传递给getCurrentPosition()PositionsOptions需要enableHighAccuracy的标志,这可能会从Edge获得更好的结果。

你把它作为像这样的最后一个参数:

var options = { 
    enableHighAccuracy: true, 
    timeout: 5000, 
    maximumAge: 0 
}; 

navigator.geolocation.getCurrentPosition(
    function(position){ // success cb); 
    // ...your function 
    }, 
    function(){ } // fail cb, 
    options 
) 

不知道这是否是问题,但值得一试。