我正在尝试以下代码来获取用户的位置。它适用于浏览器,但在Windows 8 App编译中,它说,
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>
答案 0 :(得分:0)
您忘了拨打Google地图:
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(16.4706001, -33.6728973),
zoom:3,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);