我正在使用自定义GoogleMap AngularJS Directive,这是不太受欢迎的GitHub版本。我正在使用它,因为我对other处理事件的方式不满意。
我有一个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-repeat将它们添加到地图中。
<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;
}
如何从范围更新形状的样式? Plunker
答案 0 :(得分:0)
$scope.map.shapes[id]
你和我在angular-ui谷歌地图上的同一页面。 :)
如果这不能满足您的问题,请根据示例
制作一个非常简单的plnkrhttps://rawgit.com/allenhwkim/angularjs-google-maps/master/build/shape.html 要么 https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/shape_with_ng_repeat.html
然后,让我知道。要创建一个plnkr,只需单击页面上的“Plunkr”即可。
我会修复它,直到你满意为止。