I am using a custom GoogleMap AngularJS Directive, the less popular version of GitHub. I am using it because I was not happy with the way the other one handled events.
I have a GeoJSON file full of shapes that fill a map. I would to make it interactive so that you can select each area. So I have managed to get the initial area select and area deselect to work but if you select an area then select a different one thereafter, it should change the style of the old selected area and update the new one.
I iterate over the GeoJSON file and make the scope.
$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
});
};
});
Then use and ng-repeat to add them to the map.
<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>
Then I add the event listeners. These two for the hover effects.
// 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
});
};
}
This one for when an area has been selected
$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
});
};
}
Additionally a custom function to work out the center of the area selected.
// 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;
}
How can I update the style of a shape from the scope? Plunker
$scope.map.shapes[id]
You are on the same page with me on angular-ui google maps. :)
If this does not satisfy your question, please make a very simple plnkr based on the example,
https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/shape.html or https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/shape_with_ng_repeat.html
Then, let me know. To create a plnkr, just simply click 'Plunkr' on the page.
I will make it fixed until you are satisfied.