AngularJS/NgMap-在引脚设置更改时重新输入映射
AngularJS/NgMap - Recenter map as pin sets change
我使用AngularJS和NgMap来显示一个谷歌地图实例,该实例显示一组引脚,这些引脚根据3个不同选择列表中的位置选择进行上下过滤。随着焦点越来越紧(从地区到州/省/欧盟国家,再到城市),我希望地图缩放到离位置更近的位置。
我正在利用缩放在地图元素中包括标记标签,但当设置为自动时,缩放是不稳定的,而且经常是错误的,使用true时,缩放永远不会发生,尽管它确实将所有元素都保留在视图中。
Plunk:http://plnkr.co/edit/qaLFYD?p=preview
<div map-lazy-load="http://maps.google.com/maps/api/js">
<map zoom="2" scrollwheel="false" zoom-to-include-markers="true">
<span ng-repeat="site in (dataObject | ForMap : {region:selectRegion, state:selectState, city:selectCity}) track by $index" id="{{data.Id}}">
<marker position="[{{site.Latitude}},{{site.Longitude}}]"></marker>
</span>
</map>
</div>
ngMap
没有观察标记,这就是为什么一旦标记更新,地图视口就不会居中和缩放(通过zoom-to-include-markers
属性)。但您可以考虑更新视口并显式缩放。为此,我们引入以下功能:
$scope.zoomToIncludeMarkers = function(cities) {
var bounds = new google.maps.LatLngBounds();
cities.forEach(function(c) {
var latLng = new google.maps.LatLng(c.pos[0],c.pos[1]);
bounds.extend(latLng);
});
$scope.map.fitBounds(bounds);
if(cities.length == 1){
$scope.map.setZoom(5);
}
};
其中cities
在我的情况下具有以下结构:
$scope.cities = [
{id: 1,name: 'Oslo', pos:[59.923043, 10.752839]},
{id: 2,name: 'Stockholm',pos:[59.339025, 18.065818]},
{id: 3,name: 'Copenhagen',pos:[55.675507, 12.574227]},
{id: 4,name: 'Berlin',pos:[52.521248, 13.399038]},
{id: 5,name: 'Paris',pos:[48.856127, 2.346525]}
];
因此,每次标记更改后,我们都需要调用:
$scope.zoomToIncludeMarkers($scope.cities);
工作示例
angular.module('ngMap').controller('MyCtrl', function($scope) {
$scope.selectedCities = [];
$scope.cities = [
{id: 1,name: 'Oslo', pos:[59.923043, 10.752839]},
{id: 2,name: 'Stockholm',pos:[59.339025, 18.065818]},
{id: 3,name: 'Copenhagen',pos:[55.675507, 12.574227]},
{id: 4,name: 'Berlin',pos:[52.521248, 13.399038]},
{id: 5,name: 'Paris',pos:[48.856127, 2.346525]}
];
$scope.selectionsChanged = function(){
$scope.selectedCities = [];
$scope.selectedValues.forEach(function(cid){
var city = $scope.cities.filter(function(c){
if(c.id == parseInt(cid))
return c;
})[0];
$scope.selectedCities.push(city);
});
$scope.zoomToIncludeMarkers();
};
$scope.zoomToIncludeMarkers = function() {
var bounds = new google.maps.LatLngBounds();
$scope.selectedCities.forEach(function(c) {
var latLng = new google.maps.LatLng(c.pos[0],c.pos[1]);
bounds.extend(latLng);
});
$scope.map.fitBounds(bounds);
if($scope.selectedCities.length == 1){
$scope.map.setZoom(5);
}
};
});
<script src="https://maps.google.com/maps/api/js?libraries=placeses,visualization,drawing,geometry,places"></script>
<script src="http://code.angularjs.org/1.2.25/angular.js"></script>
<script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
<div ng-app="ngMap" ng-controller="MyCtrl">
<h2>Select cities:</h2>
<select multiple ng-model="selectedValues" ng-change="selectionsChanged()">
<option ng:repeat="c in cities" selected value="{{c.id}}">{{c.name}}</option>
</select>
<!--tt> selectedValues = {{selectedCities}}</tt-->
<ng-map zoom="5" center="{{center}}"
scrollwheel="false"
zoom-to-include-markers="true">
<marker ng-repeat="c in selectedCities"
position="{{c.pos}}" title="{{c.name}}"></marker>
</ng-map>
</div>
Plunker
相关文章:
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 输入时只获取新的输入值
- 如果所有其他元素都填写在AngularJS中,如何动态添加新的输入元素
- 从按钮生成新的输入字段
- 如何使用jQuery添加输入字段并将Ajax调用到新的输入字段中
- 当
- 中的最后一个输入具有值时,添加新的<输入>
- jQuery在keyup上动态插入新的输入框
- 填写所有先前字段后,添加新的输入字段
- 如何添加新的输入行并插入数据库并出现错误 未初始化的字符串偏移量:0 in
- 使用 JQuery 在不涉及整页刷新的动态页面更改后访问/传播选择器到新的输入字段
- Laravel 5.1和JavaScript-为表单添加新的输入
- 如何在“”上动态地添加新的输入字段;keydown”;使用jQuery
- 使用JavaScript插入一行新的输入标记
- 如何使用.keyup()添加新的输入字段
- 在HTTP请求后向表单添加新的输入
- 如何比较值输入与新的输入javascript
- 显示新的输入值(添加空格)
- 如何禁用添加新文本输入的按钮,以防止用户继续添加
- 添加新的输入:复选框,没有值
- 新的输入占位符行为在Safari 10中-不再隐藏通过JavaScript更改