2

I have the following code, however I want to use the data returned by PlaceService(map).search(request) in an Angular '$scope.' variable.

Currently I have the callback function assigning a new array with the result data, however it is not accessible by the Angular $scope variable since Google Places is asynchronous.

There are two methods I can see, but I'm not quite sure how to execute them. From what I can see, I can either:

1) Access the search results in the findPlaces() function, without a callback

or

2) Pass the $scope variable into the callback function 'storeResults'

function findPlaces(addrLocation, $scope) {
// Geocoder already started

// prepare variables (filter)
var type = myType;
var radius = document.getElementById('radius').value;

// prepare request to Places
var request = {
    location: addrLocation,
    radius: radius,
    types: [type]
};

    // send request
    service = new google.maps.places.PlacesService(map);
    service.search(request, storeResults);

}

function storeResults(results, status) {
//Ensure something is found
if (status == google.maps.places.PlacesServiceStatus.OK) {
    //First we clear the list of previously found places
    clearResults();
    for (var i = 0; i <= results.length-1; i++) {
        setCREsults(results[i]);
       //alert(searchResults[i].name + ' ' + searchResults[i].address);
    }
}
else if (status == google.maps.places.PlacesServiceStatus.ZERO_RESULTS) {
    alert('Sorry, nothing is found');
}

}

Please let me know how I can use the search results in my Angular app.

Adam M Thompson
  • 438
  • 5
  • 21

0 Answers0