cordova-plugin-googlemaps icon indicating copy to clipboard operation
cordova-plugin-googlemaps copied to clipboard

Remove Marker issue

Open shrijan opened this issue 9 years ago • 18 comments

I'm submitting a ... (check one with "x") [x ] question [ ] any problem or bug report [ ] feature request

plugin version: (check one with "x") [ ] 1.4.x [x ] 2.0.0-beta2

cordova information: (run $> cordova plugin list)

insert the output from the command here

Current behavior:

Marker not remove in toggle Expected behavior:

It used to work this way with v1. But with v2 it doesn't clear all markers as expected Steps to reproduce:

Screen capture or video record:

https://www.youtube.com/watch?v=E2aZ5jxNxhs Related code, data or error log (please format your code or data):

var div = document.getElementById("map_canvas");
      var map = plugin.google.maps.Map.getMap(div, {
        'mapType': plugin.google.maps.MapTypeId.ROADMAP,
        'controls': {
          'compass': false,
          'myLocationButton': true,
          'indoorPicker': false,
          'zoom': false
        },
        'gestures': {
          'scroll': true,
          'tilt': false,
          'rotate': true,
          'zoom': true
        },
        'camera' : {
          target: {
            lat: -33.826752,
            lng: 151.242946
          },
          zoom: 13
        },
        'preferences': {
          'zoom': {
            'minZoom': 1,
            'maxZoom': 18
          },
          'building': false
        }
      });
     
      map.one(plugin.google.maps.event.MAP_READY, function() {
          $scope.parkMarkerAdded = false;
          var data = [];
          var markers = [];
          function addMarkers(data, callback, type) {
            function onMarkerAdded(marker) {
               marker.cat = type;
               markers.push(marker);
               if (markers.length === data.length) {
                 callback(markers);
               }
            }
            data.forEach(function(markerOptions) {
              map.addMarker(markerOptions, onMarkerAdded);
            });
         }
         function removeMarker(type){
           for (var i = 0; i < markers.length; i++) {
             if(markers[i].cat == type){
              markers[i].remove();
              markers.splice(i--,1)
             }
           }
         }
         $scope.togglePark = function(){
          if($scope.parkMarkerAdded == false){
            addMarkers(data, function(markers) {
              //markers[markers.length - 1].showInfoWindow();
            },'park');
            $scope.parkMarkerAdded = true;
           
          }else{
            removeMarker('park');
            $scope.parkMarkerAdded = false;
          }
        }
      })

If your problem is solved, please consider small amount donation to this project. Appreciate for your kindness. https://github.com/mapsplugin/cordova-plugin-googlemaps-doc/blob/master/README.md#buy-me-a-beer

shrijan avatar Aug 11 '17 00:08 shrijan

Is there any error message in the native log (logcat or xcode)?

wf9a5m75 avatar Aug 11 '17 00:08 wf9a5m75

And what miner version do you use?

wf9a5m75 avatar Aug 11 '17 00:08 wf9a5m75

Hi,

There is no error in native log

cordova-open-native-settings 1.4.1 "Native settings"
cordova-plugin-appavailability 0.4.2 "AppAvailability"
cordova-plugin-cache-clear 1.3.7 "Cordova Cache Clear"
cordova-plugin-camera 2.4.1 "Camera"
cordova-plugin-cocoapod-support 1.3.0 "Cordova CocoaPods Dependency Support"
cordova-plugin-compat 1.1.0 "Compat"
cordova-plugin-console 1.0.7 "Console"
cordova-plugin-device 1.1.6 "Device"
cordova-plugin-file 4.3.3 "File"
cordova-plugin-googlemaps 2.0.0-beta3-20170805-2349 "cordova-plugin-googlemaps"
cordova-plugin-nativeclicksound 0.0.3 "Native Click Sound"
cordova-plugin-splashscreen 4.0.3 "Splashscreen"
cordova-plugin-statusbar 2.2.3 "StatusBar"
cordova-plugin-whitelist 1.3.2 "Whitelist"
cordova.plugins.diagnostic 3.6.6 "Diagnostic"
ionic-plugin-keyboard 2.2.1 "Keyboard"

Should i update to latest version

shrijan avatar Aug 11 '17 00:08 shrijan

Try with more simple code:

var div = document.getElementById("map_canvas");
var map = plugin.google.maps.Map.getMap(div, {
  'mapType': plugin.google.maps.MapTypeId.ROADMAP,
  'controls': {
    'compass': false,
    'myLocationButton': true,
    'indoorPicker': false,
    'zoom': false
  },
  'gestures': {
    'scroll': true,
    'tilt': false,
    'rotate': true,
    'zoom': true
  },
  'camera' : {
    target: {
      lat: -33.826752,
      lng: 151.242946
    },
    zoom: 13
  },
  'preferences': {
    'zoom': {
      'minZoom': 1,
      'maxZoom': 18
    },
    'building': false
  }
});

map.one(plugin.google.maps.event.MAP_READY, function() {
  map.set("parkMarker", false);

  var data = [];

  $scope.togglePark = function(){
    if (!map.get("parkMarker")) {
      addMarkers(map, data, function(markers) {
        //markers[markers.length - 1].showInfoWindow();
        map.set("park", markers);
      });
    } else {
      var markers = new plugin.google.maps.BaseArrayClass(map.get("park"));
      markers.forEach(function(marker, cb) {
        marker.remove(cb);
      }, function() {
        map.set("park", undefined);
      });
    }
    map.set("parkMarker", !map.get("parkMarker"));
  };
});

function addMarkers(map, data, callback) {
  var baseArray = new plugin.google.maps.BaseArrayClass(data);
  baseArray.map(function(markerOpts, cb) {
    map.addMarker(markerOpts, cb);
  }, callback);
}

wf9a5m75 avatar Aug 11 '17 01:08 wf9a5m75

Thanks Masashi,

I used your code but still issue persists

shrijan avatar Aug 11 '17 01:08 shrijan

Android or iOS?

wf9a5m75 avatar Aug 11 '17 01:08 wf9a5m75

IOS

shrijan avatar Aug 11 '17 01:08 shrijan

I went through console. It does remove markers all the time when toggle but some of them are not cleared from map.

shrijan avatar Aug 11 '17 01:08 shrijan

1632

var data = [
  {
    "position": {
      "lat": 21.382314,
      "lng": -157.933097
    },
    "title": "Starbucks - HI - Aiea  03641",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.3871,
      "lng": -157.9482
    },
    "title": "Starbucks - HI - Aiea  03642",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.363402,
      "lng": -157.928275
    },
    "title": "Starbucks - HI - Aiea  03643",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.3884,
      "lng": -157.9431
    },
    "title": "Starbucks - HI - Aiea  03644",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.6507,
      "lng": -158.0652
    },
    "title": "Starbucks - HI - Haleiwa  03645",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 19.699335,
      "lng": -155.06566
    },
    "title": "Starbucks - HI - Hilo  03646",
    "icon": "img/starbucks.png"
  },
  {
    "position": {
      "lat": 19.698399,
      "lng": -155.064864
    },
    "title": "Starbucks - HI - Hilo  03647",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 19.722763,
      "lng": -155.085598
    },
    "title": "Starbucks - HI - Hilo [D]  03648",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.338183,
      "lng": -157.917579
    },
    "title": "Starbucks - HI - Honolulu  03649",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.3074,
      "lng": -157.865199
    },
    "title": "Starbucks - HI - Honolulu  03650",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.30846253,
      "lng": -157.8614898
    },
    "title": "Starbucks - HI - Honolulu  03651",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.311363,
      "lng": -157.863751
    },
    "title": "Starbucks - HI - Honolulu  03652",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.28507546,
      "lng": -157.838214
    },
    "title": "Starbucks - HI - Honolulu  03653",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.342733,
      "lng": -158.027408
    },
    "title": "Starbucks - HI - Honolulu  03654",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.28005068,
      "lng": -157.8285093
    },
    "title": "Starbucks - HI - Honolulu  03655",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.3115,
      "lng": -157.8649
    },
    "title": "Starbucks - HI - Honolulu  03656",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.282048,
      "lng": -157.713041
    },
    "title": "Starbucks - HI - Honolulu  03657",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.291345,
      "lng": -157.848791
    },
    "title": "Starbucks - HI - Honolulu  03658",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.335164,
      "lng": -157.868742
    },
    "title": "Starbucks - HI - Honolulu  03659",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.27852422,
      "lng": -157.7875773
    },
    "title": "Starbucks - HI - Honolulu  03660",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.294372,
      "lng": -157.841963
    },
    "title": "Starbucks - HI - Honolulu  03661",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.2819,
      "lng": -157.8163
    },
    "title": "Starbucks - HI - Honolulu  03662",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.27608195,
      "lng": -157.7049835
    },
    "title": "Starbucks - HI - Honolulu  03663",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.3129,
      "lng": -157.8129
    },
    "title": "Starbucks - HI - Honolulu  03664",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.293082,
      "lng": -157.847092
    },
    "title": "Starbucks - HI - Honolulu  03665",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.341957,
      "lng": -157.929089
    },
    "title": "Starbucks - HI - Honolulu  03666",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.279503,
      "lng": -157.833101
    },
    "title": "Starbucks - HI - Honolulu  03667",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.282251,
      "lng": -157.828172
    },
    "title": "Starbucks - HI - Honolulu  03668",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.323602,
      "lng": -157.890904
    },
    "title": "Starbucks - HI - Honolulu  03669",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.270016,
      "lng": -157.82381
    },
    "title": "Starbucks - HI - Honolulu  03670",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.289497,
      "lng": -157.842832
    },
    "title": "Starbucks - HI - Honolulu  03671",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.28201,
      "lng": -157.831087
    },
    "title": "Starbucks - HI - Honolulu  03672",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.2833,
      "lng": -157.8298
    },
    "title": "Starbucks - HI - Honolulu  03673",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.297,
      "lng": -157.8555
    },
    "title": "Starbucks - HI - Honolulu  03674",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.406095,
      "lng": -157.800761
    },
    "title": "Starbucks - HI - Honolulu  03675",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.2829,
      "lng": -157.8318
    },
    "title": "Starbucks - HI - Honolulu  03676",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.293045,
      "lng": -157.852223
    },
    "title": "Starbucks - HI - Honolulu  03677",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.3067,
      "lng": -157.858444
    },
    "title": "Starbucks - HI - Honolulu [A]  03678",
    "icon": "img/starbucks.png"
  },
  {
    "position": {
      "lat": 20.891963,
      "lng": -156.477614
    },
    "title": "Starbucks - HI - Kahului  03679",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 20.8843,
      "lng": -156.4583
    },
    "title": "Starbucks - HI - Kahului  03680",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.39356972,
      "lng": -157.7403334
    },
    "title": "Starbucks - HI - Kailua  03681",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 19.6512,
      "lng": -155.9944
    },
    "title": "Starbucks - HI - Kailua-Kona  03682",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 19.8434,
      "lng": -155.7652
    },
    "title": "Starbucks - HI - Kamuela  03683",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.34306,
      "lng": -158.078906
    },
    "title": "Starbucks - HI - Kapolei  03684",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.336,
      "lng": -158.0802
    },
    "title": "Starbucks - HI - Kapolei [D]  03685",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 20.740343,
      "lng": -156.456218
    },
    "title": "Starbucks - HI - Kihei  03686",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 20.7575,
      "lng": -156.4559
    },
    "title": "Starbucks - HI - Kihei  03687",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 19.9338,
      "lng": -155.8422
    },
    "title": "Starbucks - HI - Kohala Coast  03688",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 20.886244,
      "lng": -156.684697
    },
    "title": "Starbucks - HI - Lahaina  03689",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 20.8804,
      "lng": -156.6816
    },
    "title": "Starbucks - HI - Lahaina  03690",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.970661,
      "lng": -159.396274
    },
    "title": "Starbucks - HI - Lihue  03691",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 20.8818,
      "lng": -156.4633
    },
    "title": "Starbucks - HI - Maui [A]  03692",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.4585,
      "lng": -158.0178
    },
    "title": "Starbucks - HI - Mililani  03693",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.474341,
      "lng": -158.001864
    },
    "title": "Starbucks - HI - Mililani  03694",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 20.838965,
      "lng": -156.34192
    },
    "title": "Starbucks - HI - Pukalani  03695",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.378675,
      "lng": -157.728499
    },
    "title": "Starbucks - HI - Waipahu  03696",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.39662113,
      "lng": -158.0317397
    },
    "title": "Starbucks - HI - Waipahu  03697",
    "icon": "img/starbucks.png"

  },
  {
    "position": {
      "lat": 21.403688,
      "lng": -158.006128
    },
    "title": "Starbucks - HI - Waipahu  03698",
    "icon": "img/starbucks.png"

  }
];


var div = document.getElementById("map_canvas");
var map = plugin.google.maps.Map.getMap(div, {
  'mapType': plugin.google.maps.MapTypeId.ROADMAP,
  'controls': {
    'compass': false,
    'myLocationButton': true,
    'indoorPicker': false,
    'zoom': false
  },
  'gestures': {
    'scroll': true,
    'tilt': false,
    'rotate': true,
    'zoom': true
  },
  'camera' : {
    target: {
      "lat": 20.8843,
      "lng": -156.4583
    },
    zoom: 8
  },
  'preferences': {
    'zoom': {
      'minZoom': 1,
      'maxZoom': 18
    },
    'building': false
  }
});

map.one(plugin.google.maps.event.MAP_READY, function() {
  map.set("parkMarker", false);

  var btn = document.getElementById("btn");
  btn.addEventListener("click", function(){
    if (!map.get("parkMarker")) {
      console.log("--->addMarkers");
      addMarkers(map, data, function(markers) {
        //markers[markers.length - 1].showInfoWindow();
        map.set("park", markers);
      });
    } else {
      console.log("--->removeMarkers");
      var markers = new plugin.google.maps.BaseArrayClass(map.get("park"));
      markers.forEach(function(marker, cb) {
        marker.remove(cb);
      }, function() {
        map.set("park", undefined);
      });
    }
    map.set("parkMarker", !map.get("parkMarker"));
  });
});

function addMarkers(map, data, callback) {
  var baseArray = new plugin.google.maps.BaseArrayClass(data);
  baseArray.map(function(markerOpts, cb) {
    map.addMarker(markerOpts, cb);
  }, callback);
}

wf9a5m75 avatar Aug 11 '17 02:08 wf9a5m75

Hi,

It strange i am using exactly same as that of yours still i am having same issue

var map = plugin.google.maps.Map.getMap(div, {
  'mapType': plugin.google.maps.MapTypeId.ROADMAP,
  'controls': {
    'compass': false,
    'myLocationButton': true,
    'indoorPicker': false,
    'zoom': false
  },
  'gestures': {
    'scroll': true,
    'tilt': false,
    'rotate': true,
    'zoom': true
  },
  'camera' : {
    target: {
      "lat": 20.8843,
      "lng": -156.4583
    },
    zoom: 8
  },
  'preferences': {
    'zoom': {
      'minZoom': 1,
      'maxZoom': 18
    },
    'building': false
  }
});

map.one(plugin.google.maps.event.MAP_READY, function() {
  map.set("parkMarker", false);
  $scope.togglePark = function(){
    if (!map.get("parkMarker")) {
      console.log("--->addMarkers");
      addMarkers(map, data, function(markers) {
        //markers[markers.length - 1].showInfoWindow();
        map.set("park", markers);
      });
    } else {
      console.log("--->removeMarkers");
      var markers = new plugin.google.maps.BaseArrayClass(map.get("park"));
      markers.forEach(function(marker, cb) {
        marker.remove(cb);
      }, function() {
        map.set("park", undefined);
      });
    }
    map.set("parkMarker", !map.get("parkMarker"));
    
  } 
});

function addMarkers(map, data, callback) {
  var baseArray = new plugin.google.maps.BaseArrayClass(data);
  baseArray.map(function(markerOpts, cb) {
    map.addMarker(markerOpts, cb);
  }, callback);
}

https://youtu.be/SxHDyTQStJc

shrijan avatar Aug 11 '17 03:08 shrijan

I am updating ionic 1 project at the moment.

shrijan avatar Aug 11 '17 03:08 shrijan

You should try the latest version at least.

This plugin is used by lots of people, not only you.

If marker.remove() does not work properly, I would get more issue reports at this time.

wf9a5m75 avatar Aug 11 '17 03:08 wf9a5m75

If the issue is not solved after reinstall the latest version, please share your project files on github or bitbucket. I will check it later.

wf9a5m75 avatar Aug 11 '17 03:08 wf9a5m75

ping

wf9a5m75 avatar Aug 13 '17 16:08 wf9a5m75

Sorry was bit busy . I have created the test project just testing remove marker. Issue is not resolved at the moment. Here is my project in bitbucket https://bitbucket.org/piyashrija/googlemapcordova Thanks for help

shrijan avatar Aug 14 '17 00:08 shrijan

Thank you for waiting. I just built your project and run it.

1632

I tapped 12 times.

wf9a5m75 avatar Aug 20 '17 21:08 wf9a5m75

Thanks

shrijan avatar Aug 21 '17 02:08 shrijan

Hi I have a question. I'm implementing a map interface where users can add pins to their current location.. However, the logic/check for removing markers has been failing on me. I would like to remove a marker if the user isn't at a location anymore, and I want to set a radius bound for the location. e.g: If a user drops a pin at the airport to show they are there, once they leave that location, that user's pin should be deleted from the map. Can anyone help me out with setting radius bounds using googlemaps api?

sadeajayi avatar Feb 19 '18 17:02 sadeajayi