angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

Multiple markers and center map

Open mohammedshafiq opened this issue 10 years ago • 3 comments

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 avatar Dec 06 '15 14:12 mohammedshafiq

@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");
        }

karthikvu avatar Dec 07 '15 06:12 karthikvu

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 avatar Dec 09 '15 15:12 david-meza

@david-meza I've tried the ui-gmap-markers and fit=true method and this works perfectly. Thanks.

mohammedshafiq avatar Dec 09 '15 18:12 mohammedshafiq