How to add label to each marker angular google maps - Angular 2

你。 提交于 2019-12-11 16:34:16

问题


I am using Angular 2 for my project . I would like to know how to add label to the marker using angular google maps. I am not getting a solution to add a label using Angular 2. Below is my html code . I am retrieving the places using ngFor . I need to tag the name object to the marker . Basically , when i hover on a marker , it should show that name

    <div class="form-group">
        <input placeholder="search for location" autocorrect="off" autocapitalize="off" 
        spellcheck="off" type="text" class="form-control" #search [formControl]="searchControl">
      </div>
    <agm-map style="height: 600px" [latitude]="latitude" [longitude]="longitude">
        <agm-marker  *ngFor="let m of mapArrayList; let i = index" 
          [latitude]="m.latitude"
          [longitude]="m.longitude"
          label="m"
          (markerClick)="clickedMarker(m.id)"
         >

       </agm-marker>
    </agm-map>


mapArrayList:
0:
    address: XYZ
    name: ABC services Lts


ngOnInit() {
    this.latitude = 1.372611;
    this.longitude = 103.848076;
    this.mapArrayList = this.facilityListArray;
    console.log(this.mapArrayList);
     // set google maps defaults
     this.zoom = 40;

     // create search FormControl
     this.searchControl = new FormControl();

     // set current position
     this.setCurrentPosition();

     // load Places Autocomplete
    this.mapsAPILoader.load().then(() => {
      const autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, {
        componentRestrictions: {country: ['SG', 'IN']}
      });
      autocomplete.addListener('place_changed', () => {
        this.ngZone.run(() => {
          // get the place result
          const place: google.maps.places.PlaceResult = autocomplete.getPlace();

          // verify result
          if (place.geometry === undefined || place.geometry === null) {
            window.alert('No details available for input: \'' + place.name + '\'');
            return;
          }

          // set latitude, longitude and zoom
          this.latitude = place.geometry.location.lat();
          this.longitude = place.geometry.location.lng();
          this.zoom = 100;
        });
      });
    });
  }

回答1:


You can provide the label like this ...

<agm-map style="height: 600px" [latitude]="latitude" [longitude]="longitude">
        <agm-marker  *ngFor="let m of mapArrayList; let i = index" 
          [latitude]="m.latitude"
          [longitude]="m.longitude"
          [label]="m.label"
          (markerClick)="clickedMarker(m.id)"
         >

       </agm-marker>
    </agm-map>

Where variable m.label has a string value - usually a single uppercase character. (though you can use any string value)

In place of string value you can also use value of type MarkerLabel for providing more detailed info if you like that. Following is the structure of the MarkerLabel interface.

interface MarkerLabel {
  color: string;
  fontFamily: string;
  fontSize: string;
  fontWeight: string;
  text: string;
}


来源:https://stackoverflow.com/questions/50998913/how-to-add-label-to-each-marker-angular-google-maps-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!