Agm Google Maps-如何将经纬度绑定到HTML

时间:2018-10-05 08:52:50

标签: google-maps binding angular-google-maps

我在绑定从AJAX响应中获得的纬度和经度参数时遇到问题。如果我在HTML中对纬度和经度进行硬编码,则效果很好。但是当我从响应传递数据时,它就不起作用了。

  <agm-map [latitude]="lat" [longitude]="lng" [zoom]="zoom">
                <agm-marker [latitude]="latitude" [longitude]="longitude"></agm-marker>
              </agm-map>

     ngOnInit() {
        this.getTransactionView(this.selectedTransaction);
      }

    getTransactionView(selectedTransaction): void {

        const resultArray = {
          'latitude': '51.525244',
          'longtitude': '-0.141186'
        };
        this.transactionResult = resultArray;
        this.latitude = this.transactionResult.latitude;
        this.longitude = this.transactionResult.longtitude;
       console.log(this.latitude);
      console.log(this.longitude);
// console.log prints the values, but after binding to HTML , it doesnt display the marker
      }
}

1 个答案:

答案 0 :(得分:2)

由于提供的string类型的值,很可能发生这种情况。

AgmMarker期望latitude类型的longitudenumber值,例如:

map.component.html:

<agm-map [latitude]="lat" [longitude]="lng">
    <agm-marker [latitude]="lat" [longitude]="lng"></agm-marker>
</agm-map>

map.component.ts:

export class MapComponent implements OnInit {
  lat: number;
  lng: number;
  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.initData();
  }


  initData(): void {
    this.http.get<{}>('./assets/data.json')
    .subscribe(result => {
      console.log(result);
      this.lat = parseFloat(result['lat']);
      this.lng = parseFloat(result['lng']);
    });
  }

}

Here is a demo供参考