Loading resources to client

Hi,

I’m extending my simple Google map example (see
http://dev.itmill.com/wiki/Articles/WidgetCreationHowto
) to handle a large number of markers. Adding markers to the map is pretty slow (around 100ms), so I’d like to make the marker data available as a resource. This way the map can be drawn immediately and the markers are drawn in the background.

Now, I’ve set up an ApplicationResource that streams the marker data as a JSON string. In IGoogleMap.updateFromUIDL() I have


if (uidl.hasAttribute("markerRes")) {
	String url = client.translateToolkitUri(uidl
			.getStringAttribute("markerRes"));
	if (url != null) {
		System.out.println("Markers available at " + url);
		// Now what?
	}
}

and it prints out the correct URL. But, how do I go about in the client to load the JSON?

First, I’d like to do that in the background and not in updateFromUIDL(), what’s the easiest way to do it? Should I use a Timer (like in
http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/Timer.html
), or is there a even easier way?

Secondly, how do I get the JSON string from the stream?

Sorry for the silly question, obviously the correct way to do what I need is to use RequestBuilder to construct and send an asynchronous request to retrieve the resource stream contents. Here’s the code:


if (uidl.hasAttribute("markerRes")) {
	markerUrl = client.translateToolkitUri(uidl
			.getStringAttribute("markerRes"));
	if (markerUrl != null) {
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
				markerUrl);

		try {
			builder.setTimeoutMillis(2000);

			builder.sendRequest(null, new RequestCallback() {
				public void onError(Request request, Throwable e) {
					if (e instanceof RequestTimeoutException) {
						System.out
								.println("Timeout fetching marker data: "
										+ e.getMessage());
					} else {
						System.out
								.println("Error fetching marker data: "
										+ e.getMessage());
					}
				}

				public void onResponseReceived(Request request,
						Response response) {
					System.out.println("Response text:\n"
							+ response.getText());
				}
			});
		} catch (RequestException e) {
			System.out.println("Failed to send the request: "
					+ e.getMessage());
		}
	}
}

Note, that if the asynchronous call happens very fast the map drawing might get blocked while the markers are processed. If that happens the map is not drawn before the markers are added to it, resulting in a nasty delay for the user.

For this reason the request retrieving the markers should happen in a
DeferredCommand