public
Last active

Snippet code for parsing KML file from Google Maps

  • Download Gist
KML Snippet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/**
* Retrieve navigation data set from either remote URL or String
* @param url
* @return navigation set
*/
public static NavigationDataSet GetNavigationDataSet(String url) {
 
NavigationDataSet navigationDataSet = null;
try
{
if(LOG_LEVEL <= Log.DEBUG)Log.d(TAG, "url[" + url + "]");
final URL aUrl = new URL(url);
if(LOG_LEVEL <= Log.DEBUG)Log.d(TAG, "Connecting...");
final URLConnection conn = aUrl.openConnection();
// conn.setReadTimeout(15 * 1000); // timeout for reading the google maps data: 15 secs
conn.connect();
if(LOG_LEVEL <= Log.DEBUG)Log.d(TAG, "Connected...");
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
 
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
 
/* Create a new ContentHandler and apply it to the XML-Reader*/
NavigationSaxHandler navSax2Handler = new NavigationSaxHandler();
xr.setContentHandler(navSax2Handler);
if(LOG_LEVEL <= Log.DEBUG)Log.d(TAG, "Parse Stream");
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(aUrl.openStream()));
if(LOG_LEVEL <= Log.DEBUG)Log.d(TAG, "Get data frm Parser");
/* Our NavigationSaxHandler now provides the parsed data to us. */
navigationDataSet = navSax2Handler.getParsedData();
 
} catch (Exception e) {
if(LOG_LEVEL <= Log.ERROR)Log.e(TAG, "error getting route info", e);
navigationDataSet = null;
}
 
return navigationDataSet;
}
NavigationDataSet.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/*
* Copyright (c)2012 Poohdish Rattanavijai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.robgthai.lib.vo;
 
import java.util.ArrayList;
import java.util.Iterator;
 
/**
* Class representing Navigation path used in Google Maps
* @author poohdishr
*
*/
public class NavigationDataSet {
 
private ArrayList<Placemark> placemarks = new ArrayList<Placemark>();
private Placemark currentPlacemark;
private Placemark routePlacemark;
 
public String toString() {
String s= "";
for (Iterator<Placemark> iter=placemarks.iterator();iter.hasNext();) {
Placemark p = (Placemark)iter.next();
s += p.getTitle() + "\n" + p.getDescription() + "\n\n";
}
return s;
}
 
public void addCurrentPlacemark() {
placemarks.add(currentPlacemark);
}
 
public ArrayList<Placemark> getPlacemarks() {
return placemarks;
}
 
public void setPlacemarks(ArrayList<Placemark> placemarks) {
this.placemarks = placemarks;
}
 
public Placemark getCurrentPlacemark() {
return currentPlacemark;
}
 
public void setCurrentPlacemark(Placemark currentPlacemark) {
this.currentPlacemark = currentPlacemark;
}
 
public Placemark getRoutePlacemark() {
return routePlacemark;
}
 
public void setRoutePlacemark(Placemark routePlacemark) {
this.routePlacemark = routePlacemark;
}
 
}
NavigationSaxHandler.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
/*
* Copyright (c)2012 Poohdish Rattanavijai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.robgthai.lib.util;
 
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
import com.robgthai.lib.vo.NavigationDataSet;
import com.robgthai.lib.vo.Placemark;
 
 
@SuppressWarnings("unused")
public class NavigationSaxHandler extends DefaultHandler{
// private static final String TAG = NavigationSaxHandler.class.getSimpleName();
 
// ===========================================================
// Fields
// ===========================================================
 
private boolean in_kmltag = false;
private boolean in_placemarktag = false;
private boolean in_nametag = false;
private boolean in_descriptiontag = false;
private boolean in_geometrycollectiontag = false;
private boolean in_linestringtag = false;
private boolean in_pointtag = false;
private boolean in_coordinatestag = false;
 
private StringBuffer buffer;
 
private NavigationDataSet navigationDataSet = new NavigationDataSet();
 
// ===========================================================
// Getter & Setter
// ===========================================================
 
public NavigationDataSet getParsedData() {
navigationDataSet.getCurrentPlacemark().setCoordinates(buffer.toString().trim());
return this.navigationDataSet;
}
 
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
this.navigationDataSet = new NavigationDataSet();
}
 
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
 
/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("kml")) {
this.in_kmltag = true;
} else if (localName.equals("Placemark")) {
this.in_placemarktag = true;
// Log.d(TAG, "startElement localName[" + localName + "] qName[" + qName + "]");
navigationDataSet.setCurrentPlacemark(new Placemark());
} else if (localName.equals("name")) {
this.in_nametag = true;
} else if (localName.equals("description")) {
this.in_descriptiontag = true;
} else if (localName.equals("GeometryCollection")) {
this.in_geometrycollectiontag = true;
} else if (localName.equals("LineString")) {
this.in_linestringtag = true;
} else if (localName.equals("point")) {
this.in_pointtag = true;
} else if (localName.equals("coordinates")) {
buffer = new StringBuffer();
this.in_coordinatestag = true;
}
}
 
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("kml")) {
this.in_kmltag = false;
} else if (localName.equals("Placemark")) {
this.in_placemarktag = false;
 
// Log.d(TAG, "endElement localName[" + localName + "] qName[" + qName + "] Route[" + navigationDataSet.getCurrentPlacemark().getTitle() + "]");
if ("Route".equals(navigationDataSet.getCurrentPlacemark().getTitle()))
navigationDataSet.setRoutePlacemark(navigationDataSet.getCurrentPlacemark());
else navigationDataSet.addCurrentPlacemark();
 
} else if (localName.equals("name")) {
this.in_nametag = false;
} else if (localName.equals("description")) {
this.in_descriptiontag = false;
} else if (localName.equals("GeometryCollection")) {
this.in_geometrycollectiontag = false;
} else if (localName.equals("LineString")) {
this.in_linestringtag = false;
} else if (localName.equals("point")) {
this.in_pointtag = false;
} else if (localName.equals("coordinates")) {
this.in_coordinatestag = false;
}
}
 
/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
if(this.in_nametag){
if (navigationDataSet.getCurrentPlacemark()==null) navigationDataSet.setCurrentPlacemark(new Placemark());
navigationDataSet.getCurrentPlacemark().setTitle(new String(ch, start, length));
} else if(this.in_descriptiontag){
if (navigationDataSet.getCurrentPlacemark()==null) navigationDataSet.setCurrentPlacemark(new Placemark());
navigationDataSet.getCurrentPlacemark().setDescription(new String(ch, start, length));
} else if(this.in_coordinatestag){
if (navigationDataSet.getCurrentPlacemark()==null) navigationDataSet.setCurrentPlacemark(new Placemark());
// navigationDataSet.getCurrentPlacemark().setCoordinates(new String(ch, start, length));
buffer.append(ch, start, length);
}
}
}
Placemark.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
* Copyright (c)2012 Poohdish Rattanavijai
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.robgthai.lib.vo;
 
/**
* Class representing placemark used in Google Maps
* @author poohdishr
*
*/
public class Placemark {
 
String title;
String description;
String coordinates;
String address;
 
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCoordinates() {
return coordinates;
}
public void setCoordinates(String coordinates) {
this.coordinates = coordinates;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.