[OGo-Users] OGoAddressMapLink
Helge Hess
users@opengroupware.org
Sat, 15 Dec 2007 23:27:45 +0100
--Apple-Mail-3-407133722
Content-Type: text/plain;
charset=US-ASCII;
format=flowed;
delsp=yes
Content-Transfer-Encoding: 7bit
On 15.12.2007, at 23:19, Sebastian Reitenbach wrote:
> I found it handy to have such a link next to an address, so maybe
> someone else too. feedback is welcome.
Ah, thats cool! :-)
What I don't get is why the element takes a string. Shouldn't it take
an LSAddress object, check it and return the appropriate link?
The URL generation also looks a bit weird.
I've added a Java class I wrote a while ago to do something similiar.
Its attached. (this is done as Formatter object, but a dynamic element
is an even better thing).
Greets,
Helge
--
Helge Hess
http://www.helgehess.eu/
--Apple-Mail-3-407133722
Content-Disposition: attachment;
filename=GMapsAddressFormat.java
Content-Type: application/octet-stream;
x-unix-mode=0644;
name="GMapsAddressFormat.java"
Content-Transfer-Encoding: 7bit
package com.zideone.ojournal.lib;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.opengroupware.jope.appserver.core.WOMessage;
import org.opengroupware.jope.foundation.NSKeyValueCoding;
import org.opengroupware.logic.db.OGoAddress;
/**
* GMapsAddressFormat
* <p>
* Takes an OGoAddress object and returns a GoogleMaps URL for that.
*
* @author helge
*/
public class GMapsAddressFormat extends Format {
private static final long serialVersionUID = 1L;
protected final static Log log = LogFactory.getLog("GMapsAddressFormat");
protected String mapsPrefix;
public static final GMapsAddressFormat defaultFormat =
new GMapsAddressFormat("http://maps.google.de/maps?f=q&hl=de");
public GMapsAddressFormat(String _prefix) {
super();
this.mapsPrefix = _prefix;
}
@Override
public StringBuffer format
(Object _object, StringBuffer _sb, FieldPosition pos)
{
/*
* http://maps.google.de/maps
* ?f=q&
* hl=de&
* geocode=&
* q=Universit%C3%A4tsplatz+12,+39104+Magdeburg&
* sll=51.124213,10.546875&
* sspn=14.93921,29.882813&
* ie=UTF8&
* z=16&
* iwloc=addr&
* om=1
*/
if (!(_object instanceof OGoAddress)) {
log.warn("given object is not an OGoAddress: " + _object);
return null;
}
String q = mapsQueryForAddress((OGoAddress)_object);
if (q == null) {
log.info("got no address to generate a URL for.");
return _sb;
}
String charset = WOMessage.defaultURLEncoding();
try {
q = URLEncoder.encode(q, charset);
}
catch (UnsupportedEncodingException e) {
log.error("could not encode form parameters due to charset", e);
}
if (q == null || q.length() == 0)
return null;
/* append URL */
_sb.append(this.mapsPrefix);
_sb.append("&ie=");
_sb.append(charset);
_sb.append("&q=");
_sb.append(q);
_sb.append("&z=7"); // zoom (7=north germany, 8=SA, 6=all Germany)
return _sb;
}
/**
* Generates a Google Maps query string for a given address object. The
* address should expose those KVC keys:
* <ul>
* <li>street
* <li>city
* <li>zip
* <li>country
* </ul>
*
* @param _adr - the object representing the address
* @return a String object containing the query or null
*/
public static String mapsQueryForAddress(NSKeyValueCoding _adr) {
/* q=Universit%C3%A4tsplatz+12,+39104+Magdeburg */
if (_adr == null)
return null;
StringBuilder gq = new StringBuilder(256);
String v = (String)_adr.valueForKey("street");
if (v != null && v.length() > 0) {
if (gq.length() > 0) gq.append(",");
gq.append(v);
}
if (((v = (String)_adr.valueForKey("city")) != null) && v.length() > 0) {
if (gq.length() > 0) gq.append(",");
String v2 = (String)_adr.valueForKey("zip");
if (v2 != null && v2.length() > 0) { // DE specific?
gq.append(v2);
gq.append(' ');
}
gq.append(v);
}
if (((v = (String)_adr.valueForKey("country")) != null) && v.length() > 0) {
if (gq.length() > 0) gq.append(",");
gq.append(v);
}
return gq.length() > 0 ? gq.toString() : null;
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
}
--Apple-Mail-3-407133722--