I want to know a simple and easy way to parse KML file and store its data in an object so that I can access its data instantly here is my kml file
You can use KmlContainer
from Google Maps KML Importing Utility to access any property in a container:
...
KmlLayer layer = new KmlLayer(getMap(), kmlInputStream, getApplicationContext());
Iterable containers = layer.getContainers();
for (KmlContainer container : containers ) {
if (container.hasProperty("property_name")) {
// process property
Log.d(TAG, "" + container.getProperty("property_name"));
}
}
...
For exactly yours kml
file for standard geometry you can use something like this:
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(17.425868, 78.459761), 16));
// change next line for your kml source
InputStream kmlInputStream = getResources().openRawResource(R.raw.data);
try {
KmlLayer kmlLayer = new KmlLayer(mGoogleMap, kmlInputStream, getApplicationContext());
kmlLayer.addLayerToMap();
ArrayList<LatLng> pathPoints = new ArrayList();
if (kmlLayer != null && kmlLayer.getContainers() != null) {
for (KmlContainer container : kmlLayer.getContainers()) {
if (container.hasPlacemarks()) {
for (KmlPlacemark placemark : container.getPlacemarks()) {
Geometry geometry = placemark.getGeometry();
if (geometry.getGeometryType().equals("Point")) {
KmlPoint point = (KmlPoint) placemark.getGeometry();
LatLng latLng = new LatLng(point.getGeometryObject().latitude, point.getGeometryObject().longitude);
pathPoints.add(latLng);
} else if (geometry.getGeometryType().equals("LineString")) {
KmlLineString kmlLineString = (KmlLineString) geometry;
ArrayList<LatLng> coords = kmlLineString.getGeometryObject();
for (LatLng latLng : coords) {
pathPoints.add(latLng);
}
}
}
}
}
for (LatLng latLng : pathPoints) {
mGoogleMap.addMarker(new MarkerOptions().position(latLng));
}
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
and get something like that:
but for <ExtendedData>
you should use external library with KML parsing support like GeoTools or parse your KML file as XML.