【问题标题】:Jquery code doesn't work in jQuery mobile projectjQuery 代码在 jQuery 移动项目中不起作用
【发布时间】:2016-03-18 13:41:37
【问题描述】:

我正在使用 jQuery Mobile 库与 Cordova 开发一个混合应用程序。 因为我需要显示一张地图,所以我使用并编辑了找到here 的代码,但它似乎无法识别代码的第一行。

地理位置.js

$(document).on("pagecreate", "#map-page", function() { //doesn't work
    alert('hello');

    function initMap() {
        var defaultLatLng = new google.maps.LatLng(44.494887, 11.342616300000032); //Bologna      
        drawMap(defaultLatLng);
    }

     function drawMap(latlng) {
         var myOptions = {
             zoom: 17,
             center: latlng,
         };

         var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);

         var marker = new google.maps.Marker({
             position: latlng,
             map: map,
             title: "Parcheggio"
         });
    }
});

HTML 容器与演示相同。我包含的脚本:

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="js/geolocation.js"></script>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD91dMkAWwADchy-WY1-5O7P6nHmjGont0
&callback=initMap"></script>

我也尝试过更改 API 密钥,但没有任何改变。 你知道如何解决这个问题吗?

【问题讨论】:

标签: jquery cordova jquery-mobile google-maps-api-3 maps


【解决方案1】:

您应该在document.ready 块中移动事件绑定。

$(document).ready(function(){
    $(document).on("pagecreate", "#map-page", function() {
      ....
   });
});

另外,在 jquery 包含之后包含 Geolocation.js。

【讨论】:

  • 谢谢,但还是不行。看起来它不读取以 $(document) 或 $ 开头的代码。
  • @MonicaKiur - 您在 jquery.js 之前包含 geolocation.js。需要更改顺序。请立即查看
  • 我更改了订单。现在它可以工作了,但它仍然没有加载地图
  • @MonicaKiur - 很高兴知道这一点。为此,我要求您发布另一个问题。
  • 当然。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多