2016-12-27 305 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>