2016-08-20 57 views
1

一个谷歌的地方API搜索我试图让使用谷歌的地方API的地方在我的控制器:如何做angularjs控制器

$http.get("https://maps.googleapis.com/maps/api/place/radarsearch/json?location=51.503186,-0.126446&radius=5000&type=museum&key=MY_KEY") 
    .then(function(response) { 
     $scope.results = response.data; 
     console.log($scope.results); 
    }); 

可惜,这是行不通的。我的console.log后,我得到以下错误。

XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/place/radarsearch/json?location=51.503…126446&radius=5000&type=museum&key=MY_KEY. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9004' is therefore not allowed access. 

顺便说一句,这不是唯一的本地主机,因为我已经尝试在一个活的服务器相同。 什么是谷歌angularjs的方式来实现这一目标?

+0

你有没有简单地在你的index.html中添加谷歌地图文件与所有的JavaScript文件? –

+0

是的,我的谷歌装载得很好。 –

+0

以及为什么它们在Google链接和键= MY_KEY的末尾没有数字。你不想在这里发布它? –

回答

1

的谷歌Places API的Web服务并不打算通过AJAX($http服务)使用,但你可以利用Places Library相反,这里是角例如:

angular.module('mapApp', []) 
 

 
    .controller('mapCtrl', ['$scope', '$http', function ($scope, $http) { 
 

 
     $scope.map = new google.maps.Map(document.getElementById('map'), { 
 
      center: { lat: 51.503186, lng: -0.126446 }, 
 
      zoom: 15 
 
     }); 
 

 
     $scope.infoWindow = new google.maps.InfoWindow(); 
 
     $scope.service = new google.maps.places.PlacesService($scope.map); 
 

 
     // The idle event is a debounced event, so we can query & listen without 
 
     // throwing too many requests at the server. 
 
     $scope.map.addListener('idle', function() { 
 
      var request = { 
 
       location: {lat: 51.503186, lng: -0.126446}, 
 
       radius: 5000, 
 
       type: ['museum'] 
 
      }; 
 
      $scope.service.radarSearch(request, $scope.processResults); 
 
     }); 
 

 
     $scope.processResults = function (results, status) { 
 
      if (status !== google.maps.places.PlacesServiceStatus.OK) { 
 
       console.error(status); 
 
       return; 
 
      } 
 
      for (var i = 0, result; result = results[i]; i++) { 
 
       $scope.addMarker(result); 
 
      } 
 
     }; 
 

 
     $scope.addMarker = function(place) { 
 
      var marker = new google.maps.Marker({ 
 
       map: $scope.map, 
 
       position: place.geometry.location, 
 
       icon: { 
 
        url: 'http://maps.gstatic.com/mapfiles/circle.png', 
 
        //anchor: new google.maps.Point(16, 16), 
 
        scaledSize: new google.maps.Size(20, 32) 
 
       } 
 
      }); 
 

 
      google.maps.event.addListener(marker, 'click', function() { 
 
       $scope.service.getDetails(place, function (result, status) { 
 
        if (status !== google.maps.places.PlacesServiceStatus.OK) { 
 
         console.error(status); 
 
         return; 
 
        } 
 
        $scope.infoWindow.setContent(result.name); 
 
        $scope.infoWindow.open($scope.map, marker); 
 
       }); 
 
      }); 
 
     } 
 

 

 
    }]);
#map { 
 
     height: 420px; 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places,visualization"></script> 
 
<script src="https://code.angularjs.org/1.3.15/angular.js"></script> 
 

 
<div ng-app="mapApp" ng-controller="mapCtrl" > 
 
    <div id="map"></div> 
 
</div>