我想使用包含lat / lng坐标的URL参数从AngularFire的Firestore中获取get
的特定文档,然后将这些坐标绑定到google-map组件的[center]属性以根据查看的项目动态显示地图。但是数据未连接到google map属性,因此默认情况下以Google HQ位置为中心。
主组件List
在URL hike/:hikeId
中传递事件的ID,我提取了该事件,然后使用AngularFire来获取文档。
hike-detail.component.html
<h1>Hike Detail</h1>
<h3>Name: <span>{{ (hikeInfo | async)?.name }}</span></h3> // Displays Hike A
<google-map
[center]="hikeInfo.center | async"
width="100%">
</google-map>
hike-detail.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { stringify } from 'querystring';
import { map } from 'rxjs/operators';
@Component({
selector: 'app-hike-detail',
templateUrl: './hike-detail.component.html',
styleUrls: ['./hike-detail.component.css']
})
export class HikeDetailComponent implements OnInit {
hikeId;
hikeInfo;
center: google.maps.LatLngLiteral;
constructor(
private route: ActivatedRoute,
private location: Location,
private router: Router,
private firestore: AngularFirestore
) { }
ngOnInit(): void {
this.hikeId = this.route.snapshot.paramMap.get('id');
this.hikeInfo = this.firestore.collection('hikes').doc(this.hikeId).valueChanges();
}
}
数据库
firestore
-- hikes
-- key
-- name: Hike A
-- center: {
lat: 28.12,
lng: 32.71
}
当前显示名称和以通用Google HQ为中心的google地图,也可以使用[center]绑定。
this.hikeInfo = this.firestore.collection('hikes').doc(this.hikeId).valueChanges();
this.hikeInfo.subscribe(items => {
console.log(items);
this.center = items.center;
});
然后您可以在html中执行:
<google-map
[center]="center"
width="100%">
</google-map>