【发布时间】:2017-03-12 11:11:03
【问题描述】:
Angular 新手,尝试使用 Google Maps / Places API 设置一个非常简单的应用程序。
我可以通过以下方式成功使用地理定位服务:
.controller('MainCtrl', [
'$scope',
'$http',
function($scope, $http){
$scope.userLocation = [];
$scope.currentLocation = function() {
$http({
method: 'GET',
url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=xxx'
}).then(function successCallback(response) {
$scope.userLocation.push(response);
});
}
}])
问题: 当我尝试向他们的 Places API 发出类似的 GET 请求时 - 我收到了 CORS 错误:
XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=36.23…5.1458888&type=bar&key=xxx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://0.0.0.0:3000' is therefore not allowed access.
问题:我是否能够从 Angular 发出这些类型的请求,或者我是否需要设置后端来代替调用?
我尝试使用 JSONP 作为方法,它通过了 CORS 问题,但是我遇到了无法访问数据的问题。不确定它是否是无效的 JSON ......但它说 Unexpected identifier :,当我查看源代码时,它对我来说看起来像是非常好的数据:
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 36.2388477,
"lng" : -115.1540073
},
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "578bfba1f17b0c5fbdafd5b71600b50d3e95c1ca",
"name" : "Ruby Tuesday",
"opening_hours" : {
"open_now" : true,
"weekday_text" : []
},
【问题讨论】:
标签: angularjs json google-maps google-maps-api-3