I am writing an application for finding near by resturants & other attractions using the Google Maps & Places API. I was using google places search function to help obtain information
var service = new google.maps.places.PlacesService(map);
service.search(request, callback);
but I then realised that it would not return values I needed like formatted address for the markers that I created using my search. Now what I am confused by is to get this extra information I need to use the getDetails()
function. Should this replace the search function that I used above or should it be placed some time after that? When google describes it on their website it looks like it should just replace the search function because in the example it takes the same exact parameters and runs just the same as the search function yet if I do that, then it simply doesn't return any places at all.
Here is a bit of my code to help explain what I am trying to acplish.
//Function for finding destination types. Receives destination type as a string.
function findDestinationType(where)
{
request = null;
var request =
{
location: point,
radius: 2500,
types: [where]
};
var service = new google.maps.places.PlacesService(map);
service.getDetails(request, callback);
}
//Call Back to fill array with markers & locations
function callback(results, status)
{
if (status == google.maps.places.PlacesServiceStatus.OK)
{
initialize();
iterator = 0;
markerArr = [];
for (var i = 0; i < results.length; i++)
{
markerArr.push(results[i].geometry.location);
result = results[i];
createMarker(results[i]);
}
}
else
{
alert("Sorry, there are no locations in your area");
}
}
//Function to create marker
function createMarker(place)
{
var marker = new google.maps.Marker(
{
position: markerArr[iterator],
map: map,
draggable: false,
animation: google.maps.Animation.DROP
})
//alert(markerArr[iterator]);
markersA.push(marker);
iterator++;
google.maps.event.addListener(marker, 'click', function()
{
console.log('clicked');
infowindow.setContent(place.name);
infowindow.open(map, this);
//directionsDisplay.setMap(map);
});
}
I am writing an application for finding near by resturants & other attractions using the Google Maps & Places API. I was using google places search function to help obtain information
var service = new google.maps.places.PlacesService(map);
service.search(request, callback);
but I then realised that it would not return values I needed like formatted address for the markers that I created using my search. Now what I am confused by is to get this extra information I need to use the getDetails()
function. Should this replace the search function that I used above or should it be placed some time after that? When google describes it on their website it looks like it should just replace the search function because in the example it takes the same exact parameters and runs just the same as the search function yet if I do that, then it simply doesn't return any places at all.
Here is a bit of my code to help explain what I am trying to acplish.
//Function for finding destination types. Receives destination type as a string.
function findDestinationType(where)
{
request = null;
var request =
{
location: point,
radius: 2500,
types: [where]
};
var service = new google.maps.places.PlacesService(map);
service.getDetails(request, callback);
}
//Call Back to fill array with markers & locations
function callback(results, status)
{
if (status == google.maps.places.PlacesServiceStatus.OK)
{
initialize();
iterator = 0;
markerArr = [];
for (var i = 0; i < results.length; i++)
{
markerArr.push(results[i].geometry.location);
result = results[i];
createMarker(results[i]);
}
}
else
{
alert("Sorry, there are no locations in your area");
}
}
//Function to create marker
function createMarker(place)
{
var marker = new google.maps.Marker(
{
position: markerArr[iterator],
map: map,
draggable: false,
animation: google.maps.Animation.DROP
})
//alert(markerArr[iterator]);
markersA.push(marker);
iterator++;
google.maps.event.addListener(marker, 'click', function()
{
console.log('clicked');
infowindow.setContent(place.name);
infowindow.open(map, this);
//directionsDisplay.setMap(map);
});
}
Share
Improve this question
asked Aug 7, 2012 at 13:49
NolskiNolski
4434 gold badges8 silver badges24 bronze badges
1 Answer
Reset to default 4Your assumption is wrong, getDetails() expects not the same parameters, it expects a reference to a place.
The reference you will get as a result for a places-search, it's a token.
So the workflow is when you search for a place:
- use
places.search()
for basic informations about the place - when you need more informations use
places.getDetails()
with the reference from step 1. as argument