简体   繁体   English

Google地图不显示? Cordova插件

[英]Google map not displaying? Cordova plugin

I have followed the following link instructions just test that a map is appearing. 我按照以下链接说明进行操作,只是测试是否正在显示地图。 So I got to stage 4 where the map should be displayed, however I am not getting anything. 因此,我进入了应该显示地图的第四阶段,但是我什么也没得到。 ( http://www.christianengvall.se/phonegap-and-google-maps/ ) http://www.christianengvall.se/phonegap-and-google-maps/

I have added all the references for the JS and CSS files, as well as installing the google map plugin. 我已经添加了JS和CSS文件的所有引用,以及安装了Google Map插件。

I changed the function ondeviceready as I am already using this function for other code, so instead the map should initialize when the submit button is clicked. 我已经在其他设备上使用了此功能,因此我已在ondeviceready上更改了该功能,因此应在单击“提交”按钮时初始化地图。 The reason for this is because I keep all my JavaScript outside the HTML page and on a specific JS file. 这样做的原因是因为我将所有JavaScript都保留在HTML页面之外,并且保留在特定的JS文件中。

So at the moment, when i clicked the button, nothing is happening. 因此,此刻,当我单击按钮时,没有任何反应。 Can anyone see where I am going wrong? 谁能看到我要去哪里错了?

HTML: HTML:

<div data-role="page" id="map" data-theme="d">
<div data-role="content">
    <input type="submit" value="View Map" onclick="return mapAPI()">
<div id="map_canvas">
</div>
    </div>

mapAPI function: mapAPI函数:

function mapAPI(){
var map = new GoogleMap();
map.initialize();

} }

googlemap.js: googlemap.js:

function GoogleMap(){
  this.initialize = function(){
   var map = showMap();
 }
  var showMap = function(){
  var mapOptions = {
   zoom: 4,
   center: new google.maps.LatLng(-33, 151),
  mapTypeId: google.maps.MapTypeId.ROADMAP
   }
 var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
return map;
}

} }

Attach an event listener to the button instead. 而是事件侦听器附加到按钮。

<input type="submit" id="viewMap" value="View Map">

<script>
    google.maps.event.addDomListener(document.getElementById('viewMap'), 'click', mapAPI);
</script>

Hi Are you whitelisting the URL in the Head section of the index.html. 嗨,您是否将index.html的Head部分中的URL列入白名单。 If not , this is how you do it . 如果不是,这就是您的做法。

  <meta charset="utf-8" />

  <meta http-equiv="Content-Security-Policy" content="script-src 'self' https://maps.googleapis.com/ https://maps.gstatic.com/ https://mts0.googleapis.com/ 'unsafe-inline' 'unsafe-eval'">

A simple way to show googlemaps on the mobile using Cordova 使用Cordova在移动设备上显示googlemaps的简单方法

<script type="text/javascript">
    function initialize() {
      var mapOptions = {
        zoom: 14,
        center: new google.maps.LatLng(-33, 151),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById('map'), mapOptions);
    }



</script>

You need API KEY for Googlemaps to work , Generate one and put the reference head section 您需要API KEY才能使Googlemaps正常工作,生成一个并放置参考标头部分

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM