Create Map with Features via "BATCH"-Processing

78 views
Skip to first unread message

Fabian

unread,
Jun 9, 2010, 4:59:51 AM6/9/10
to Google Maps Data API
Hello everybody,

this post contains a FULL STEP BY STEP guide how to create a Google
myMap with multiple features (placemarks) including geocoding via the
Google Maps Data Java API. For more info on how to set up the gdata
libraries, feel free to ask me.

My questions concerning how to minimize the amount of web service
calls and make the processing more performant are at the end of this
post.


SCENARIO
Users want to do temporary ad-hoc requests like "show me these 7
addresses on a map". Since the URL parameters on "http://
maps.google.com/maps?q=Berlin,Germany" only allow to process ONE
address, I decided for the MAPS GDATA API. For every "show me
addresses on a map" request I overwrite ONE existing MapEntry with
"old features" by a new MapEntry with "new features".


MY CURRENT SOLUTION WITH BATCH-PROCESSING (every big step results in
one web service call)
1. Login to data api with google account credentials

mapService.setUserCredentials(GOOGLE_ACCOUNT_USER_NAME,
GOOGLE_ACCOUNT_PASSWORD);


2. Get all MapEntries in my google account

private final URL ALL_MAPS_FEED_URL = new URL("http://maps.google.com/
maps/feeds/maps/default/full");
MapFeed allMapsFeed = mapService.getFeed(ALL_MAPS_FEED_URL,
MapFeed.class);


3.1 Iterate over all MapFeedEntries and get the MapEntry with
title="foobar"

List<MapEntry> allMaps = allMapsFeed.getEntries();
MapEntry map = null;
for (int i = 0; i < allMaps.size(); i++)
{
if (allMaps.get(i).getTitle().getPlainText().equals("foobar"))
{
map = allMaps.get(i);
}
}


4. Delete the MapEntry with title="foobar" with all its "old features"

final URL editUrl = new URL(map.getEditLink().getHref());
mapService.delete(editUrl);


5. Create new MapEntry with title="foobar"

final URL mapInsertUrl = new
URL(allMapsFeed.getEntryPostLink().getHref());
MapEntry newMap = new MapEntry();
newMap.setDraft(Boolean.TRUE); // Map "not published"
newMap.setTitle(new PlainTextConstruct("foobar"));
mapService.insert(mapInsertUrl, newMap);


6. Update the local variable allMapsFeed to have the created mapEntry
locally available

allMapsFeed = mapService.getFeed(ALL_MAPS_FEED_URL, MapFeed.class);


7. Add Features to new MapEntry via BATCH-Processing. Features are
created as a String with KML notation

final URL featureFeedUrl = newMap.getFeatureFeedUrl();
final FeatureFeed allFeaturesFeed = mapService.getFeed(featureFeedUrl,
FeatureFeed.class);
final Link batchLink = allFeaturesFeed.getLink(Link.Rel.FEED_BATCH,
Link.Type.ATOM);

if (batchLink != null)
{
final URL batchUrl = new URL(batchLink.getHref());
---------------------------------------------------------------------------------
final List<String> placemarksList =
PlacemarksUtil.createPlacemarkKmlStrings(addresses);

The PlacemarksUtil processes Address-Objekts containing Strings for
Country, City and Street of an adress. It receives the GEOCODE for
EACH address by calling the google GEOCODE Web Service at
http://maps.google.com/maps/geo?q=Germany,Berlin,Opernplatz+27&output=csv&key=##YOUR_API_KEY##
The geocode web service gives a result like
"200,8,52.5165761,13.3934964". You have to comma-split this string and
extract the coordinates latitude (value 3) and longitude (value 4)

If you need more info on this, please ask me.
---------------------------------------------------------------------------------

FeatureFeed newFeatures = new FeatureFeed();

for (int i = 0; i < placemarksList.size(); i++)
{
XmlBlob kml = new XmlBlob();
kml.setBlob(placemarksList.get(i));

FeatureEntry feature = new FeatureEntry();
feature.setKml(kml);

// Optional
// BatchUtils.setBatchId(feature, Integer.toString(i)); //
Lokale Batch ID für diesen Eintrag.
// BatchUtils.setBatchOperationType(feature,
BatchOperationType.INSERT);

newFeatures.getEntries().add(feature);
}

mapService.batch(batchUrl, newFeatures);
}
else
{
// batching is not supported for this feed
}


That's it basically. Now you need to get the MapEntry's browser
display URL
URL browserUrlForMap = new URL(newMap.getHtmlLink().getHref());

You can open this URL with the standard browser of your user's
computer.
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +
browserUrlForMap);


MY GOAL
Reduce the amount of web service calls to a minimum to increase the
performance.


QUESTIONS
1. I would like to create a new MapEntry and add multiple
FeatureEntries immediately before making the INSERT CALL
mapService.insert(mapInsertUrl, mapEntry); How can I do something
like:

final List<String> placemarksList =
KartenDienstPlacemarksUtil.createPlacemarkKmlStrings(addresses);
FeatureFeed newFeatures = new FeatureFeed();
for (int i = 0; i < placemarksList.size(); i++)
{
XmlBlob kml = new XmlBlob();
kml.setBlob(placemarksList.get(i));

FeatureEntry feature = new FeatureEntry();
feature.setKml(kml);
newFeatures.getEntries().add(feature);
}

map.setSource(newFeatures); OR map.setContent(xxx); ???


2. Is BATCH-processing available for the GEOCODE web service e. g.
"Give me the coordinates for these 10 addresses with one web service
call"
http://maps.google.com/maps/geo?q=Germany,Berlin,Opernplatz+27&output=csv&key=##YOUR_API_KEY##
?

Thanks for your help and tips!
Cheers from Germany,
Fabian
Reply all
Reply to author
Forward
0 new messages