Hi Christian, This is a nice idea, but having a value type make calls out to the network isn't the sort of thing it ought to do. But what would make sense is to wrap this up into a domain service that can then be injected into entities, eg something like a LocationLookupService.
Let me take your code and refactor it a little. Cheers Dan On 23 January 2013 18:45, Christian Steinebach < [email protected]> wrote: > Hi again! > > A 'quick and dirty' extension of Location.java to obtain the location from > google based on an address string. > > Location.fromGoogleAddress("Selbu, Norway") > Location.fromGoogleAddress("Downing street 1,London,UK"); > > > As long as google can make any sense out of the address it will return a > result. > May use xml or json, xml used here. > Dependencies > > <dependency> > <groupId>commons-httpclient</groupId> > <artifactId>commons-httpclient</artifactId> > <version>3.1</version> > </dependency> > <dependency> > <groupId>org.jdom</groupId> > <artifactId>jdom</artifactId> > <version>1.1.3</version> > </dependency> > > Just an idea. > > Cheers > Christian > > > > > package com.danhaywood.isis.wicket.gmap3.applib; > > import java.io.Serializable; > import java.io.StringReader; > import org.apache.commons.httpclient.HttpClient; > import org.apache.commons.httpclient.methods.GetMethod; > import org.apache.commons.httpclient.util.URIUtil; > > import org.apache.isis.applib.annotation.Value; > import org.jdom.Document; > import org.jdom.Element; > import org.jdom.input.SAXBuilder; > > /** > * Value type representing a location on a map. > * > */ > @Value(semanticsProviderClass = LocationSemanticsProvider.class) > public class Location implements Serializable { > > private static final long serialVersionUID = 1L; > static final Location DEFAULT_VALUE = new Location(51.4777479, 0.0d); > // Greenwich Royal Observatory > static final String BASEURL = " > http://maps.googleapis.com/maps/api/geocode/"; > static final String MODE = "xml"; > static final HttpClient httpclient = new HttpClient(); > static final GetMethod get = new GetMethod(BASEURL + MODE); > > /** > * Factory method > * > * @see #toString() > */ > public static Location fromString(String encodedString) { > final String[] split = encodedString.split(";"); > try { > double latitude = Double.parseDouble(split[0]); > double longitude = Double.parseDouble(split[1]); > return new Location(latitude, longitude); > } catch (Exception e) { > return null; > } > } > > public static Location fromGoogleAddress(String address) { > boolean sensor = false; > String query = "?address=" + address + "&sensor=" + sensor; > > try { > get.setQueryString(URIUtil.encodeQuery(query)); > httpclient.executeMethod(get); > String xml = get.getResponseBodyAsString(); > SAXBuilder builder = new SAXBuilder(); > Document doc = builder.build(new StringReader(xml)); > Element root = doc.getRootElement(); > String lat = > root.getChild("result").getChild("geometry").getChild("location").getChildTextTrim("lat"); > String lon = > root.getChild("result").getChild("geometry").getChild("location").getChildTextTrim("lng"); > return Location.fromString(lat + ";" + lon); > } catch (Exception ex) { > return null; > } > } > > ________________________________________ > From: Dan Haywood [[email protected]] > Sent: Wednesday, January 23, 2013 9:54 AM > To: [email protected] > Subject: Re: isis compile from source, gmaps2 widget? > > Just picking up on this thread.... > > I've been doing some work to resurrect the gmaps widget, and now have it > going. > > It does depend on a SNAPSHOT version of wicketstuff-gmap3, so can't be made > part of Isis proper yet. > > However, I have created a github repo which explains how to configure it > [1] (with some pretty screenshots). > > Also, this needed some updates to wicket viewer itself, so you'll need to > build Isis from source to try it out. > > (Jeroen: the fixes I've done here might've sorted out our calendar view, > but I haven't tested it yet). > > Dan > > > [1] https://github.com/danhaywood/isis-wicket-gmap3 >
