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);
Related
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.
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
As per this link: http://code.google.com/apis/maps/documentation/javascript/places.html#place_search_requests
The method can take either
-a point and a radius or
-a rectangle boundary
using the point + radius gives me correct results, however the max radius is only 50 km and I need my search to be up to 1000 km, so I tried using bounds.
When my bounds are small I get the same correct results as the point + radius, however when I increase the bounds I get no results at all.
The code below will give correct results, however if you replace the var sw and var ne with the commented out coordinates, it no longer works.
ie. kitchener is found within the boundary between ayr and guelph, but not in the boundary between detroit and ottawa. Which doesn't make any sense if you look at the map.
<script type="text/javascript">
var map;
var infowindow;
function initialize() {
var Cambridge = new google.maps.LatLng(43.346528, -80.417962);
var sw = new google.maps.LatLng(43.292501,-80.543175); // 41.914541,-83.282318
var ne = new google.maps.LatLng(43.605057,-80.156250); // 45.580391,-76.283051
var zoneBounds = new google.maps.LatLngBounds(sw,ne);
map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: Cambridge,
zoom: 10
});
var request = {
bounds: zoneBounds,
//location: Cambridge, //using location and
//radius: 500000, //radius works
name: ['Kitchener, ON']
};
infowindow = new google.maps.InfoWindow();
var 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++) {
createMarker(results[i]);
}
}
}
function createMarker(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
I'm supposed to be able to use PlacesServiceStatus to find the status of the request, i assume this would give me insight into the problem but I have no idea how to retrieve the status codes
Any help with this would be much appreciated
Thanks!
edit: ok, I added alert(status); and the status says it's "ZERO_RESULTS"
In the callback method you could do something like
function callback(results, status) {
window.alert(results.length);
that will tell you how many results you have, you could loop round them something like this...
for (var i = 0; i < results.length; i++) {
window.alert(results[i].formatted_address);
}
I've tried this myself and yes, if the bounds are greater than 50km then the results are pretty useless. I could guess at an answer; if the bounds are very large then it may involve too much of a hit on the Google servers. However, I don't like that answer so if someone knows better; help us out!
Another alternative is to use the Places Autocomplete -
https://developers.google.com/maps/documentation/javascript/places#places_autocomplete
You could use the geolocation but I doubt that will provide the responses you're looking for, for example if you geocode 'enfield' it will show you all the enfields in the world, if you geocode 'enf' it will show you a finish airport!
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.