请您先登录,才能继续操作
[Groovy] google earth KML generator
right
2008-06-17
很土的 KML encoder, 只标记了 点/折线,颜色图标也很土
def writer = new StringWriter() def writer = new StringWriter() def kml = new groovy.xml.MarkupBuilder(writer) // def device = evdata[0].device kml.kml(xmlns: "http://earth.google.com/kml/2.2") { Folder { //name(device.imeiNumber + new Date().getTime() + ".kml") name("" + new Date().getTime() + ".kml") open(1) //polyline Placemark { name("autopath") Style { IconStyle { color("ff0000ff") scale(1) } LineStyle { color("ff0000ff") width(1) } PolyStyle { color("ff0000ff") } } LineString { coordinates( evdata.collect { event -> "" + event.longitude + "," + event.latitude + "," + "0," }.join() ) } } //polyline evdata.each{ event-> Placemark { name(1) Style { IconStyle { Icon { href("http://maps.google.com/mapfiles/kml/pal4/icon57.png") } } } //style Point { coordinates( "" + event.longitude + "," + event.latitude + "," + "0" ) } }//placemark } } } out.write(writer.toString()) |
|
right
2008-06-17
//完整文件
// 对于具有latitude/longitude/device 属性的eventdata的list,需要KML 文件 // 就可以这样写了。 // render eventdatalist as KML import grails.util.GrailsWebUtil import javax.servlet.http.HttpServletResponse import org.codehaus.groovy.grails.commons.ConfigurationHolder import org.codehaus.groovy.grails.web.converters.AbstractConverter import org.codehaus.groovy.grails.web.converters.Converter import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException /** * * FIXME add geozone * */ class KML extends AbstractConverter implements Converter { static String ct = 'application/vnd.google-earth.kml+xml kml' Eventdata[] object String encoding; static final String DEFAULT_ENCODING = "utf-8"; public KML() { Map config = ConfigurationHolder.getFlatConfig(); Object enc = config.get("grails.converters.encoding"); if ((enc != null) && (enc.toString().trim().length() > 0)) { this.encoding = enc.toString(); } else { this.encoding = DEFAULT_ENCODING; } } public void setTarget(Object arg0) { this.object = arg0; } public void render(Writer writer) throws ConverterException { def eds = this.object; this.writeEvents_KML(writer, eds) } boolean writeEvents_KML(out, evdata) throws IOException { def writer = new StringWriter() def kml = new groovy.xml.MarkupBuilder(writer) // def device = evdata[0].device kml.kml(xmlns: "http://earth.google.com/kml/2.2") { Folder { //name(device.imeiNumber + new Date().getTime() + ".kml") name("" + new Date().getTime() + ".kml") open(1) //polyline Placemark { name("autopath") Style { IconStyle { color("ff0000ff") scale(1) } LineStyle { color("ff0000ff") width(1) } PolyStyle { color("ff0000ff") } } LineString { coordinates( evdata.collect { event -> "" + event.longitude + "," + event.latitude + "," + "0," }.join() ) } } //polyline evdata.eachWithIndex{ event,index-> Placemark { name(index+1) Style { IconStyle { Icon { href("http://maps.google.com/mapfiles/kml/pal4/icon57.png") } } } //style Point { coordinates( "" + event.longitude + "," + event.latitude + "," + "0" ) } }//placemark } } } out.write(writer.toString()) return true; } void render(HttpServletResponse response) throws ConverterException { response.setContentType(GrailsWebUtil.getContentType(ct, this.encoding)); try { Writer writer = response.getWriter(); String encoding = response.getCharacterEncoding(); render(writer); } catch (IOException e) { throw new ConverterException(e); } } } |