2017-07-26 74 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 
) 

不知道這是否是問題,但值得一試。