Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The new marker has updated its location but old marker not remove in ionic native google map #363

Open
GhoshWorld opened this issue Jul 17, 2021 · 1 comment

Comments

@GhoshWorld
Copy link

GhoshWorld commented Jul 17, 2021

The marker is updating to new position and set a new marker but problem with old position marker not remove. marker.remove() not work under this if condition. How I remove old marker? any solution please. here is my code =>

loadMap() {

this.map = GoogleMaps.create('map_canvas', {
  controls: {
    myLocationButton : true,
    myLocation : true
  },
  camera: {
    target: {
      lat: this.latitude, lng: this.longitude
    },
    zoom: 18,
    tilt: 30
  },
});

this.map.addMarker({
  position: { lat: this.latitude, lng: this.longitude },
  draggable: true,
  disableAutoPan: true,
  icon: 'blue',
  title: 'Avirup'
}).then((marker: Marker) => {
  marker.showInfoWindow();

const subscription = this.geolocation.watchPosition().subscribe(position => {
  let geoposition = (position as Geoposition);
  let latitude = geoposition.coords.latitude;
  let longitude = geoposition.coords.longitude;
  let marker : Marker
    marker = this.map.addMarkerSync({
    position: { lat: latitude, lng: longitude },
    draggable: true,
    disableAutoPan: true,
    icon: { url: './assets/image/addresspin.png' ,
    size: {
        width: 30 ,
        height: 30
    }},
    title: 'Move'
  })
  marker.showInfoWindow();
   if(marker.getPosition().lat == latitude && marker.getPosition().lng == longitude){
    marker.remove()
  }
})
} ```
@GhoshWorld
Copy link
Author

This work only change some code. We can change addMarkerSync to addMarker event. ::

watchPosition() {

 this.geolocation.watchPosition().subscribe(position => {
  let geoposition = (position as Geoposition);
  let latitude = geoposition.coords.latitude;
  let longitude = geoposition.coords.longitude;
  this.map.addMarker({
    position: { lat: latitude, lng: longitude },
    draggable: true,
    disableAutoPan: true,
    icon: {
      url: './assets/image/addresspin.png',
      size: {
        width: 30,
        height: 30
      }
    },
    title: 'Avirup Move'

  }).then((marker: Marker) => {

    if (marker) {
      setTimeout(() => {
        marker.remove()
      }, 1000)
    } else {
      console.log("marker", marker)
      marker.showInfoWindow();
      marker.on(GoogleMapsEvent.MARKER_CLICK).subscribe(() => {
        console.log("markeron")
      });
      marker.trigger(GoogleMapsEvent.MARKER_CLICK);
    }
  });
})

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant