【发布时间】:2013-12-22 16:00:21
【问题描述】:
我正在尝试使用以下代码来获取用户的位置。它说,它适用于浏览器,但适用于 Windows 8 应用程序编译,
Java 运行时错误,“Google”未定义!
有人建议我加载 Maps Api。我怎么做?请正确的代码和答案。如果满意,我一定会投票给你。
<!Doctype html>
<html>
<head>
<title>Get Location</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head>
<body>
<p id="locResults">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<script>
function getLocation(){
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{document.getElementById("locResults").innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
document.getElementById("locResults").innerHTML="Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
document.getElementById("locResults").innerHTML="Getting Your Location...";
getReverseGeocodingData(position.coords.latitude, position.coords.longitude);
}
function getReverseGeocodingData(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
// This is making the Geocode request
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status !== google.maps.GeocoderStatus.OK) {
document.getElementById("locResults").innerHTML = status;
}
// This is checking to see if the Geoeode Status is OK before proceeding
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
var address = (results[0].formatted_address);
document.getElementById("locResults").innerHTML = address;
}
});
}
</script>
</body>
</html>
【问题讨论】:
标签: javascript google-maps windows-8 google-api