Assign ID to marker in leaflet

后端 未结 7 1033
忘了有多久
忘了有多久 2020-12-23 11:47

So i try to achieve a result as on foursquare: https://foursquare.com/explore?cat=drinks&mode=url&near=Paris which is when you clik on a marker on the map, it scrol

相关标签:
7条回答
  • 1.) Lets create Marker with unique id...

    L.marker([marker.lat, marker.lng],{customID:'some ID',title:marker.title}).on('click', this.markerClick).addTo(mymap);
    

    2.) Go to node_modules@types\leaflet\index.d.ts and add customID?:string;

    export interface MarkerOptions extends InteractiveLayerOptions {
        icon?: Icon | DivIcon;
        title?: string;
    
        ....
    
        autoPanSpeed?: number;
        customID:string;
    }
    

    3.) In the same file add customID to LeafletMouseEvent

    export interface LeafletMouseEvent extends LeafletEvent {
      latlng: LatLng;
      layerPoint: Point;
      containerPoint: Point;
      originalEvent: MouseEvent;
      customID:customID
    }
    

    4.) Create customID class

    export class customID {
      constructor(customID: string);
      customID: number;
    } 
    

    5.) Get your marker id in function

    markerClick(e){
      console.log(e.sourceTarget.options.customID)
    }
    
    0 讨论(0)
提交回复
热议问题