[英]Get distance from google maps API in Angular service?
我正在嘗試運行JS版本的Google Maps API,因為常規API給了我一個CORS問題。 我試圖找到兩個目的地之間的距離和時間。 如何從創建的服務中取回數據?
我的服務看起來像這樣。
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Injectable()
export class GMapsService {
constructor(private http: Http) { }
getData() {
return //the data back.
}
}
https://jsfiddle.net/65nuyn4b此jsfiddle鏈接顯示了如何完成它。 但這只是普通的JS,我需要從服務中運行它,所以我很困惑如何包含鏈接中顯示的script標簽,以及如何將所有內容返回到我要訂閱的任何組件中此功能。 我有想念嗎? 我是Angular的新手。
如果您的getData()
函數返回可觀察的數據,例如return this.http.get("some url");
那么您可以將數據取回另一個組件中:
import {GMapsService} from './serviceFileName';
export class YourComponent {
// inject your service
constructor(private myService: GMapsService) {
}
aFunction() {
this.myService.getData().subscribe((data: any) => {
//process the returned data;
}, err => {
console.error('GMapsService getData() fail');
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.