【问题标题】:The new marker has updated its location but old marker not remove in ionic native google map新标记已更新其位置,但旧标记未在离子原生谷歌地图中删除
【发布时间】:2021-09-25 20:00:28
【问题描述】:

标记正在更新到新位置并设置新标记,但旧位置标记的问题未删除。 marker.remove() 在此 if 条件下不起作用。我如何删除旧标记?请提供任何解决方案。这是我的代码 =>

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()
  }
})

}

【问题讨论】:

    标签: google-maps ionic-framework google-maps-markers marker ionic-native


    【解决方案1】:

    您的代码中有两个不同的标记:

    1. this.map.addMarker() 的结果;
    2. this.map.addMarkerSync() 的结果。

    你有一个嵌套结构,你丢失了一个标记。可能是您要删除的那个。尝试重命名至少一个变量以避免混淆。

    【讨论】:

    • 感谢您的回复。我使用addMarker() 事件解决了这个问题。此addMarkerSync() 事件无法与remove() 函数一起正常工作。
    猜你喜欢
    • 2019-03-07
    • 2018-06-06
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多