离子-谷歌地图不显示(或显示为灰色)

4
我在使用Ionic框架时,与谷歌地图的兼容性出现了很多问题。我遵循了这篇指南上的步骤,在index.html中成功地显示了谷歌地图。但当我尝试添加新页面到应用程序并在它们之间切换时,地图不会正确显示,除非你刷新页面。
我制作了三个注入到index.html中的页面:主页、中介和办公室。主页应该显示谷歌地图并链接到中介。中介只是一个空页面,链接到主页和办公室。办公室也应该显示地图,并返回到主页。
这是我得到的结果:
  • 主页 -> 中介 -> 办公室 - 地图在办公室页面上未能成功展示
  • 主页 -> 中介 -> 办公室 -> 主页 - 地图变成灰色
为什么会发生这种情况呢?我尝试用以下代码包装谷歌地图功能: google.maps.event.addDomListener(window, 'load', function() {}); 并尝试了所有我能找到的关于“谷歌地图JavaScript API灰屏”的解决方案,但都没有解决问题。以下是相关代码: index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">Ionic Blank Starter</h1>
      </ion-header-bar>
      <ion-nav-view><!-- Other html files injected here --></ion-nav-view> 
    </ion-pane>
    <script src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
  </body>
</html>

app.js

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
var example = angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
});


example.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider
    .state('home', {
      url: "/home",
      templateUrl: "app/home/home.html"
    })
    .state('intermediate', {
      url: "/intermediate",
      templateUrl: "app/intermediate/intermediate.html"
    })
    .state('office', {
      url: "/office",
      templateUrl: "app/office/office.html"
    });

  $urlRouterProvider.otherwise("/home");
});

example.controller("MapController", function($scope, $ionicLoading) {

    //google.maps.event.addDomListener(window, 'load', function() {
        var myLatlng = new google.maps.LatLng(37.3000, -120.4833);

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

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

        //$scope.map = map;
    //});

});

style.css

/* Empty. Add your own CSS if you like */
.scroll {
    height: 100%;
}

#map {
    height: 100%;
    width: 100%;
}

home.html

<ion-view>

    <ion-content class="padding has-header" ng-controller="MapController">

    <h1>Home</h1>   
    <h1><a ui-sref="intermediate">Intermediate</a></h1>

    <div id="map" data-tap-disabled="true"></div>

    </ion-content>
</ion-view>

intermediate.html

<ion-view>

    <ion-content class="padding has-header" ng-controller="MapController">

    <h1>Intermediate</h1>
    <a ui-sref="home">Home</a>
    <a ui-sref="office">Office</a>

    </ion-content>
</ion-view>

office.html

<ion-view>

    <ion-content class="padding has-header" ng-controller="MapController">

    <h1>Office</h1>
    <h1><a ui-sref="home">Home</a></h1>

    <div id="map" data-tap-disabled="true"></div>

    </ion-content>
</ion-view>
4个回答

5

我今天遇到了类似的问题。通过将地图初始化放入$timeout块中解决了这个问题:

$timeout(function() {
    // initialization here
});

我认为问题来自于DOM/map加载,尝试使用$timeout来触发新的digest循环。


编辑:ionic2用户可以尝试使用常规的setTimeout调用,或者从NgZone中运行,以尝试相同的操作。


1
即使在Ionic 2中使用普通的setTimeout函数,这对我也有效!谢谢! - HansMu158

5

因为您有多个具有相同ID的地图元素。JavaScript不允许这样做。我知道它们是独立的页面,但在Ionic框架中,这三个页面在同一个DOM中。当您切换到另一个页面时,Ionic会将ion-view附加到当前DOM而不是重新构建一个全新的DOM。 Ionic默认使用这种方式以获得更好的UI性能。为了确认这一点,您可以在页面之间切换并使用检查器查找id="map"。您将找到多个结果,并且JavaScript无法确定应该将地图HTML添加到哪个地图上。为避免这种情况,您可以像这样将缓存设置为false:

  $stateProvider
    .state('home', {
      url: "/home",
      cache: false,
      templateUrl: "app/home/home.html"
    })
    .state('intermediate', {
      url: "/intermediate",
      cache: false,
      templateUrl: "app/intermediate/intermediate.html"
    })
    .state('office', {
      url: "/office",
      cache: false,
      templateUrl: "app/office/office.html"
    });

此外,对于谷歌地图本身,您需要触发一个调整大小的事件来修复灰色区域。在生成地图对象后,您需要添加以下代码:
google.maps.event.addListener(map, "idle", function(){
    google.maps.event.trigger(map, 'resize'); 
});

抱歉提出这个问题,但我应该在哪里编写那段代码? - Usr

0
今天,我尝试在iOS设备上运行我的地图应用程序。它向我展示了地图下方的红色位置和Google标志。但没有地图。 它向我展示了灰色。
我通过在 Xcode -> Targets -> General -> Bundle Identifier API控制台 -> 凭据 -> API密钥 -> iOS应用程序 -> Bundle Identifiers中放置相同的捆绑标识符来解决了这个问题。问题解决了。 之前是不同的,所以地图向我显示为灰色。

0

今天我在 Ionic 4 中遇到了类似的问题。 使用 setTimeout 解决了灰色地图问题。

ngOnInit() {
  setTimeout(() => {
    this.loadMap()
  }, 100);
}

loadMap() {
  //Your map code
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接