【问题标题】:Change marker position when data change数据更改时更改标记位置
【发布时间】:2022-01-06 17:00:55
【问题描述】:

我正在 vue js 项目上使用 google map api。 当我的数据更改更改标记位置更改位置而不刷新卡时,我有一个包含我想要的位置的数据表。 这是我的代码,我想添加更改标记位置的功能以及如何在我的组件中实现它。 这是我的代码:

      this.initMap(
        this.$refs.googleMaps as HTMLElement,
        this.points
      )
    }
  }

  initMarker(map: google.maps.Map, pList: points[]) {
    const bounds = new google.maps.LatLngBounds()
    pList.forEach(
      (pElement: pElement, index: number) => {

        const marker = new google.maps.Marker({
          position: {
            lat: pElement.geoPoint?.latitude as number,
            lng: pElement.geoPoint?.longitude as number
          },
          icon: { url: 'url' },
          map
        })

        const position = new google.maps.LatLng(
          pElement.geoPoint?.latitude,
          pElement.geoPoint?.longitude
        )

        marker.addListener('click', () => {
          map.panTo(position)
        })

        bounds.extend(position)
      }
    )

    map.fitBounds(bounds)
  }

  initMap(mapElement: HTMLElement, pList: points[]) {
    const gestureOption: GoogleMapsGestureHandlingOptions = 'cooperative'
    const mapProp: google.maps.MapOptions = {
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      gestureHandling: gestureOption,
      disableDefaultUI: true,
      clickableIcons: false,
      minZoom: 20
    }
    this.googleMaps = new google.maps.Map(mapElement, mapProp)
    this.initMarker(this.googleMaps, pList)
  }


  mounted() {
    this.renderMap(
      () =>
        this.initMap(
          this.$refs.googleMaps as HTMLElement,
          this.pList
        )
    )
  }

【问题讨论】:

    标签: javascript typescript vue.js google-maps


    【解决方案1】:

    这里我不知道哪些数据代表表格数据。但我建议您将计算属性与 setter 和 getter 一起用于您的情况。您已将 Marker 和 Table Data 都转换为计算属性以实现您想要的

    【讨论】:

      猜你喜欢
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      • 2017-03-26
      • 2014-09-19
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多