【问题标题】:How to remove markers cluster from google map(Ionic4)如何从谷歌地图中删除标记簇(Ionic4)
【发布时间】:2019-06-10 02:43:58
【问题描述】:

如何从谷歌地图中删除标记簇(Ionic4)

部分代码流程:

import * as MarkerClusterer from 'node-js-marker-clusterer';

markerCluster:MarkerCluster;

        if (this.markerCluster) {
          this.markerCluster.clearMarkers();
        }


        this.markerCluster = this.map.addMarkerClusterSync({
          markers: this.locations,
          boundsDraw: false,
          icons: [
            {min: 2, max: 100, url: "./assets/icon/favicon.ico", anchor: 
          {x: 16, y: 16}}
          ]
        });

this.markerCluster.clearMarkers(); ←此代码无效

【问题讨论】:

    标签: ionic-framework markerclusterer


    【解决方案1】:

    这是删除谷歌地图上所有标记的简单方法,

    this.map.clear();
    

    githup 中的这个简单示例

    import { Component, OnInit } from '@angular/core';
    import {
      ToastController,
      Platform,
      LoadingController
    } from '@ionic/angular';
    import {
      GoogleMaps,
      GoogleMap,
      GoogleMapsEvent,
      Marker,
      GoogleMapsAnimation,
      MyLocation
    } from '@ionic-native/google-maps';
    
    @Component({
      selector: 'app-home',
      templateUrl: 'home.page.html',
      styleUrls: ['home.page.scss'],
    })
    export class HomePage implements OnInit {
    
      map: GoogleMap;
      loading: any;
    
      constructor(
        public loadingCtrl: LoadingController,
        public toastCtrl: ToastController,
        private platform: Platform) { }
    
      async ngOnInit() {
        // Since ngOnInit() is executed before `deviceready` event,
        // you have to wait the event.
        await this.platform.ready();
        await this.loadMap();
      }
    
      loadMap() {
        this.map = GoogleMaps.create('map_canvas', {
          camera: {
            target: {
              lat: 43.0741704,
              lng: -89.3809802
            },
            zoom: 18,
            tilt: 30
          }
        });
    
      }
    
      async onButtonClick() {
        this.map.clear();
    
        this.loading = await this.loadingCtrl.create({
          message: 'Please wait...'
        });
        await this.loading.present();
    
        // Get the location of you
        this.map.getMyLocation().then((location: MyLocation) => {
          this.loading.dismiss();
          console.log(JSON.stringify(location, null ,2));
    
          // Move the map camera to the location with animation
          this.map.animateCamera({
            target: location.latLng,
            zoom: 17,
            tilt: 30
          });
    
          // add a marker
          let marker: Marker = this.map.addMarkerSync({
            title: '@ionic-native/google-maps plugin!',
            snippet: 'This plugin is awesome!',
            position: location.latLng,
            animation: GoogleMapsAnimation.BOUNCE
          });
    
          // show the infoWindow
          marker.showInfoWindow();
    
          // If clicked it, display the alert
          marker.on(GoogleMapsEvent.MARKER_CLICK).subscribe(() => {
            this.showToast('clicked!');
          });
        })
        .catch(err => {
          this.loading.dismiss();
          this.showToast(err.error_message);
        });
      }
    
      async showToast(message: string) {
        let toast = await this.toastCtrl.create({
          message: message,
          duration: 2000,
          position: 'middle'
        });
    
        toast.present();
      }
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 2015-04-09
      • 2012-10-17
      • 2013-12-04
      • 1970-01-01
      • 2013-09-29
      • 2015-06-05
      相关资源
      最近更新 更多