【发布时间】: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