【发布时间】:2021-03-30 20:56:22
【问题描述】:
我正在尝试使用他们的 API 获取 Google Places 并获取包含“photo_reference”属性的响应正文。有了这个属性,我想获取 Google Place Image 并调用第二个 API。
我有一个可以搜索地点的输入字段。通过输入字符串并单击搜索按钮,将调用此方法:
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
private lng: number;
private lat: number;
private place_id: string;
private listOfPoi: GoogleLocation[];
constructor(
private gs: GoogleService,
private ds: DataService,
private router: Router
) { }
ngOnInit(): void {
this.ds.getLocation().subscribe(loc => this.listOfPoi = loc);
}
searchPoi(location: string): void {
this.router.navigate(['/poi']);
this.gs.getPoi(location).pipe(
map((response) => response)
).subscribe((data: GoogleResponse) => {
if(data.status === "OK") {
this.ds.setLocation(data.results); //data.results is an array of locations
}
});
}
在我的另一个组件中,我尝试获取位置并将它们映射到具有属性 imgUrl 的新对象中。 我想将它们保存在一个数组中。
export class SightseeingViewComponent implements OnInit {
listOfPoi: LocationWithImage[];
constructor(private ds: DataService, private gs: GoogleService) {}
ngOnInit(): void {
this.ds.getLocation().subscribe(
(loc) =>
(this.listOfPoi = loc.map((loc) => {
return {
...loc,
imgUrl:
this.gs.getImage(loc.photos[0].photo_reference).toString(),
};
}))
);
}
}
但我的问题是 img src="[object Object]" 它应该是字符串!
我的问题是这一行this.gs.getImage(loc.photos[0].photo_reference).toString()
我试图用一个随机硬编码的 img Url 替换这一行,它可以工作。图像已显示,但我无法使用此方法将 URL 作为字符串检索。
当然 toString() 不起作用,但我不知道我还能做什么?
这是谷歌定位模型:
//The model of a google location object from the API response (getLocationInfo)
export class GoogleResponse {
constructor(
public results: GoogleLocation[],
public status: string
) {}
}
export class GoogleLocation {
constructor(
public formatted_address: string,
public geometry: Locations,
public icon: string,
public name: string,
public photos: PhotoInfo[],
public place_id: string,
public reference: string,
public type: string[]
) {}
}
export interface LocationWithImage extends GoogleLocation {
imgUrl?: string;
}
...
编辑:
这是我的服务: 有了这个,我调用了我的后端,它通过其 API 从 Google 获取 photo_reference
@Injectable({
providedIn: 'root',
})
export class GoogleService {
url: string = 'http://localhost:3000/';
constructor(private http: HttpClient) {}
getLocationInfo(location: string): Observable<GoogleResponse> {
console.log(location);
const headers = new HttpHeaders({
'Content-Type': 'application/json',
});
return this.http.post<GoogleResponse>(
this.url + 'googleplace/',
{ name: location },
{ headers: headers }
);
}
getPoi(location: string): Observable<GoogleResponse> {
console.log(location);
const headers = new HttpHeaders({
'Content-Type': 'application/json',
});
return this.http.post<GoogleResponse>(
this.url + 'googlepoi/',
{ name: location },
{ headers: headers }
);
}
getImage(photoRef: string): Observable<string> {
console.log(photoRef);
const headers = new HttpHeaders({
'Content-Type': 'application/json',
});
return this.http.post<string>(
this.url + 'googlephoto/',
{ photoRef: photoRef },
{ headers: headers }
);
}
}
这是我从其他组件发送和检索数据所需的其他服务
@Injectable({
providedIn: 'root'
})
export class DataService {
private googleLocationSource = new Subject<GoogleLocation[]>();
constructor() { }
public getLocation(): Observable<GoogleLocation[]> {
return this.googleLocationSource.asObservable();
}
public setLocation(gl: GoogleLocation[]) {
return this.googleLocationSource.next(gl);
}
【问题讨论】:
-
大概 getImage 返回一个 observable 数据,不清楚为什么您认为 toString 会神奇地使其同步。请给minimal reproducible example。
-
是的,我知道...我怎样才能得到它的字符串值?我尝试了 pipe、forkjoin、map、switchmap 等,但没有工作.. 好的!我提供了更多信息,这有帮助吗?
-
目前还不清楚您尝试了什么,我希望
forkJoin可以工作(假设您想扇出多个可观察对象然后获得所有结果)。
标签: javascript angular typescript api rxjs