2017-05-28 24 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

相关问题