LabeledMarker integration in gwt-google-apis Google Maps library

41 views
Skip to first unread message

Alberto Núñez

unread,
Aug 22, 2008, 6:12:09 AM8/22/08
to Google Web Toolkit
Hi all,

I needed to show text inside markers in the application I'm currently
working on, and I found a javascript library to achieve this, called
LabeledMarker (http://googlemapsapi.blogspot.com/2007/04/labeledmarker-
v10-do-more-with-your.html).

I had different options to integrate labeledmarkers.js into the maps
library, and I wanted to do it using JSIO, the way the library is
programmed. A choice was to subclass the Marker and MarkerOptions
classes, but that doesn't work because MarkerOptions cannot be
subclassed as it is declared final.

I went with the easier solution: patch the maps library source code.
Below you have the patch for revision r696 of gwt-google-apis. It
should work for earlier revisions, like r290.
It adds 4 new methods to the Marker class:
- void setLabelVisibility(boolean visibility)
- boolean getLabelVisibility()
- void showLabel()
- void hideLabel()
And 3 new methods to the MarkerOptions class:
- void setLabelText(String labelText)
- void setLabelClass(String labelClass)
- void setLabelOffset(Size labelOffset)

So, to make it work you will need to:
- Patch and build the gwt-google-api source code from svn.
- Download either labeledmarker.js or labeledmarker_packed.js from
http://gmaps-utility-library.googlecode.com/svn/trunk/labeledmarker/1.2/src/
and put it on your public folder (js/maps/ is good). Then load it in
the head section of your html:
<script type="text/javascript" language="javascript" src="js/maps/
labeledmarker.js"></script>

Anyway, there is a bug that must be fixed. Javascript throws an error
when the hide method of the marker is called. I will have to look into
it. If you get this error, a workaround (that allows to hide the
marker but not the label) is to comment 'this.hideLabel()' in the
following function of labeledmarker.js:

LabeledMarker.prototype.hide = function() {

GMarker.prototype.hide.apply(this, arguments);
/* this.hideLabel(); */

};

I hope this is useful for those who need it. Any suggestions would be
appreciated.

Here is the patch (as simple as it gets):

Index: maps/maps/src/com/google/gwt/maps/client/overlay/Marker.java
===================================================================
--- maps/maps/src/com/google/gwt/maps/client/overlay/Marker.java
+++ maps/maps/src/com/google/gwt/maps/client/overlay/Marker.java
@@ -672,6 +672,23 @@
}
}

+
+ public void setLabelVisibility(boolean visibility) {
+ MarkerImpl.impl.setLabelVisibility(this, visibility);
+ }
+
+ public boolean getLabelVisibility() {
+ return MarkerImpl.impl.getLabelVisibility(this);
+ }
+
+ public void showLabel() {
+ MarkerImpl.impl.showLabel(this);
+ }
+
+ public void hideLabel() {
+ MarkerImpl.impl.hideLabel(this);
+ }
+
/**
* Manually trigger the specified event on this object.
*
Index: maps/maps/src/com/google/gwt/maps/client/impl/MarkerImpl.java
===================================================================
--- maps/maps/src/com/google/gwt/maps/client/impl/MarkerImpl.java
+++ maps/maps/src/com/google/gwt/maps/client/impl/MarkerImpl.java
@@ -31,10 +31,10 @@

MarkerImpl impl = GWT.create(MarkerImpl.class);

- @Constructor("$wnd.GMarker")
+ @Constructor("$wnd.LabeledMarker")
JavaScriptObject construct(LatLng point);

- @Constructor("$wnd.GMarker")
+ @Constructor("$wnd.LabeledMarker")
JavaScriptObject construct(LatLng point, MarkerOptions options);

void disableDragging(Marker marker);
@@ -68,4 +68,13 @@
void show(Marker marker);

void showMapBlowup(Marker marker, JavaScriptObject options);
+
+ void setLabelVisibility(Marker marker, boolean visibility);
+
+ boolean getLabelVisibility(Marker marker);
+
+ void showLabel(Marker marker);
+
+ void hideLabel(Marker marker);
+
}
Index: maps/maps/src/com/google/gwt/maps/client/overlay/
MarkerOptions.java
===================================================================
--- maps/maps/src/com/google/gwt/maps/client/overlay/
MarkerOptions.java
+++ maps/maps/src/com/google/gwt/maps/client/overlay/
MarkerOptions.java
@@ -17,6 +17,7 @@

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.maps.client.impl.MarkerOptionsImpl;
+import com.google.gwt.maps.client.geom.Size;

/**
* Instances of this class are used in the {@link Marker} constructor
@@ -114,4 +115,17 @@
public void setTitle(String title) {
MarkerOptionsImpl.impl.setTitle(jsoPeer, title);
}
+
+ public void setLabelText(String labelText) {
+ MarkerOptionsImpl.impl.setLabelText(jsoPeer, labelText);
+ }
+
+ public void setLabelClass(String labelClass) {
+ MarkerOptionsImpl.impl.setLabelClass(jsoPeer, labelClass);
+ }
+
+ public void setLabelOffset(Size labelOffset) {
+ MarkerOptionsImpl.impl.setLabelOffset(jsoPeer, labelOffset);
+ }
+
}
Index: maps/maps/src/com/google/gwt/maps/client/impl/
MarkerOptionsImpl.java
===================================================================
--- maps/maps/src/com/google/gwt/maps/client/impl/
MarkerOptionsImpl.java
+++ maps/maps/src/com/google/gwt/maps/client/impl/
MarkerOptionsImpl.java
@@ -21,6 +21,7 @@
import com.google.gwt.maps.jsio.client.BeanProperties;
import com.google.gwt.maps.jsio.client.Constructor;
import com.google.gwt.maps.jsio.client.JSFlyweightWrapper;
+import com.google.gwt.maps.client.geom.Size;

/**
* Wrapper for the GMarkerOptions object from the Maps API using
JSIO. The Maps
@@ -48,5 +49,11 @@
void setIcon(JavaScriptObject jsoPeer, Icon icon);

void setTitle(JavaScriptObject jsoPeer, String title);
+
+ void setLabelText(JavaScriptObject jsoPeer, String labelText);
+
+ void setLabelClass(JavaScriptObject jsoPeer, String labelClass);
+
+ void setLabelOffset(JavaScriptObject jsoPeer, Size labelOffset);

}
Reply all
Reply to author
Forward
0 new messages