Angularjs 当使用ui路由器在Angular中更改路由时,如何重新初始化ngMaps?
我正在使用,以下是我的相关代码:Angularjs 当使用ui路由器在Angular中更改路由时,如何重新初始化ngMaps?,angularjs,google-maps,google-maps-api-3,ng-map,Angularjs,Google Maps,Google Maps Api 3,Ng Map,我正在使用,以下是我的相关代码: <div map-lazy-load="https://maps.google.com/maps/api/js" map-lazy-load-params="{{googleMapsUrl}}" ng-style="{'height': feedActualHeight + 'px'}"> <ng-map id="iPadFeedMap" zoom="14" center="Times Square, New York, NY"
<div map-lazy-load="https://maps.google.com/maps/api/js" map-lazy-load-params="{{googleMapsUrl}}" ng-style="{'height': feedActualHeight + 'px'}">
<ng-map id="iPadFeedMap" zoom="14" center="Times Square, New York, NY">
</ng-map>
</div>
这非常有效,可以完美地加载地图。但当我离开并返回页面时,地图将变灰:
加载此页面时是否有重新初始化地图的方法?似乎只有当
中心属性的值作为地址值提供时才会发生这种情况(例如纽约时代广场)。在这种情况下,确定地图中心的方法包括以下步骤:
- 由于利用了,因此会调用相应的方法来解析实际位置(
lat,lng
)
- 映射对象
中心
属性正在更新
由于当中心属性作为位置值提供时(例如,[40.759011,-73.9844722000003]
),我从未遇到过此问题,因此我建议更改地图的初始化方式:
- 删除
center
属性的设置center=“时代广场,新建
纽约州约克市“
- 改为设置地图中心,如下所示:
示例:按地址设置地图中心
NgMap.getMap("iPadFeedMap").then(function (map) {
NgMap.getGeoLocation($scope.address)
.then(function (latlng) {
map.setCenter(latlng);
});
});
$scope.address = "Times Square, New York, NY";
无论如何设置中心,我都遇到了完全相同的问题(lat/lng和物理地址都失败了),但被接受的答案对我来说不太合适
我的解决方案是在每次返回地图时,使用NgMap的lazy init
属性重新初始化地图
<ng-map id="dashboard-map"
center="Boston, MA"
lazy-init="true"
zoom="9"
map-type-id="TERRAIN">
</ng-map>
这样,每当我点击仪表板时,ctrl键就会运行并重新初始化映射。现在每次都能正确显示
这是一个很有帮助的线索:
// dashboardCtrl.js
NgMap.getMap({ id: "dashboard-map" }).
then(function(map) {
NgMap.initMap(map.id);
});