javascript - 可以在 Angular 5 中发出 http get 请求之前发生 Google Map API 调用
问题描述
我正在尝试使用 URL 参数来执行 GET 请求,然后使用 JSON 输出来支持对 Google Maps 的查询。到目前为止,我已经弄清楚了如何通过清理和传递静态数据来使嵌入式地图工作。但是,在构建 URL 后完成对查询的调用,因此结果为“未定义”。
@Component({
selector: 'app-contactdetail',
templateUrl: './contactdetail.component.html',
styleUrls: ['./contactdetail.component.css']
})
export class ContactdetailComponent implements OnInit {
contactId: string;
contactDetail: Object = { };
lat: string;
lng: string;
queryBuilder = `&q=${this.lat},${this.lng}`;
apiLink = `https://www.google.com/maps/embed/v1/place?key=`;
// `&q=321+Fake+Street,Mouton+ME`
apiKey = `lol`;
builtLink = `${this.apiLink}${this.apiKey}${this.queryBuilder}`;
url: string = this.builtLink;
urlSafe: SafeResourceUrl =
this.sanitizer.bypassSecurityTrustResourceUrl(this.url);;
constructor(private route: ActivatedRoute,
private http: Http,
private sanitizer: DomSanitizer
) {}
ngOnInit() {
this.contactId = this.route.snapshot.params.id;
this.getContactDetail(this.contactId);
console.log(this.urlSafe);
console.log(this.builtLink);
}
// TODO: make this pass in an option eventually.
/* buildQuery(){
const address = `&q=${this.contactDetail.address.split('
').join('+')},`;
const city = ``
} */
async buildURL(url) {
const sanitized =
this.sanitizer.bypassSecurityTrustResourceUrl(url);
return sanitized;
}
async getContactDetail(contactId) {
return this.http
.request(`http://localhost:3000/contacts/${contactId}`)
.subscribe((res: any) => {
this.contactDetail = res.json();
if (this.contactDetail.hasOwnProperty('geoLocation_lat')) {
this.lat = this.contactDetail['geoLocation_lat'];
console.log(this.lat);
}
if (this.contactDetail.hasOwnProperty('geoLocation_lng')) {
this.lng = this.contactDetail['geoLocation_lng'];
console.log(this.lng);
}
console.log(this.contactDetail);
});
}
}
以及我正在尝试的 HTML 代码:
</div>
<iframe
width="600"
height="450"
frameborder="0"
style="border:0"
[src]="urlSafe">
</iframe>
</div>
我对 Web Dev 非常陌生,因此我们将不胜感激。谢谢你。
解决方案
我没有看到谷歌地图查询是如何被触发的,但只要在 .getContactDetails 完成后正确触发,那么在我看来你需要在订阅中定义 URL。换句话说,
queryBuilder = `&q=${this.lat},${this.lng}`;
apiLink = `https://www.google.com/maps/embed/v1/place?key=`;
// `&q=321+Fake+Street,Mouton+ME`
apiKey = `lol`;
builtLink = `${this.apiLink}${this.apiKey}${this.queryBuilder}`;
不是动态的。它只是使用当前定义的 .lat 和 .lng 值,这些值是未定义的。
更改.getContactDetail
为
async getContactDetail(contactId) {
// ...http request
.subscribe(res => {
// ...parse .lat and .lng; handle parsing errors
this.queryBuilder = `&q=${this.lat},${this.lng}`;
this.apiLink = `https://www.google.com/maps/embed/v1/place?key=`;
this.builtLink = `${this.apiLink}${this.apiKey}${this.queryBuilder}`;
});
}
您应该正确构建 URL。
推荐阅读
- java - Spring Boot - 使用 TestRestTemplate 在 testi 期间类的空字段
- html - 能
在里面 ? - python - Popen 不将标准输出写入文件
- kubernetes - 在 Kubernetes for mysql 中使用 hostpath 和 glusterfs 挂载目录问题
- flutter - 将变换应用于小部件而不重绘小部件树
- c# - C# SQLite vs DataTable:“指定的转换无效。”
- javascript - NGINX 部署的 socket.io 服务器的 Socket.io 客户端问题
- css - 更改 Flexdashboard 中值框的高度
- java - RestAssuredMockMvc 请求属性未作为 ArrayList 传递
- sql - 如何防止红移中的锁定。(共享锁停止写作业)