【问题标题】:Exception: Google is not defined [Cordova geolocation]例外:未定义 Google [Cordova 地理位置]
【发布时间】:2017-09-07 04:39:34
【问题描述】:

我正在尝试制作一个简单的应用程序,以显示我在谷歌地图 API 上的位置。
我正在使用 Cordova。
我为浏览器构建它,并且工作正常,但是当我为 android 或 windows 构建它时,它构建但是当我运行它时,应用程序打开但无法工作。

它向我显示一条警告说 Google 未定义。
看附图:Warning error!

这是我的 JS 代码:

var 应用程序 = {

initialize: function() {
    this.bindEvents();
},

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {
    navigator.geolocation.getCurrentPosition(app.onSuccess, app.onError);
},

onSuccess: function(position){

    var longitude = position.coords.longitude;
    var latitude = position.coords.latitude;
    var latLong = new google.maps.LatLng(latitude, longitude);

    var mapOptions = {
        center: latLong,
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map"), mapOptions);

    var marker = new google.maps.Marker({
          position: latLong,
          map: map,
          title: 'my location'
      });
},

onError: function(error){
    alert("cod erro " + error.code + ". \n" + 
        "mensagem: " + error.message);
}

};

【问题讨论】:

    标签: javascript html api cordova google-maps


    【解决方案1】:

    将此脚本放在 HTML 文档的末尾(将 YOUR_API_KEY 替换为您的实际 API 密钥

    <script async defer src="https://maps.googleapis.com/maps/api/js key=YOUR_API_KEY&callback=initMap">
    </script>
    

    在此脚本中,回调称为 initMap()。它看起来像这样:

    function initMap() {
    
        var mapOptions = {
            center: {lat: -34.397, lng: 150.644},
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    
        map = new google.maps.Map(document.getElementById("map"), mapOptions);
    
        google.maps.event.addListenerOnce(map, 'tilesloaded', function(){ 
             getGeoLocation();
         });
    }
    
     // Gets user location
    function getGeoLocation() {
    
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
    
                // Gets lat & lng and makes a LatLng object
                var longitude = position.coords.longitude;
                var latitude = position.coords.latitude;
                var latLong = new google.maps.LatLng(latitude, longitude);
    
                // Places a marker on the map
                var marker = new google.maps.Marker({
                    position: latLong,
                    map: map,
                    title: 'my location'
                });
    
                // Centers map on user location
                map.setCenter({lat:latitude, lng:longitude});
    
            }, geoLocationError, {maximumAge:600000, timeout:10000, enableHighAccuracy: true});
        }
    }
    

    【讨论】:

    • 我试过没有成功。请看我的js代码。我用我的代码编辑了帖子,请看一下,谢谢
    • @tuga49 您应该从您的 initMap 回调而不是 onDeviceReady 中获取用户的位置。当 cordova 准备好与本机设备 API(如相机)通信时,将触发 deviceReady 状态。再次查看我的代码,发现在我的 initMap 回调中创建地图后,我获得了用户的位置,该回调是脚本标签的一部分,用于从他们的服务器检索谷歌地图。您可以在脚本末尾看到“callback=initMap”。一旦我从 Google 检索到所有初始地图数据,它就会触发。
    • 嗨。我解决不了。您可以编辑我的代码并将正确的代码发送给我吗?不知道怎么办,谢谢
    • @tuga49 请更新您的问题并添加您的 initMap 函数
    • @tuga49 我更新了我的答案。从您的 onDeviceReady 中删除 navigator.geolocation.getCurrentPosition(app.onSuccess, app.onError) 并添加我在上面放置的代码。它会起作用的。
    猜你喜欢
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2015-09-05
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多