我使用的是自定义GoogleMap AngularJS指令,这是不太受欢迎的GitHub版本。我之所以使用它,是因为我对其他 one处理事件的方式不满意。
我有一个GeoJSON文件,里面充满了填充地图的形状。我想让它具有交互性,这样你就可以选择每个区域了。因此,我已经设法使最初的区域选择和区域取消选择工作,但是如果您选择了一个区域,然后选择一个不同的区域之后,它应该改变旧选定区域的样式并更新新的区域。
我遍历GeoJSON文件并创建范围。
$scope.shapes = [];
$http.get('/static/json/areas.json').then(function(res){
for (var x = res.data.features.length -1; x >= 0; x--) {
paths = [];
for (var y = res.data.features[x].geometry.coordinates[0].length - 1; y >= 0; y--) {
paths.push([ res.data.features[x].geometry.coordinates[0][y][3], res.data.features[x].geometry.coordinates[0][y][0]]);
};
$scope.shapes.push({
areaName: res.data.features[x].properties.name,
path: paths,
fillColor: '#4F639E',
fillOpacity: 0.4,
strokeColor: '#4F639E',
strokeOpacity: 1,
strokeWeight:1
});
};
});然后使用和ng重复将它们添加到地图上。
<map
zoom="13"
center="[-33.955, 18.42]"
disable-default-u-i="true"
disable-double-click-zoom="true"
draggable="true"
keyboard-shortcuts="false"
map-type-id="SATELLITE">
<shape ng-repeat="shape in shapes track by $index"
on-mouseover="areaOver()"
on-mouseout="areaOut()"
on-click="areaClick()"
id="{{$index+1}}"
name="polygon"
stroke-color="{{shape.strokeColor}}"
stroke-opacity="{{shape.strokeOpacity}}"
stroke-weight="{{shape.strokeWeight}}"
fill-color="{{shape.fillColor}}"
fill-opacity="{{shape.fillOpacity}}"
paths="{{shape.path}}">
</shape>
</map>然后添加事件侦听器。这两种对悬停的影响。
// Highlight area on hover
$scope.areaOver = function() {
if ($scope.select != this.id) {
this.setOptions({
fillOpacity: 1
});
};
}
// Return area on mouse-out to previous style
$scope.areaOut = function() {
if ($scope.select != this.id) {
this.setOptions({
fillOpacity: 0.4
});
};
}这个是当选定了一个区域时使用的
$scope.areaClick = function() {
var path = this.getPath();
// The first time area selected
if ($scope.select == null) {
$scope.select = this.id;
$scope.map.fitBounds(get_bounds(path.j))
this.setOptions({
fillOpacity: 0,
strokeColor: '#FFFFFF',
strokeWeight: 2,
zIndex: +1
});
}
// When the same area has been selected again - reset view.
else if ($scope.select == this.id) {
$scope.select = null;
this.setOptions({
fillOpacity: 0.4,
strokeColor: '#4F639E',
strokeWeight: 1,
zIndex: -1
});
$scope.map.setZoom(13);
$scope.map.setCenter({lat: -33.96, lng: 18.38});
}
// When a different area has been selected - reset old style then update new style.
else {
// Reset old selected shape
$scope.map.data.revertStyle(); // Not working
$scope.apply;
for (var i = $scope.map.shapes.length - 1; i >= 0; i--) {
$scope.map.shapes[i].setOptions({ // Not working
fillOpacity: 0.4,
strokeColor: '#4F639E',
strokeWeight: 1,
zIndex: -1
});
};
$scope.apply;
// Move / Style newly selected shape
$scope.select = this.id;
$scope.map.fitBounds(get_bounds(path.j))
this.setOptions({
fillOpacity: 0,
strokeColor: '#FFFFFF',
strokeWeight: 2,
zIndex: +1
});
};
}另外还有一个自定义函数,用于计算选定区域的中心。
// Custom Function
function get_bounds(path) {
var smallest_lat = 360;
var smallest_lng = 360;
var largest_lat = -360;
var largest_lng = -360;
for (var i = path.length - 1; i >= 0; i--) {
var lat = path[i].lat();
var lng = path[i].lng();
if (lat > largest_lat) {largest_lat = lat};
if (lat < smallest_lat) {smallest_lat = lat};
if (lng > largest_lng) {largest_lng = lng};
if (lng < smallest_lng) {smallest_lng = lng};
}
var northEast = new google.maps.LatLng(smallest_lat, smallest_lng);
var southWest = new google.maps.LatLng(largest_lat, largest_lng);
var bounds = new google.maps.LatLngBounds(northEast, southWest);
return bounds;
}如何从作用域更新形状的样式?柱塞
发布于 2014-09-29 15:00:38
$scope.map.shapes[id]
你和我在一个页面上的角度用户界面谷歌地图。:)
如果这不能满足你的问题,请根据这个例子做一个非常简单的plnkr,
https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/shape.html或repeat.html
那就告诉我。要创建plnkr,只需单击页面上的“Plunkr”即可。
我会把它修好,直到你满意为止。
https://stackoverflow.com/questions/26101004
复制相似问题