【问题标题】:Google map marker title duplicate谷歌地图标记标题重复
【发布时间】:2019-05-12 01:00:45
【问题描述】:

我正在开发一个 Ionic 应用程序。我的应用程序是从 USGS 获取数据 json api 地震,然后在谷歌地图上设置坐标。我正在循环一个数组来创建标记。一切正常,但是当我单击任何图标标记时,我会得到重复的标题! .

export class HomePage implements OnInit {
  protected points: { lng: number, lat: number }[] = [];

  items: any
  pet: string = "Today";
  map: GoogleMap;
  mags: number;

  constructor(
    private http: HTTP) {

  }

  async ngOnInit() {

    this.getData()

  }

  async getData() {

   this.http.get(`https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson`, {}, {}).then(data => {

      this.items = JSON.parse(data.data)
      let earth = JSON.parse(data.data)

      console.log(this.items)

      for (let datas of earth['features']) {

        this.points.push({ lng: datas.geometry.coordinates[0], lat: datas.geometry.coordinates[1] });

        let mag = datas.properties.place
        let title = datas.properties.title

       /// Marker icon

        let dest = this.points.map((coords) => {
          return this.map.addMarker({
            position: coords,
            icon: this.mags_icons
            title : title
          }).then((marker: Marker) => {

            marker.on(GoogleMapsEvent.MARKER_CLICK).subscribe(() => {

            });

          });
        });




        this.map = GoogleMaps.create('map_canvas');
      }

    })
  }

}

【问题讨论】:

    标签: angular google-maps


    【解决方案1】:

    标记的实例化方式看起来不正确,因为每次对特征集合进行迭代时,都会重新创建预置标记(这就是我猜测 title 引用相同值的原因) .

    下面的例子演示了如何创建标记并设置title 来引用正确的特征操作:

    getData() {
        this.http
          .get(
            `https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson`,{},{}
          )
          .then(data => {
            let geojson = JSON.parse(data.data);
            for (let feature of geojson["features"]) {
              let markerProps = {
                title: feature.properties.title,
                position: {
                  lng: feature.geometry.coordinates[0],
                  lat: feature.geometry.coordinates[1]
                }
              };
    
              let marker = this.map.addMarker(markerProps).then(marker => {
                marker.on(GoogleMapsEvent.MARKER_CLICK).subscribe(() => {
                  //...
                });
              });
            }
          });
    }
    

    另一种选择是使用map.addMarkerSync 函数:

    let geojson = JSON.parse(data.data);
    for (let feature of geojson["features"]) {
        let markerProps = {
          title: feature.properties.title,
          position: {
              lng: feature.geometry.coordinates[0],
              lat: feature.geometry.coordinates[1]
          }
        };
    
        let marker = this.map.addMarkerSync(markerProps);
        marker.on(GoogleMapsEvent.MARKER_CLICK).subscribe(() => {
            //...
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-18
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      相关资源
      最近更新 更多