2016-12-27 148 views
-1

我試圖獲得與谷歌地圖API的地理位置,但未能 整個問題來了,是Error.POSITION_UNAVAILABLE:HTML5地理位置locationError.POSITION_UNAVAILABLE:

在這裏不用我的代碼

是什麼原因造成這個錯誤發生我在哪裏可以開始研究解決問題的方法?

function findYou() { 
 
    if (!navigator.geolocation.getCurrentPosition(showPosition, 
 
    noLocation, { 
 
     maximumAge: 1200000, 
 
     timeout: 30000 
 
    })) { 
 
    document.getElementById("lat").innerHTML = 
 
     "This browser does not support geolocation."; 
 
    } 
 
} 
 

 
function showPosition(location) { 
 
    var latitude = location.coords.latitude; 
 
    var longitude = location.coords.longitude; 
 
    var accuracy = location.coords.accuracy; 
 
    // 創建地圖 
 
    var position = new google.maps.LatLng(latitude, longitude); 
 
    // 創建地圖選項 
 
    var myOptions = { 
 
    zoom: 18, 
 
    center: position, 
 
    mapTypeId: google.maps.MapTypeId.HYBRID 
 
    }; 
 
    // 顯示地圖 
 
    var map = new google.maps.Map(document.getElementById("map"), 
 
    myOptions); 
 
    document.getElementById("lat").innerHTML = 
 
    "Your latitude is " + latitude; 
 
    document.getElementById("lon").innerHTML = 
 
    "Your longitude is " + longitude; 
 
    document.getElementById("acc").innerHTML = 
 
    "Accurate within " + accuracy + " meters"; 
 
} 
 

 
function noLocation(locationError) { 
 
    var errorMessage = document.getElementById("lat"); 
 
    switch (locationError.code) { 
 
    case locationError.PERMISSION_DENIED: 
 
     errorMessage.innerHTML = 
 
     "You have denied my request for your location."; 
 
     break; 
 
    case locationError.POSITION_UNAVAILABLE: 
 
     errorMessage.innerHTML = 
 
     "Your position is not available at this time."; 
 
     break; 
 
    case locationError.TIMEOUT: 
 
     errorMessage.innerHTML = 
 
     "My request for your location took too long."; 
 
     break; 
 
    default: 
 
     errorMessage.innerHTML = 
 
     "An unexpected error occurred."; 
 
    } 
 
} 
 
findYou();
#map { 
 
    width: 500px; 
 
    height: 500px; 
 
}
<!doctype html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <title>HTML5 Geolocation</title> 
 
    <style> 
 
    </style> 
 
    <!--set the api --> 
 
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"> 
 
    </script> 
 

 
</head> 
 

 
<body> 
 
    <h1>Your Location</h1> 
 

 
    <p id="lat">&nbsp;</p> 
 
    <p id="lon">&nbsp;</p> 
 
    <p id="acc">&nbsp;</p> 
 
    <div id="map"> 
 
    </div> 
 
</body> 
 

 
</html>

+0

您可以使用,你的答案寫插入編輯器上方的插入片段工具按鈕HTML/CSS/JS片段會自動突出顯示和格式化(至少讓我們更容易閱讀) – mike510a

+0

發佈的代碼適用於我:[小提琴](https://jsfiddle.net/v7a7ejL6/) – geocodezip

回答

0

嘗試更換你的腳本參考這個在HTML頁面中,

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBu-916DdpKAjTmJNIgngS6HL_kDIKU0aU&callback=myMap"></script>