Angular 2 agm library for google maps setting place by place id

后端 未结 1 1677
-上瘾入骨i
-上瘾入骨i 2021-01-11 21:20

I am implementing in a page a google map where a place id is passed to me, I then need to obtain such place id and load the map with that marker. I was going through the doc

相关标签:
1条回答
  • 2021-01-11 22:26

    You can use mapReady output property of agm-map. Change your html to

    <agm-map [latitude]="latitude" [longitude]="longitude" [scrollwheel]="false" [zoom]="zoom" (mapReady)="mapReady($event)">
          <agm-marker [latitude]="latitude" [longitude]="longitude"></agm-marker>
    </agm-map>
    

    and define following functions in your component. This function will be called when your map is ready.

    mapReady($event: any) { 
      // here $event will be of type google.maps.Map 
      // and you can put your logic here to get lat lng for marker. I have just put a sample code. You can refactor it the way you want.
      this.getLatLong('ChIJN1t_tDeuEmsRUsoyG83frY4', $event, null);
    }
    
    getLatLong(placeid: string, map: any, fn) {
        let placeService = new google.maps.places.PlacesService(map);
        placeService.getDetails({
          placeId: placeid
          }, function (result, status) {
            console.log(result.geometry.location.lat());
            console.log(result.geometry.location.lng())
          });
      }
    

    Change / refactor this sample code according to your need such that you are able set values to latitude and longitude parameters that you are passing to html.

    <agm-marker [latitude]="latitude" [longitude]="longitude"></agm-marker>
    
    0 讨论(0)
提交回复
热议问题