我在谷歌地图和firebase数据库上工作,我想将我的位置保存在firebase数据库中,我想用@Output和eventEmitter传输数据,pickedLocation
有值this.locationPick
deosn't get value请帮助我。
@Component({
selector: 'app-location-picker',
templateUrl: './location-picker.component.html',
styleUrls: ['./location-picker.component.scss'],
})
export class LocationPickerComponent implements OnInit {
@Output() public locationPick = new EventEmitter<PlaceLocation>();
isLoading = false;
selectedLocationImage: string;
place: Place[];
constructor(private modalCtrl: ModalController, private http: HttpClient) { }
ngOnInit() {}
onPickLocation() {
this.modalCtrl.create({component: MapModalComponent}).then(modalEl => {
modalEl.onDidDismiss().then(modalData => {
if (!modalData.data) {
return;
}
const pickedLocation: PlaceLocation = {
lat: modalData.data.lat,
lng: modalData.data.lng,
address: null,
staticMapImageUrl: null
};
this.isLoading = true;
this.getAddress(modalData.data.lat, modalData.data.lng)
.pipe(switchMap(address => {
pickedLocation.address = address;
return of(this.getMapImage(pickedLocation.lat, pickedLocation.lng, 14));
})).subscribe(staticMapImageUrl => {
pickedLocation.staticMapImageUrl = staticMapImageUrl;
this.selectedLocationImage = staticMapImageUrl;
this.isLoading = false;
this.locationPick.emit(pickedLocation);
console.log(pickedLocation);
});
});
modalEl.present();
});
}
private getAddress(lat: number, lng: number) {
return this.http
.get<any>(
`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${environment.googleMapsAPIKey}`
)
.pipe(
map(geoData => {
if (!geoData || !geoData.results || geoData.results.length === 0) {
return null;
}
return geoData.results[0].formatted_address;
}));
}
private getMapImage(lat: number, lng: number, zoom: number) {
return `https://maps.googleapis.com/maps/api/staticmap?center=${lat},${lng}&zoom=${zoom}&size=500x300&maptype=roadmap
&markers=color:red%7Clabel:Place%7C${lat},${lng}
&key=${environment.googleMapsAPIKey}`;
}
}
console.log(pickedLocation);
有url,但console.log(this.locationPick);
说
EventEmitter {_isScalar: false, observers: Array(1), closed: false, isStopped: false, hasError: false, …}
closed: false
hasError: false
isStopped: false
observers: [Subscriber]
thrownError: null
__isAsync: false
_isScalar: false
__proto__: Subject
发布于 2019-05-05 17:08:52
在LocationPickerComponent
的父组件的HTML文件中添加以下代码
<app-location-picker (locationPick)="locationPicked($event)"></app-location-picker>
另外,在父组件的TS文件中接收以下值:
locationPicked(value) {
console.log(value)
}
要了解在Angular中组件之间共享数据的不同方式,请阅读下面的文章
https://angularfirebase.com/lessons/sharing-data-between-angular-components-four-methods/
https://stackoverflow.com/questions/55982180
复制相似问题