【问题标题】:ionic - google maps not displaying (or showing grey)离子 - 谷歌地图不显示(或显示灰色)
【发布时间】:2015-11-10 12:42:26
【问题描述】:

我在让谷歌地图与离子框架合作时遇到了很多麻烦。这是一个示例,我按照this guide 让谷歌地图成功地在 index.html 上工作。但是,当我尝试向应用程序添加一些新页面并在它们之间导航时,除非您刷新页面,否则地图无法正确显示。

我制作了 3 个页面注入 index.html:家庭、中介和办公室。主页应该显示谷歌地图并链接到中介。中介只是一个链接到家庭和办公室的空白页面。 Office 还应该显示返回主页的地图和链接。

这是我得到的行为:

  • 家 -> 中级 -> 办公室 - 地图不显示在办公室
  • 家 -> 中级 -> 办公室 -> 家 - 地图是灰色的

为什么会这样?我试过用

包装谷歌地图功能

google.maps.event.addDomListener(window, 'load', function() {});

并尝试了我为“google maps javascript api gray screen”找到的所有解决方案,但似乎没有任何效果。

以下是相关来源:

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>

【问题讨论】:

    标签: angularjs google-maps google-maps-api-3 ionic


    【解决方案1】:

    因为您有多个具有相同 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'); 
    });
    

    【讨论】:

    • 嗨,很抱歉提出这个问题,但我在哪里写代码?
    【解决方案2】:

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

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

    我认为问题来自 DOM/地图加载,并尝试$timeout 引发新的摘要循环。


    编辑:ionic2 用户可以尝试常规的setTimeout 呼叫,或NgZone 中的run,以便尝试相同的操作。

    【讨论】:

    • 即使在具有正常 setTimeout 功能的 Ionic 2 中也适用于我!谢谢!
    【解决方案3】:

    今天,我尝试在 iOS 设备上运行我的地图应用程序。它向我显示了地图下方的红色位置和谷歌符号。但不是地图。它显示我是灰色的。

    我通过在 Xcode -> Targets -> General -> Bundle IdentifierAPI Console -> Credentials -> API key -> iOS apps -> 中放置相同的包标识符来解决它捆绑标识符。问题已解决。在它不同之前,这就是地图显示为灰色的原因。

    【讨论】:

      【解决方案4】:

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

      ngOnInit() {
        setTimeout(() => {
          this.loadMap()
        }, 100);
      }
      
      loadMap() {
        //Your map code
      }
      

      【讨论】:

        猜你喜欢
        • 2018-11-15
        • 2018-07-16
        • 2013-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多