Google map addon

Hello,

I’ve created a small demo using Google map addon -
http://linked2map.com/

You can login throught LinkedIn and see your connections on Google Map.

Thanks a lot.

I logged in and it tells me how many connections I have, but I don’t see them on the map…

I’ve been working with the google maps addon for a few days and everything is going well except for one problem. I can’t get polygons and polyoverlays to remove from the map, the application just adds polygons over the old ones. This isn’t occurring with markers, and when I refresh the page, the polygons are gone. I’m currently using vaadin 6.4.2, version 0.9.10 of the google earth add-on, and tomcat 6.0.18.

It seems there is a similar problem with removing polyoverlays that was fixed regarding markers. I’ll take a look at it when I next get the time to work on the addon.

The fix was a small one so I did it while I was looking at the source. Version 0.9.11 is now up
in the directory
that should fix the problem.

Also added a getOverlays() method to the public API.

Hello Henry,

hope I didn’t miss anything important to this but since version 0.9.10 using custom icons for markers no longer works.
Seems you either forgot to add

this.iconAnchor = latLng;

to the constructor of
BasicMarker
or forgot a check for null around

				markerJSON.append(",\"iconAnchorX\":"
						+ marker.getIconAnchor().x);
				markerJSON.append(",\"iconAnchorY\":"
						+ marker.getIconAnchor().y);

in
BasicMarkerSource.getMarkerJSON
.

Cheers,
Andreas

P.S.: perhaps you should also add a check that the apiKey is not null (as opposed to
“”
)…

Oops, you’re right. I didn’t catch that because my test case had a explicit setIconAnchor() call.


0.9.12 is now available
that should fix the problem.

I didn’t touch the apiKey check, because it’s initialized to “” and not null. It can only be null if you explicitly call the constructor to set it to null. I’ll change it later if it becomes a problem.

Upgraded the map widget to 0.9.12 and it doesn’t seem to resolve the issue unfortunately. Markers still work, but polygons aren’t being removed with the remove Overlay or update overlay commands. Did I need to upload the gwt-maps.jar, or is it the same as the previous version?

Trying to see if it was a browser issue, but still had the same issue in Safari and in Firefox. I was wondering if it could be a tomcat issue?

edit: Tried with the latest update and with the latest tomcat, and it didn’t fix the issue… is there something special I need to do with the code to get overlays to remove?

Did you remove the old version of the jar, and recompile your widgetset afterward?

I haven’t been able to test the recompiled widgetset yet. I’ll let you know the result after I can test it.

edit: Trying to recompile it is proving quite difficult at the moment since the gwt compiler that I have is 2.0.4 which does not seem compatible with the vaadin plugin. Any suggestions?

edit: After much effort, I got the widget to recompile, and the update worked. Thanks very much!

Hi Henri,

first of all, thanks a lot for this excellent add-on of yours! It’s saving us a lot of effort and works really great.

I do have an issue, though, with setting my own icons for the markers with version 0.9.12 of the add-on. I’m using icons which have the same shape as the default Google markers. The icons do appear on the map, except that they are not properly aligned. The needle-tip of the icon is not placed on the coordinates of the marker (as with the default icon) but is displaced a bit to the lower right of the coordinates. I tried to adjust this alignment by using the setIconAnchor() method, but to no avail. No matter what parameter values I chose, the alignment remained incorrect. It didn’t change with different values, either. This method seems to have no effect at all. Regarding the image file of my markers, I’d expect the icon anchor to be (10.0, 29.0), but this doesn’t work. Do you have any ideas?

When browsing your code trying to find a solution for myself, I spotted a small performance optimization you could apply to class BasicMarkerSource. In method getMarkerJSON() you’re using a StringBuilder for assembling the JSON string, which is good. I’d suggest that you completely avoid string concatenization in this method. So, line

markerJSON.append("{\"mid\":\"" + marker.getId());

could be changed to

markerJSON.append("{\"mid\":\"").append(marker.getId());

and so on. This would give the Garbage Collector an easier time :wink:

Keep up the good work!

Did you call requestRepaint() on the map after adjusting the anchor position? Currently the marker doesn’t notify the map that its properties have changed, so the map doesn’t know to send them to the browser without an explicit requestRepaint() call.

Good catch, I’ll fix that on the next update :slight_smile:

Having an issue now when I place a basicmarker it’ll erase polygons that are laid on the map (when I click another polygon down it relays all of them). Is there anyway I can fix this behavior?

Yes, in fact I have created a subclass of BasicMarker with all necessary initializations (icon URLs, iconAnchors etc.) in its constructor. After creating all marker instances and adding them to the map I call requestRepaint(). However, this doesn’t help either.

There are two more things that I stumbled upon. The first one is not exactly a bug in that sense, rather a pitfall. I had a situation when the Google Map widget suddenly stopped drawing any map markers at all, no matter what I tried. The reason behind this behaviour was quite simple. In some marker title strings special characters such as " or ’ appeared. I then learned that class org.vaadin.hezamu.googlemapwidget.GoogleMap sends all marker data to the client through a JSON string. Of course, these special chars wreak havoc on the JSON string if not properly escaped. Sure, this can easily be avoided by the user of the add-on by escaping the characters before adding the text to a marker. But I could well imagine some switch in class BasicMarker to enable/disable an automatic escaping of these characters, for instance in method setTitle(String). I tried to escape these characters myself (with ’ becoming " or '). This fixed the problems with the JSON data, but the replaced characters were not properly displayed in the tooltip text of the marker. That is, the text [“Eating” at McDonald’s]
is displayed as ["Eating" at McDonald's]
without the desired effect that &#34 is displayed as double quotes. As a workaround I’m now simply removing all those characters completely from the title string before adding it to my BasicMarker subclass. Do you have any idea how it could be possible to escape these characters on the one hand but have them displayed correctly after the data has been sent to the client on the other hand?

The second thing regards a newly created GoogleMap instance. To only add markers which are visible in the current viewport of the map, I do a check against the map’s current bounds and the position of the marker to be added. But if the map has just been created (i.e. no MapMoveEvent has been emitted yet), the values for boundsNE and boundsSW are still null. I’d have to calculate the current bounds of the map for myself (which I don’t know how to do yet). Is there a possibility to obtain the maps bounds directly after creating it? After the map is moved or zoomed, the bounds are set with the correct values.

Sorry, that is a regression from the previous overlay-related change. I’ll fix that on the next update. You’ll need to recompile the widgetset again.

I can’t reproduce the problem, my test case sets the icon and the anchor correctly. I have a button that runs this:

if (mark1.getIconUrl() == null) {
	mark1.setIconUrl("http://bits.ohloh.net/attachments/18966/v_med.gif");
	mark1.setIconAnchor(null);
} else if (mark1.getIconAnchor() == null) {
	mark1.setIconAnchor(new Point2D.Double(-20, -20));
} else {
	mark1.setIconUrl(null);
	mark1.setIconAnchor(null);
}

googleMap.requestRepaint();

It correctly toggles the marker from no icon, to a icon with no anchor, and icon with anchor. If this doesn’t help you I’ll need some example code to investigate further.

The strings need to be escaped in the server side component (like you did) and also unescaped in the client side component. Do you have any preference how to do the escaping? One option would be to do it by hand, or to add a dependency to eg. Apache Commons Lang. Latter would be a lot cleaner, but I hesitate to add any dependencies to the addon.

Not currently, but I’ll add a reportMapBounds() method to the map. Note that this will trigger an extra roundtrip when the client side component reports the bounds data (including zoom level and center) when it’s rendered in the browser.


0.9.13 is available
.

API changes:

  • Added the reportMapBounds() method which will result in a immediate MapMoveEvent when the browser renders the widget.

Fixes and updates:

  • BasicMarkerSource now fully uses StringBuilder when creating marker JSON.
  • Fixed a bug that removed polygons and polylines when markers were added.
  • Fixed a bug that crashed the widget if single or double quotes were used in the marker titles.
  • Fixed a bug that prevented hiding markers, and as a side effect redrew all markers on every render. This improves client side performance quite a bit, especially if a lot of markers are used.

I’m afraid that the addon is not GAE compatible at the moment. The server side API uses Point2D.Double to handle coordinate points, so those would need to be replaced with something else.

Probably a simple custom class would be the best choice. I don’t think the change would be very hard, but there are several places to replace.

0.9.13 cleared up the issue with markers and polygons. Thanks!

Replaced the Point but wicked problems seems to arise. Adding a BasicMarker to a map will cause excessive session expiration, in any event fired from the application. No difference with local or deployed version. Also sometimes this doesn’t happen but even then event listeners are never reached. Any idea where the problem might be?

The problem description is too vague to pinpoint the problem, but are there any exceptions or errors in the Eclipse console or browser debug console?

How did you include the modified widget? Did you just copy & modify the source? If so, did you remove the addon JAR and recompile your widgetset?

And to make sure, does the app compile without errors?