There's quite a few questions on this, and documentation available, however it seems that I can't find the solution for the use case I'm trying to solve.
What I'd like to do:
- Have a Google Map with a search box so a user can search for a location (a city of more specific a point of interest). This is something that I can do.
- When a user searches for a location, no marker is placed, the map only zooms in on the location
- User can click on an establishment from the Places Library
- When that place is clicked, I'd like to get details from this place
So it's basically extending the places autocomplete with the last bullet mentioned
-or-
using the geocomplete library, more specifically the form example, because it already gives all the details needed that are listed below. The thing I'm missing is how to not place a marker on search, and how to get details from an establishment only when the users clicks on it.
The details that I would like to get from the establishment when clicked, which are directly available through geocomplete, are:
- Name
- Formatted address (see geocomplete form example)
- URL to Google Maps (like: https://maps.google.com/?cid=16742034748962857918)
- Latitude
- Longitude
- Website
UPDATE
With the geocomplete library you can bind a click event, like so:
$("#geocomplete_input_field").bind("geocode:click", function(event, latLng){
$("input[name=lat]").val(latLng.lat());
$("input[name=lng]").val(latLng.lng());
});
So with that we get the latitude and longitude. But can you also get the formatted_address, name, and url as happens in the form example?
Well, you don't need any external library to accomplish this. Since everything is already done (codes), all you need to do is just add a 'POI Click Event'. It listens for the click event on a POI icon and then uses the placeId for anything you wanted to achieve. In your case you wanted to acquire these: name, formatted_addres, url, lat, lng, and website.
In the example that I will provide you, I just created a constructor 'ClickEventHandler'. You can check POI Click for sample and documentation.
Then, create a new instance inside your init() function and supply the arguments: map and origin.
In the 'ClickEventHandler' object, create a prototype 'getPlaceInformation' that accepts an argument 'placeid'. You need to use Google Maps Javascript API Place Details in this section. Inside Google Maps Javascript API Place Details getDetails method, you will use the placeid you supplied as argument coming from the previous function (getPlaceInformation) you created. getDetails method needs 2 arguments: place and status. This method gets all the details from the placeid you supplied. Once you get the status === 'OK', it will return to you data that you are expecting.
Working example below (with details in infowindow and input elements value supplied from the POI):
Hope it helps and happy coding!