jtbitt
jtbitt

Reputation: 581

How can I set the data from inside a Google Maps API call to a variable?

I'm doing a Google Maps API call to the Places library, with the goal of extracting business data and using it visually in my Angular application. So far, I'm successfully pulling back the data from Google.

However, I'm having a hard time assigning the 'place' data that's retrieved from the API call to a variable outside of the getGoogleData() function.

I've been trying to use 'return' and '.then' but I haven't been able to get it to work. Here is the code.

function getGoogleData(){
    var service = new google.maps.places.PlacesService(map);
    var place = { placeId: 'ChIJr4vVw8-kwoAR0iEipIyAZWw' };

    service.getDetails(place, function(place, status) {
            if (status === google.maps.places.PlacesServiceStatus.OK) {
                if(place) {
                    console.log(place);
                }
            }
        }
    );
}
getGoogleData();

Upvotes: 0

Views: 80

Answers (1)

Abdullah Al Noman
Abdullah Al Noman

Reputation: 2878

function getGoogleData(){
    var service = new google.maps.places.PlacesService(map);
    var place = { placeId: 'ChIJr4vVw8-kwoAR0iEipIyAZWw' };

    service.getDetails(place, function(place, status) {
            if (status === google.maps.places.PlacesServiceStatus.OK) {
                if(place) {
                    console.log(place);
                    $scope.$apply();
                }
            }
        }
    );
}

add $scope.$apply() to your function .

Upvotes: 3

Related Questions