【问题标题】:Google API working on Browser and not in Windows 8 appGoogle API 在浏览器上运行,而不是在 Windows 8 应用程序中运行
【发布时间】: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


【解决方案1】:

您忘记调用 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);

【讨论】:

  • 感谢您的回复,但仍然显示相同的错误消息,google 未定义。还有更多提示吗?
  • 好吧,如果您的代码中没有 API 密钥,但这是有道理的。另一件事是您需要检查订单并让浏览器在使用其 API 的其他对象之前知道什么是“google”。就是这样。
猜你喜欢
  • 1970-01-01
  • 2016-08-16
  • 2012-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
相关资源
最近更新 更多