我在使用Ionic框架时,与谷歌地图的兼容性出现了很多问题。我遵循了这篇指南上的步骤,在index.html中成功地显示了谷歌地图。但当我尝试添加新页面到应用程序并在它们之间切换时,地图不会正确显示,除非你刷新页面。
我制作了三个注入到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>
setTimeout
函数,这对我也有效!谢谢! - HansMu158