我想从我的angular2应用程序中的地点ID中检索谷歌地方照片。我从自动填充方法获得了placeId。
我正在使用angular2-google-map库。
import { Component, NgZone, Inject, OnInit, ViewChild, EventEmitter, ElementRef } from '@angular/core';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { AgmCoreModule, MapsAPILoader } from 'angular2-google-maps/core';
declare var google: any;
@Component({
selector: 'add-location-component',
templateUrl: './addLocation.component.html'
})
export class AddLocationComponent implements OnInit {
private googlePlaceId: any;
@ViewChild("search")
public searchElementRef: ElementRef;
public searchControl: FormControl;
constructor(
public authService: AuthService,
private mapsAPILoader: MapsAPILoader,
@Inject(NgZone) private ngZone: NgZone
) { }
ngOnInit() {
this.searchControl = new FormControl();
this.mapsAPILoader.load().then(() => {
let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, {
types: ["address"]
});
autocomplete.addListener("place_changed", () => {
this.ngZone.run(() => {
let place = autocomplete.getPlace();
if (place.geometry === undefined || place.geometry === null) {
return;
}
this.googlePlaceId = place.place_id;
console.log(this.googlePlaceId);
});
});
});
}
}
通过使用place_id或lattitude和longitude我想获取该地方的前10张照片。我被困在了angular2-google-map。
你能帮助我吗? 感谢。答案 0 :(得分:2)
使用此placeId,您将使用PlacesService类。您拨打getDetails
方法并在回调中收到PlaceResult
个photos
属性(该数组为PhotoPlace
,您可以在其上调用getUrl
检索可显示图片的方法。)
(这意味着您已检索到Place库以及核心Google地图API库)
function RetrievePhotoUrlsFromPlaceId(placeId) {
/* Instantiate a placesService */
var placesService = new google.maps.places.PlacesService();
/* Get place details */
placesServices.getDetails({
placeId: placeId
}, (placeResult, status) => {
if(status === 'OK') {
var photos = placeResult.photos;
var urls = []; // we will store the urls here
photos.forEach((photo) => {
urls.push(photo.getUrl({
maxWidht: 500, // at least set one or the other - mandatory
maxHeight: undefined
}));
});
/* You have your URLs here !! */
}
});
}
[编辑]
实际上,由于您使用Autocomplete
因此已经有PlaceResult
,因此更加简单。
function RetrievePhotoUrlsFromPlace(place) {
var photos = place.photos;
var urls = []; // we will store the urls here
photos.forEach((photo) => {
urls.push(photo.getUrl({
maxWidht: 500, // at least set one or the other - mandatory
maxHeight: undefined
}));
});
/* You have your URLs here !! */
}