Friday October 24, 2008
TOTD #51: Embedding Google Maps in Java Server Faces using GMaps4JSF
![]() |
GMaps4JSF allows Google Maps to be easily integrated with any JSF application. This blog shows how to use this library with Mojarra - JSF implementation delivered from the GlassFish community. |
| private float latitude; private float longitude; private String details; @ManagedProperty(value="#{cities}") private Cities cities; private final String BASE_GEOCODER_URL = "http://maps.google.com/maps/geo?"; private final String ENCODING = "UTF-8"; private final String GOOGLE_MAPS_KEY = "GOOGLE_MAPS_API_KEY"; private final String OUTPUT_FORMAT = "CSV"; public String getLatLong() throws IOException { details = cities.getCityName() + ", " + cities.getCountryName(); String GEOCODER_REQUEST = BASE_GEOCODER_URL + "q=" + URLEncoder.encode(details, ENCODING) + "&key=" + GOOGLE_MAPS_KEY + "&output=" + OUTPUT_FORMAT; BufferedReader reader = new BufferedReader( new InputStreamReader( new URL(GEOCODER_REQUEST).openStream())); String line = null; int statusCode = -1; while ((line = reader.readLine()) != null) { // 200,4,37.320052,-121.877636 // status code,accuracy,latitude,longitude statusCode = Integer.parseInt(line.substring(0, 3)); if (statusCode == 200) { int secondComma = line.indexOf(",", 5); int lastComma = line.lastIndexOf(","); latitude = Float.valueOf(line.substring(secondComma+1, lastComma)); longitude = Float.valueOf(line.substring(lastComma+1)); System.out.println("Latitude: " + latitude); System.out.println("Longitude: " + longitude); } } return "map"; } // getters and setters |
| @ManagedBean(name="coords", scope="request") |
| <h:commandButton action="#{coords.getLatLong}" value="map"/> |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:m="http://code.google.com/p/gmaps4jsf/"> <head> <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAF9QYjrVEsD9al2QCyg8e-hTwM0brOpm-All5BF6PoaKBxRWWERRHQdtsJnNsqELmKZCKghs54I-0Uw" type="text/javascript"> </script> </head> <body> <m:map latitude="#{coords.latitude}" longitude="#{coords.longitude}" width="500px" height="300px" zoom="14" addStretOverlay="true"> <m:marker draggable="true"> <m:eventListener eventName="dragend" jsFunction="showStreet"/> </m:marker> <m:htmlInformationWindow htmlText="#{coords.details}"/> <m:mapControl name="GLargeMapControl" position="G_ANCHOR_BOTTOM_RIGHT"/> <m:mapControl name="GMapTypeControl"/> </m:map> <br/> <br/> <m:streetViewPanorama width="500px" height="200px" latitude="#{coords.latitude}" longitude="#{coords.longitude}" jsVariable="pano1" /> <script type="text/javascript"> function showStreet(latlng) { pano1.setLocationAndPOV(latlng); } </script> <form jsfc="h:form"> <input jsfc="h:commandButton" action="back" value="Back"/> </form> </body> </html> |
|
<navigation-rule> <from-view-id>/welcome.xhtml</from-view-id> <navigation-case> <from-outcome>map</from-outcome> <to-view-id>/map.xhtml</to-view-id> </navigation-case> </navigation-rule> <navigation-rule> <from-view-id>/map.xhtml</from-view-id> <navigation-case> <from-outcome>back</from-outcome> <to-view-id>/welcome.xhtml</to-view-id> </navigation-case> </navigation-rule> |



Posted by Arun Gupta in web2.0 | Comments[8]
|
|
|
|
|
Today's Page Hits: 2750
Total # blog entries: 994
| « November 2009 | ||||||
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
1 | 2 | 4 | 6 | 7 | ||
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | |||||
| Today | ||||||
VERY NICE BLOG
ITS VERY USEFUL
THANK YOU..........
Posted by michael on October 24, 2008 at 06:35 AM PDT #
thank you :)
Posted by tattoo on October 26, 2008 at 04:46 AM PDT #
thanks.
Posted by izolasyon on November 02, 2008 at 10:03 AM PST #
Arun,
Good overview and code segment.
Thanks!
Posted by Chris on December 19, 2008 at 07:02 AM PST #
I hope they do the same with .net soon enough so that we can use this is our .net apps too.
Posted by Web Designer on January 19, 2009 at 01:50 PM PST #
Google maps have opened so many new ways and technologies. Its great.
Posted by Joy on March 12, 2009 at 02:58 PM PDT #
A very good technology..
Posted by sinema izle on March 13, 2009 at 02:08 PM PDT #
very nice blog its very useful thank you very much cong...
Posted by estetik on March 27, 2009 at 03:14 AM PDT #