26/02: Markers in Google Maps
Category: JBoss Seam | Posted by: JohnHaselden
I've been playing with the Google Maps tag supplied with RichFaces this week and it's been a bit frustrating regarding the lack of documentation for getting markers to function. While I haven't actually finialised the routines on the project, I thought I'd share my knowledge and hopefully you'll find it of some use.
First up, let's consider the composition of the tag:
OK, in order
So this will display a map for you that will start at your lat / lng position and allow you to move around and zoom in and out. But beyond that, it won't let you do anything really useful. What I needed was the ability to add markers to the map for the project I'm working on and this is all handled in the 'init(map)' call.
This JavaScript method is used to add event listeners to the map that respond to events on the map. Read the Google docs if you want more info on what can be captured as an event. At the moment, I'm only interested in the user clicking on the map, so here is my listener.
And that's a working map. From your GMarker, you can get the GLatLng object that will allow you to retrieve the latitude and longitude of the marker for later. I store these values in hidden form elements that are mapped into my backing Address bean.
As I progress with this work, I'm going to be adding geolocation look-ups (mainly focussed around UK postcodes initially) so keep checking back for more!
First up, let's consider the composition of the tag:
<rich:gmap
mapType="G_NORMAL_MAP"
lat="#{address.latitude}"
lng="#{address.longitude}"
zoom="5"
gmapVar="map"
gmapKey="your gmap API key"
oninit="init(map)"
id="map"/>
OK, in order
- mapType is the type of map you can display: here we're going for a normal map: no satellite images or hybrids - possible values are G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP.
- lat is the starting latitude of the map with the value in the range of -90 to +90
- lng is the starting longitude of the map with the value in the range of -180 to +180
- zoom is the zoom ratio of the map 1 being furthest out and 18 being the closest
- gmapVar is the name of the object created in the DOM on the browser for your map
- gmapKey is the Google maps API key you'll need to access Google maps
- oninit is a JavaScript method your map needs to call on start up - we'll get to this in a minute as it's very important
- id is the id of this element in the browser - this is distinct from gmapVar
So this will display a map for you that will start at your lat / lng position and allow you to move around and zoom in and out. But beyond that, it won't let you do anything really useful. What I needed was the ability to add markers to the map for the project I'm working on and this is all handled in the 'init(map)' call.
This JavaScript method is used to add event listeners to the map that respond to events on the map. Read the Google docs if you want more info on what can be captured as an event. At the moment, I'm only interested in the user clicking on the map, so here is my listener.
var marker_count = 0;
var lat;
var lng;
function init(map) {
GEvent.addListener(map, "click", function(marker, point) {
if(marker) {
// a marker has been clicked; remove it
marker_count = marker_count-1;
map.removeOverlay(marker);
} else {
// add a marker
marker = new GMarker(point);
map.addOverlay(marker);
}
});
}
And that's a working map. From your GMarker, you can get the GLatLng object that will allow you to retrieve the latitude and longitude of the marker for later. I store these values in hidden form elements that are mapped into my backing Address bean.
As I progress with this work, I'm going to be adding geolocation look-ups (mainly focussed around UK postcodes initially) so keep checking back for more!
Comments
JohnHaselden wrote:
Thanks James! I've seen in a few places that the solution to the limitations of the Google Maps API is to be a little 'creative'. However, I would suggest that if you're using Google Maps within a commercial application, then you need to be mindful of the terms and conditions.
21/04 21:59:05
neki wrote:
Hi,
Maybe you could help me with getting lat and lng into my application.
For now I am able to show the map, but I don't know where to put javascript...
(I'm developing in seam, ear project)
Maybe you could help me with getting lat and lng into my application.
For now I am able to show the map, but I don't know where to put javascript...
(I'm developing in seam, ear project)
06/05 09:26:02
JohnHaselden wrote:
The easiest solution is to include the javascript within your xhtml file. Otherwise, you may wish to consider having a .js file that contains your javascript and include this file from your xhtml file. The .js file would need to be relative to your xhtml files in your project (either in the same directory or a js directory - it's up to you).
06/05 11:33:17
neki wrote:
ok thanks for that...
my next step, now that i can set the marker is retreving the lat and lng into my backing bean...;) i'll play a little with that... if you have any suggestions I'll be glad to hear them :)
thanks for now,
neki
my next step, now that i can set the marker is retreving the lat and lng into my backing bean...;) i'll play a little with that... if you have any suggestions I'll be glad to hear them :)
thanks for now,
neki
18/05 15:55:26
ArianaDiCy wrote:
I really very liked this post. Can I copy it to my blog? Thank you in advance. Sincerely
13/05 14:42:45



James Salt wrote:
this was my simple uk geocoding class, it may be of some use.
Thanks,
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
public class GeoCoder {
private final static String ENCODING = "UTF-8";
private final static String KEY = "YOU_KEY_HERE";
public static class Location {
public String lon, lat;
private Location(String lat, String lon) {
this.lon = lon;
this.lat = lat;
}
public String toString() {
return "Lat: " + lat + ", Lon: " + lon;
}
}
public static Location getLocation(String address) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(new URL(
"http://maps.google.com/maps..."
+ URLEncoder.encode(address, ENCODING)
+ "&output=csv&key=" + KEY).openStream()));
String line;
Location location = null;
int statusCode = -1;
while ((line = in.readLine()) != null) {
// Format: 200,6,42.730070,-73.690570
statusCode = Integer.parseInt(line.substring(0, 3));
if (statusCode == 200)
location = new Location(line.substring("200,6,".length(), line
.indexOf(',', "200,6,".length())), line.substring(line
.indexOf(',', "200,6,".length()) + 1, line.length()));
}
if (location == null) {
switch (statusCode) {
case 400:
throw new IOException("Bad Request");
case 500:
throw new IOException("Unknown error from Google Encoder");
case 601:
throw new IOException("Missing query");
case 602:
return null;
case 603:
throw new IOException("Legal problem");
case 604:
throw new IOException("No route");
case 610:
throw new IOException("Bad key");
case 620:
throw new IOException("Too many queries");
}
}
return location;
}
}