eri*_*sse 3 google-maps google-maps-api-3 angularjs ionic
谷歌地图与离子框架合作我遇到了很多麻烦.这是一个示例,我按照本指南让google地图成功地在index.html上工作.但是,当我尝试向应用添加一些新页面并在它们之间导航时,除非您刷新页面,否则地图无法正常显示.
我做了3页注入index.html:家庭,中介和办公室.Home应该显示谷歌地图并链接到中介.中介只是一个空白页面,链接到家庭和办公室.办公室也应该显示地图并链接回家.
这是我得到的行为:
为什么会这样?我试过包装谷歌地图功能
google.maps.event.addDomListener(window, 'load', function() {});
并尝试了我发现的"google maps javascript api grey 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>
Run Code Online (Sandbox Code Playgroud)
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;
//});
});
Run Code Online (Sandbox Code Playgroud)
style.css文件
/* Empty. Add your own CSS if you like */
.scroll {
height: 100%;
}
#map {
height: 100%;
width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我今天遇到了类似的问题。通过将地图初始化放在 $timeout 块中来解决它:
$timeout(function() {
// initialization here
});
Run Code Online (Sandbox Code Playgroud)
我认为问题来自 DOM/地图加载,并试图$timeout引发新的摘要循环。
编辑: ionic2 用户可以尝试常规setTimeout调用,或run从NgZone尝试相同的调用。
因为您有多个具有相同ID的地图元素.Javascript只是不允许这样.我知道它们是单独的页面,但是使用Ionic框架,这三个页面位于同一个DOM中.当您切换到另一个页面时,Ionic会将离子视图附加到当前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"
});
Run Code Online (Sandbox Code Playgroud)
此外,对于谷歌地图本身,您需要触发调整大小事件来修复灰色.生成地图对象后,您需要具有以下代码:
google.maps.event.addListener(map, "idle", function(){
google.maps.event.trigger(map, 'resize');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2664 次 |
| 最近记录: |