mapsplugin / cordova-plugin-googlemaps

Google Maps plugin for Cordova
Apache License 2.0
1.66k stars 916 forks source link

Last marker stays on map / clear() seems not to trigger #204

Closed hirbod closed 10 years ago

hirbod commented 10 years ago

Hi Masashi, I guess I've found another bug. On my map I try to find the position and display it on the map. When I change the page and return, my last marker position got readded on the map again even I completly .remove() and .clear() everything before on init.

Just to make my point clearer: (please look at commentar in the code)

I have an AngularJS "MapController"

$('ons-sliding-menu div:first').css('background-color', 'rgba(0,0,0,0)');

        // i had some problems with reinit so i need to delete and remove everything
       // i even fired .clear() before

        if($rootScope.map) {
            $rootScope.map.clear();
            $rootScope.map.remove();
            $('#map_canvas').remove();

              // and finaly resetted the scopeVar
            $rootScope.map = '';
        }

         // just to prevent that the transparent background beein applied  to fast
         setTimeout(function(){

            // as usual, the div
            var div = document.getElementById("map_canvas");
            // some maths for the height
            $('#map_canvas').css('height', (($(window).height() - $('#map_canvas').offset().top) / 2));

           // in case of its the "first init" and we do not have $rootScope.map
            $rootScope.map = '';

            // get the map
            $rootScope.map = plugin.google.maps.Map.getMap(div);

            // again, i tried to clear but it keeps
            $rootScope.map.clear();

            // this function seems not to work
            $rootScope.map.setBackgroundColor('white');

            //$rootScope.map.refreshLayout();

            // triggerin onInit function
            $rootScope.map.on(plugin.google.maps.event.MAP_READY, $scope.onMapInit);

        }, 600);

Now the init-function

        $scope.onMapInit = function(map){
            console.log('MAP INITED');

             // changed to cordova geolocation plugin, because getMyLocation only fires once on iOS
            //$scope.map.getMyLocation($scope.onSuccess, $scope.onError);

            navigator.geolocation.getCurrentPosition($scope.onSuccess, $scope.onError);
        };

And finally the success callback


        $scope.onSuccess = function(position) {

            console.log('LOCATION TRACED');

            const MY_POS = new plugin.google.maps.LatLng(position.coords.latitude,position.coords.longitude);

             // call the addMarker
            $rootScope.map.addMarker({

                'position': MY_POS,
                'title': 'test'

            }, function(marker) {
                //marker.showInfoWindow();

                console.log(location.latLng);

                //$rootScope.map.setZoom(8);
               // center the map
                $rootScope.map.setCenter(MY_POS);

               // make some animation
                $rootScope.map.animateCamera({

                    'target': MY_POS,
                    'zoom': 17,
                    'duration': 5000,
                    'bearing': -140,
                    'tilt': 50

                },  function() {

                    }

                );

            });

        };

Everything seems fine, but when I had another position before now I can see two markers.

foto

hirbod commented 10 years ago

Ok, I guess its my fault.

            $rootScope.map.on(plugin.google.maps.event.MAP_READY, $scope.onMapInit);

After hours of programming I really seem to get blind after some time. Is there an off() function? I add the listener all the time... thats the reason...

hirbod commented 10 years ago

Ok, there is an off() function but I can not chain it. But thats ok. Sadly, the listener doesn't get removed and this is for sure a bug. My console.log() seem to fire everything 2,3,4,5 times (depens how often I recalled)

Don't work: $rootScope.map.off(plugin.google.maps.event.MAP_READY); $rootScope.map.removeEventListener(plugin.google.maps.event.MAP_READY);

Even tried this: $rootScope.map.off(plugin.google.maps.event.MAP_READY, $scope.onMapInit); $rootScope.map.removeEventListener(plugin.google.maps.event.MAP_READY, $scope.onMapInit);

Works (YAY)!!! $rootScope.map.removeEventListener();

205

wf9a5m75 commented 10 years ago

map.clear() does not include map.off() As of v1.2.2, map.remove() will include map.off(), but you need to call map.off() in v1.2.1.

hirbod commented 10 years ago

Allright, thanks for your quick response. That is fine!

wf9a5m75 commented 10 years ago

:+1: