2

I am creating a trip view using google map with angularjs directive. All works fine except the google map fitbound to a latitude longitude collection. I have tried all the way to use the fitbounds methods but failed.

Thanks,

Below is my code efforts.

Directive:

 <div class="trip-google-map" trip-map="" mapid="2" latitude="22.998673" longitude="72.514346"></div>

Directive Code:

app.directive('tripMap', function ($compile) {
    return {
        controller: function ($scope, $location, mapService) {
            this.registerMap = function (myMap) {
                mapService.setTripMap(myMap);

            };
        },
        link: function (scope, elem, attrs, ctrl) {


            var mapOptions,
                latitude,
                longitude,
                mapStyles,
                map;
            latitude = attrs.latitude;
            longitude = attrs.longitude;
            mapStyles =
                [
                    {
                        "featureType": "water",
                        "elementType": "geometry",
                        "stylers": [
                            {
                                "color": "#a2daf2"
                            }
                        ]
                    },
                    {
                        "featureType": "landscape.man_made",
                        "elementType": "geometry",
                        "stylers": [
                            {
                                "color": "#f7f1df"
                            }
                        ]
                    },
                    {
                        "featureType": "landscape.natural",
                        "elementType": "geometry",
                        "stylers": [
                            {
                                "color": "#d0e3b4"
                            }
                        ]
                    },
                    {
                        "featureType": "landscape.natural.terrain",
                        "elementType": "geometry",
                        "stylers": [
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "poi.park",
                        "elementType": "geometry",
                        "stylers": [
                            {
                                "color": "#bde6ab"
                            }
                        ]
                    },
                    {
                        "featureType": "poi",
                        "elementType": "labels",
                        "stylers": [
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "poi.medical",
                        "elementType": "geometry",
                        "stylers": [
                            {
                                "color": "#fbd3da"
                            }
                        ]
                    },
                    {
                        "featureType": "poi.business",
                        "elementType": "all",
                        "stylers": [
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "road",
                        "elementType": "geometry.stroke",
                        "stylers": [
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "road",
                        "elementType": "labels",
                        "stylers": [
                            {
                                "visibility": "on"
                            }
                        ]
                    },
                    {
                        "featureType": "road.highway",
                        "elementType": "geometry.fill",
                        "stylers": [
                            {
                                "color": "#ffe15f"
                            }
                        ]
                    },
                    {
                        "featureType": "road.highway",
                        "elementType": "geometry.stroke",
                        "stylers": [
                            {
                                "color": "#efd151"
                            }
                        ]
                    },
                    {
                        "featureType": "road.arterial",
                        "elementType": "geometry.fill",
                        "stylers": [
                            {
                                "color": "#ffffff"
                            }
                        ]
                    },
                    {
                        "featureType": "road.local",
                        "elementType": "geometry.fill",
                        "stylers": [
                            {
                                "color": "black"
                            }
                        ]
                    },
                    {
                        "featureType": "transit.station.airport",
                        "elementType": "geometry.fill",
                        "stylers": [
                            {
                                "color": "#cfb2db"
                            }
                        ]
                    }
                ];



            mapOptions = {
                zoom: 12,
                disableDefaultUI: true,

                center: new google.maps.LatLng(latitude, longitude),

                mapTypeId: google.maps.MapTypeId.ROADMAP,
                styles: mapStyles,
                mapTypeControl: true,

                mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                    position: google.maps.ControlPosition.BOTTOM_CENTER
                },
                panControl: true,
                panControlOptions: {
                    position: google.maps.ControlPosition.LEFT_CENTER
                },
                zoomControl: true,
                zoomControlOptions: {
                    style: google.maps.ZoomControlStyle.LARGE,
                    position: google.maps.ControlPosition.LEFT_CENTER
                },
                scaleControl: true,
                streetViewControl: true,
                streetViewControlOptions: {
                    position: google.maps.ControlPosition.LEFT_CENTER
                },
            };
            /*
                        var mapOptions = {
                            mapTypeId: google.maps.MapTypeId.ROADMAP,
                            mapTypeControl: false
                        };
                        */
            //google.maps.visualRefresh = true;
            var map = new google.maps.Map(elem[0], mapOptions);
            ctrl.registerMap(map);
            scope.InitializeTripdetailController();

            scope.$apply(function () {
                window.setTimeout(function () {
                    google.maps.event.trigger(map, 'resize');
                }, 100);
            });
        }

    };
});

Controller Code:

app.controller('tripdetailController', ['$scope', '$rootScope', '$timeout', 'mapService', 'ngDialog', function ($scope, $rootScope, $timeout, mapService, ngDialog) {
        $scope.tripPathPolylines = [];
        var trips = [];
        var insertTripLatlng = function (trip) {
            var isExists = false;
            for (var v in trips) {
                if (trips[v].hash == trip.hash) {
                    isExists = true
                    break;
                }
            }
            if (isExists == false) {
                trips.push(trip);
            }
        }
        function settrip() {
            var marker;
            var tripmap = mapService.getTripMap();
            var bounds = new google.maps.LatLngBounds();
            for (var j = 0; j < trips.length; j++) {
                var ltlng = new google.maps.LatLng(trips[j].lat, trips[j].lng);
                bounds.extend(ltlng);

                marker = new google.maps.Marker({
                    position: ltlng,
                    map: tripmap
                });

            }
            tripmap.fitBounds(bounds);
        }
        $scope.InitializeTripdetailController = function () {
            var tripData = $scope.$parent.ngDialogData;
            for (var p = 0; p < tripData.messages.length; p++) {
                insertTripLatlng({ lat: tripData.messages[p].trackPoint.pos.lat, lng: tripData.messages[p].trackPoint.pos.lng, hash: tripData.messages[p].trackPoint.pos.lat + "-" + tripData.messages[p].trackPoint.pos.lng })
            }
            settrip();
        }
    }]);

FitBounds Code:

 var tripmap = mapService.getTripMap();
 var bounds = new google.maps.LatLngBounds();
 for (var j = 0; j < trips.length; j++) {
     var ltlng = new google.maps.LatLng(trips[j].lat, trips[j].lng);
     bounds.extend(ltlng);
     marker = new google.maps.Marker({position: ltlng,map: tripmap});
  }
  tripmap.fitBounds(bounds);

The map looks like:

It looks as with current code

It should be after fitbounds as:

enter image description here

Update: css for map:

Update: The map is loaded in ngDialog popup.

 .trip-google-map {
     width: 100%;
     height: 450px;
  }
Janty
  • 1,708
  • 2
  • 15
  • 29
  • does it happen too when you remove the resize-trigger for the map? – Dr.Molle Dec 24 '14 at 10:53
  • Thanks @Dr.Molle Yes, After resize-trigger, Map loads faster. But fitbounds not working. – Janty Dec 24 '14 at 11:04
  • As it seems the map doesn't have a size when you call `fitBounds`. Will the size of the map be modified programmatically in any manner? – Dr.Molle Dec 24 '14 at 11:29
  • I am using the size as in css (update css code above ). can be issue due to size width in percentage? – Janty Dec 24 '14 at 11:48
  • @Dr.Molle one thing i forgot to tell about all is opened in ngDialog popup. Is there anything wrong with? – Janty Dec 24 '14 at 11:54
  • 1
    I guess the map is hidden when you call `fitBounds`.I'm not familiar with angularjs, but there is a `opened`-event for a `ngDialog`, try to call `fitBounds` when this event fires. – Dr.Molle Dec 24 '14 at 12:15
  • Thanks @Dr.Molle, Yes... it was due to ngDialog. I have tried it in indepedent window. Its working perfect......Thanks – Janty Dec 24 '14 at 12:26
  • hi Molle, I am not able to resovle it now in ng-dialog. have you any idea? – Janty Dec 25 '14 at 12:49
  • Can you give a link to the page? – Dr.Molle Dec 25 '14 at 20:52

1 Answers1

2

It was due to map is resized by ngDialog popup directive.

Solved by call fitbounds in map resize callback with as

           $scope.$apply(function () {
                window.setTimeout(function () {
                google.maps.event.trigger(tripmap, 'resize');
                    tripmap.fitBounds(bounds);
                }, 100);
            });

Thanks all for help.

Janty
  • 1,708
  • 2
  • 15
  • 29