【发布时间】:2015-06-08 11:28:52
【问题描述】:
我一直在涉足angular implementation of the google maps API 并且一直在努力弄清楚如何访问任何常规 API 帮助程序。
我的问题最初是因为我想根据我的标记以编程方式居中地图。 事实证明这很奇怪,我似乎遗漏了一些小东西,或者我误解了如何使用该指令创建新地图
我的理解是我需要通过uiGmapIsReady方法访问google API:
uiGmapIsReady.promise()
.then(function(instances) {
// simplified for one map
return instances[0].map;
})
.then(function(map) {
// here is have access to utility functions like like"map.getCenter()" via map
console.log(map);
});
问题是,上面是一个单独的实例,而不是我创建并绑定到视图的原始地图对象。 (如果有道理的话)
EG。 map 可以访问助手,但它没有绑定到指令,并且设置 $scope.map = map、_.merge($scope.map, map) 或其他变体不起作用(/ 感觉不对,感觉 hacky)
控制器代码:
//Hide map until ready
$scope.loadingIsDone = false;
$scope.markers = [];
// map wont work without these "hard-coded" values
$scope.map = {
center: {
latitude: 40,
longitude: 13.37
},
zoom: 10
};
uiGmapIsReady.promise()
.then(function(instances) {
return instances[0].map;
})
.then(function(map) {
// id like to set the center and bounds based on markers
// assuming it must happen here?
uiGmapGoogleMapApi.then(function(googleMaps) {
googleMaps.event.addDomListener(window, "resize", function() {
var center = map.getCenter();
googleMaps.event.trigger(map, "resize");
map.setCenter(center);
});
});
});
// get my data
uiGmapGoogleMapApi.then(function(maps) {
someAPI.logIn.then(function(token) {
// supply token to get future request
someAPI.getOffers(token).then(function(res) {
var markers = [];
//convert data points to markers
angular.forEach(res.data, function(value, key) {
markers.push(coordinate.MakeMarker(value));
});
// apply marker collection to scope object, and unhide map
$scope.markers = markers;
$scope.loadingIsDone = true;
});
});
});
查看
<div ng-if="loadingIsDone" class="animate-if map-container">
<div id="map_canvas">
<ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="true" bounds="map.bounds">
<ui-gmap-markers models="markers" coords="'self'" icon="'icon'"> </ui-gmap-markers>
</ui-gmap-google-map>
</div>
</div>
总结
- 如何以正确的方式创建新地图实例 EG...
$scope.map = - ...允许通过绑定对象访问普通 (Google) API 帮助程序
- 允许我设置中心和“绑定”坐标
【问题讨论】:
标签: angularjs google-maps google-maps-api-3 angular-google-maps