Multiple markers and center map
HI,
I'm sure the answer is quite simple but I've done a search but cannot seem to find it.
I would like to centre my map based on some given markers on initial load of the map. The map should therefore display all the markers with the correct centre position and zoom level.
How can I achieve this?
<ui-gmap-google-map center='map.center' zoom='map.zoom' bounds="map.bounds">
<ui-gmap-marker ng-repeat="m in map.markers" coords="m.coords" icon="m.icon" idkey="m.id">
</ui-gmap-marker>
</ui-gmap-google-map>
$scope.googlemap = {};
$scope.map = {
center: {
latitude: 0,
longitude: 0
},
zoom: 17,
markers: [],
bounds: {},
pan: 1,
options: $scope.mapOptions,
control: {},
events: {
tilesloaded: function (maps, eventName, args) {},
dragend: function (maps, eventName, args) {},
zoom_changed: function (maps, eventName, args) {}
}
};
$scope.map.markers.push({
id: Date.now(),
coords: {
latitude: $scope.placeDetails.geometry.location.lat,
longitude: $scope.placeDetails.geometry.location.lng
}
});
@mohammedshafiq : I had achieved the same in the following way :
<ui - gmap - google - map center = 'map.center' bounds = "bnds" >
var bounds = new google.maps.LatLngBounds();
angular.forEach(markers, function(k, i) {
var myLatLng = new google.maps.LatLng(Number($rootScope.markerList[i].latitude), Number($rootScope.markerList[i].longitude));
bounds.extend(myLatLng);
.....
}
$rootScope.markerList = markers; $rootScope.setMapBounds(bounds);
$rootScope.setMapBounds = function(bounds) {
$timeout(function() {
$rootScope.bnds = {
northeast: {
latitude: bounds.getNorthEast().lat(),
longitude: bounds.getNorthEast().lng()
},
southwest: {
latitude: bounds.getSouthWest().lat(),
longitude: bounds.getSouthWest().lng()
}
}
}, 100);
console.log("Bounds set");
}
If you use the ui-gmap-markers directive instead of ui-gmap-marker, there is a fit attribute which you can set to true to achieve what you are looking for
@david-meza I've tried the ui-gmap-markers and fit=true method and this works perfectly. Thanks.