Page 1 of 2

Use p:gmap with address.

Posted: 17 Jan 2012, 13:44
by neijunior
Hello !

Anyone know if there is a way to use "p:gmap" passing an address and not only the longitude and latitudes ?

Like this component of IceFaces:
http://comp-suite.icefaces.org/comp-sui ... nu&exp=map

I searched in forum and issue, but found nothing equal.

Thanks.

Re: Use p:gmap with address.

Posted: 24 Jan 2012, 18:25
by claytonrocha
I also am in need of such an implementation would be very cool

Re: Use p:gmap with address.

Posted: 24 Jan 2012, 21:25
by smithh032772
Go to Issue 3220 in Issue Tracker, click Star (as that casts your vote for this new feature to be added to PrimeFaces, and to be updated via email about this issue/feature request).

Re: Use p:gmap with address.

Posted: 24 Jan 2012, 21:35
by neijunior
Hello smithh032772

Very good. I'll vote, thanks !!

Re: Use p:gmap with address.

Posted: 24 Jan 2012, 23:59
by djmj
It would be very handsome, especially receiving geocoding informations.

But there is an jsf implementation already out there, so maybe no need to reinvent the wheel, but I did not tested it yet.

http://code.google.com/p/gmaps4jsf/

But since Google Geocoding informations should be retrievied by client and not a server (recommend by google), Javascript is the way to go here.
(Google will block you if your server has too many requests!, using client side request you can avoid this)

In my case I only use it inside an admin panel to retrieve coordinates and save them to database, so it would be handsome using it serverside.

Just call the google api and save the lat and lng in (hidden) fields which are attached to a bean.


Here extract of my function code. For better usage I have one client marker reference which is updated with new location upon a search and I pass a callback function to open a confirm dialog to confirm the found location.

Code: Select all

var geocoder = new google.maps.Geocoder();

function geocodeAddress(inputAddressId, inputLatId, inputLngId, coordMaxFractDigits, map)
{
	var inputAddress = document.getElementById(inputAddressId);

	geocoder.geocode({'address': inputAddress.value}, function(results, status)
	{
		if(status == google.maps.GeocoderStatus.OK)
		{
			if(results.length > 0)
			{
				if(results.length > 1)
				{
					alert("Multiple addresses found for given input.")
					//do something
				}
			
				//set name of address text field, direct usage
				inputAddress.value = String(results[0].address_components[0].long_name);
				
				var latLng = results[0].geometry.location;
			
				//set lat and lng text fields
				document.getElementById(inputLatId).value = round(parseFloat(latLng.lat()), coordMaxFractDigits).toLocaleString();
				document.getElementById(inputLngId).value = round(parseFloat(latLng.lng()), coordMaxFractDigits).toLocaleString();
			
				map.setCenter(latLng);
			
				var viewport = results[0].geometry.viewport;
				map.setZoom(10 - Math.round(Math.log(Math.abs(viewport.getSouthWest().lng() - viewport.getNorthEast().lng()))/Math.log(2)));
			}
		}
	});
}

Call it something like:

Code: Select all

<h:form>
	<p:gmap id="cityMap"/>
	<p:inputText id="name" .../>
	<h:inputHidden id="lat" .../>
	<h:inputHidden id="lng" .../>
	<p:commandButton ... onclick="geocodeAddress(\'#{component.parent.findComponent('name').clientId}\', 
		\'#{component.parent.findComponent('lat').clientId}\', 
		\'#{component.parent.findComponent('lng').clientId}\', 
		5,
		#{p:widgetVar('cityMap')}.getMap())"/>
</h:form>

Maybe the now lat and lng values can be reset into center attribute of gmap for autofocus.

Re: Use p:gmap with address.

Posted: 25 Jan 2012, 02:29
by smithh032772
djmj wrote:Here extract of my function code. For better usage I have one client marker reference which is updated with new location upon a search.
Thanks for sharing your code. What about using geocoder (client javascript) with p:autoComplete or p:inputText, start typing address, and geocoder does similar to p:autoComplete?

Re: Use p:gmap with address.

Posted: 25 Jan 2012, 04:09
by djmj
I don't see how that should be working, since autocomplete is using server side bean for auto suggestion.

Maybe send the Json response to the bean and populate a list, or maybe it is possible to extract just the names of all found locations. But if it is just for autocomplete feature I don't see much sense in communicating with the server.

Re: Use p:gmap with address.

Posted: 25 Jan 2012, 04:29
by smithh032772
That might work. Good thought/idea.

Earlier you mentioned that google recommends javascript client code, but I think I researched this, and I think I saw that REST webservice is available for geocoder, for those interested in server-side solution that could possibly populate p:autoComplete. At some point, I'd like to research this and try it. I think I bookmarked it in my Chrome browser.

Re: Use p:gmap with address.

Posted: 25 Jan 2012, 11:25
by cagatay.civici

Re: Use p:gmap with address.

Posted: 25 Jan 2012, 13:00
by neijunior
Hello optimus.prime,
On the issue in 1629, says he would be ready in version 3.0, 3.0-RC1 and Future of primefaces, but have no implementation so.
You know when that will be implemented?

Thanks.