1

I am trying to get places using google place api in my controller:

$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);
    });

Unfortunately this doesn't work. After my console.log, I get the following error.

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.

BTW this is not unique to localhost as I have tried the same in a live server. What is the google angularjs way to achieve this?

LearnToday
  • 2,762
  • 9
  • 38
  • 67

1 Answers1

1

The Google Places API Web Service is not intended to be used via ajax ($http service), but you could utilize Places Library instead, here is Angular example:

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>
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193