2017-05-28 64 views
0

我想使用我的提供者向Google Maps API發出請求並檢索一些結果,但由於沒有任何東西會返回,因此我遇到了麻煩想法我做錯了什麼。 我對供應商的方法,使一個請求,谷歌:無法使用IONIC 2框架從http請求獲取Google Maps API的結果

getClosestMovieTheater(): Observable<Place[]>{ 
    console.log("started request to google."); 
    let t = this.http 
    .get('https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&keyword=cruise&key='MY_API_KEY', { headers: this.getHeaders(), withCredentials: true}) 
    .map(mapAll); 

    return t; 
} 

private getHeaders(){ 
    let headers = new Headers(); 
    headers.append('Accept', 'application/json'); 
    return headers; 
} 

我刨根據我的位置檢索最近的電影院,但現在我與谷歌的例子測試它只是爲了看看它是否工作。 再就是映射結果的方法:

function toP(r:any): Place{ 
    let p = <Place>({ 
    lng: r.results.geometry.location.lng, 
    lat: r.results.geometry.location.lat 
}) 

    return p; 
} 

function mapAll(response:Response): Place[]{ return response.json().map(toP); }

任何人都可以請幫助?預先感謝您。

+0

你在哪兒叫'getClosestMovieTheater'?也顯示該代碼。 – Chrillewoodz

+0

其實我發現這個問題是因爲CORS ...問題是我認爲它被默認禁用localhost ...你知道任何解決方案,使其在Opera瀏覽器中啓用它嗎?謝謝 – Luki

回答

1

CORS頭文件在Google服務器上設置,因此您無法做任何事情。您應該使用地圖JavaScript API的地方庫,而不是客戶端代碼中的Web服務調用。

看看下面的文檔描述地圖的JavaScript API的地方庫:

https://developers.google.com/maps/documentation/javascript/places

商家信息庫還提供了類似於Web服務的搜索功能。

我希望這有助於!

+0

謝謝你的迴應。我其實只是安裝了Firefox瀏覽器,並安裝了擴展,使CORS的本地主機和東西開始工作:) – Luki

相關問題