I followed the code example for the findPlaceFromQuery() method from https://developers.google.com/maps/documentation/javascript/places#find_place_from_query.
I have the exact code for the query part. However, the PlacesServiceStatus returned is always not OK. I tried changing the query value in the request object but the result is always the same.
Any idea why this is happening?
I'm pasting the relevant code snippet below for easy reference. It's mostly the same as the sample code. I just added the else clause at the end:
var map;
var service;
var infowindow;
function initMap() {
var mapCenter = new google.maps.LatLng(-33.8617374,151.2021291);
map = new google.maps.Map(document.getElementById('map'), {
center: mapCenter,
zoom: 15
});
var request = {
query: 'Museum of Contemporary Art Australia',
fields: ['photos', 'formatted_address', 'name', 'rating', 'opening_hours', 'geometry'],
service = new google.maps.places.PlacesService(map);
service.findPlaceFromQuery(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
else{
alert("status bad");
}
}
A couple of prerequests that people need to be aware of for this code to work. For the places API request to work the following action must be performed:
The places library must be added explicity from the calling method like so &libraries=places. This is because the places API doesn't come as standard with the maps API.
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY& libraries=places&callback=YOUR_CALLBACK_METHOD"></script>
The API key used must have the right places credentials from the google console. This link exaplains how this can be done.
Related
Question: How can I get the ~same JSON response that I get when I call Google Places server-side API /textsearch/ in my browser, but using Google Places JS Library instead?
Here is the server-side Google Places API /textsearch/
https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+pyrmont&key=AIzaSyAXPJDWXhzlAY0N8mMUcgFJ6yNSEd0vMg8
Here is the sample code:
I've tried to console.log() the JSON, but I can't get any JSON to log to the console.
var map;
var service;
var infowindow;
function initialize() {
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
map = new google.maps.Map(document.getElementById('map'), {
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: '500',
query: 'restaurant'
};
service = new google.maps.places.PlacesService(map);
service.textSearch(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
console.log(place.id)
}
}
Here's the documentation in case it's helpful: https://developers.google.com/maps/documentation/javascript/places#TextSearchRequests
Thank you for any help you can provide!
I have been sifting through documentation unsuccessfully and need help. I need to access the first value in Google places autocomplete or geocode the lat lng coords I have in Google Places. I am auto populating the field with say "Palo Alto" and need Google Places' first suggestion for an auto complete of that city. I have tried using the Google Geocode feature but the results are different than the Places API and therefore useless to me. Example places outputs Palo Alto, CA and the geocode feature outputs Palo Alto, CA USA. I have tried hacking it together by reverse geocoding the lat lng splitting the string above to only say Palo Alto and populating an input field that is hooked into a Places autocomplete, but I can't get it to trigger. I have a script written to pull the output from the autocomplete results; I just need to trigger it. I have tried using $('myCity').trigger('keypress'); once I have inserted Palo Alto into the input field to no avail. All suggestions on what steps to take next are welcome. I have also looked into the following code, but I want to do it without the map and i get a javascript error when i remove it. "TypeError: a is undefined [Break On This Error] (66 out of range 46)"
var lat = -130;
var lng = 37;
var request = {
location: new google.maps.LatLng(lat, lng);
};
var service = new google.maps.places.PlacesService(map);
service.search(request, function (results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
console.log(results[i])
}
}
});
Thanks for your help.
You need to perform a Text Search.
Go to Places Library and go down to Text Search Requests.
Heres an example:
var map;
var service;
var infowindow;
function initialize() {
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: '500',
query: 'restaurant'
};
service = new google.maps.places.PlacesService(map);
service.textSearch(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
}
Note: In your case change the request to this:
var request = {
query: 'Palo Alto'
};
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 accomplish.
//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);
});
}
Your 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
I need the list of locations (areas/subareas) in a particular city.
I thought, google api might be a possible option to fetch this. if i pass city's lat/long and radius then there must be some way to fetch the list of locations.
however i could not find any possible solution?
Can any one help me out?
Yeah, Google Maps JavaScript API V3 can be a good solution for it. If you check this website, there is a similar example of it:
var map;
var service;
var infowindow;
function initialize() {
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: pyrmont,
zoom: 15
});
var request = {
location: pyrmont,
radius: '500',
types: ['store']
};
service = new google.maps.places.PlacesService(map);
service.search(request, callback);
}
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
}
There it finds stores in a specific radius from Pyrmont. You can take a look at it documentation, change the type parameter (see this list) for whatever you want and get its result in that callback function. After that, you could use those cities' info for your purpose.
When calling search service api
var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316);
var request = { location: pyrmont, radius: '500', types: ['store'] };
service = new google.maps.places.PlacesService(map);
service.search(request, callback);
is it necessary to give the location/radius of the search area?
What about if i want to search a location in total world not in specified area like google does at http://maps.google.com/.
--- UPDATE ---
This is my updated code as #Trott advised but i am getting status = "ZERO_RESULTS" in my callback function.
var keyword='search placename';
var request = { name : keyword,
bounds : new google.maps.LatLngBounds(
google.maps.LatLng(-89.999999,-179.999999),
google.maps.LatLng(89.999999,179.999999)
)};
service = new google.maps.places.PlacesService(map);
service.search(request, callback);
function callback(results, status)
{
console.log("status="+status);
if ( status == google.maps.places.PlacesServiceStatus.OK)
{
for (var i = 0; i < results.length; i++)
console.log(results[i]);
}
}
What i am doing wrong?
Calls to search() must contain either a location and a radius, or else a bounds (as a LatLngBounds object). This is documented at http://code.google.com/apis/maps/documentation/javascript/places.html#place_search_requests.
You could see what happens if you specify a LatLngBnds that covers the entire world. Haven't tried it, but if it worked, that should have the effect of searching the whole world without necessarily biasing a particular location.
LatLngBounds is documented at http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLngBounds. I think you'd want to do something like this:
var sw = new google.maps.LatLng(-89.999999,-179.999999);
var ne = new google.maps.LatLng(89.999999,179.999999);
var bounds = new google.maps.LatLngBounds(sw,ne);