Basically I get a KML file via a specific URL which contains some Longitude and Latitude values I need in order to hook up to a drawable image using overlays on Google maps running on the android platform. I have a similar example of what I need but can't seem to be able to get it working to suit my situation
In the below code, it performs a GET request to get the KML file via the URL
private String[] getDirectionData(String srcPlace, String destPlace) {
String urlString = "http://www.scribblemaps.com/maps/kml/20001.kml";
Log.d("URL", urlString);
Document doc = null;
HttpURLConnection urlConnection = null;
URL url = null;
String pathConent = "";
try {
url = new URL(urlString.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(urlConnection.getInputStream());
} catch (Exception e) {
}
NodeList nl = doc.getElementsByTagName("LineString");
for (int s = 0; s < nl.getLength(); s++) {
Node rootNode = nl.item(s);
NodeList configItems = rootNode.getChildNodes();
for (int x = 0; x < configItems.getLength(); x++) {
Node lineStringNode = configItems.item(x);
NodeList path = lineStringNode.getChildNodes();
pathConent = path.item(0).getNodeValue();
}
}
String[] tempContent = pathConent.split(" ");
return tempContent;
}
This is the data that the code is interested in:
<LineString><altitudeMode>clampToGround</altitudeMode> <coordinates>-2.384764899505565,51.372219297926534,0 -2.3842928307189437,51.372781890066285,0 -2.3853442566527816,51.373089973308595,0 -2.3860523598327132,51.37216571736219,0 -2.3851511376037093,51.371817442165565,0 -2.3847005264892274,51.37227287842815,0</coordinates></LineString></Placemark></Document></kml>
as you can see above the values are listed with the latitude first, however the code converts the values the other way round as it should be the longitude first then the latitude.
and as you can see below the code is broken up into longitude and latitude to perform various actions.
public void getRouteDirection(Location location){
String pairs[] = getDirectionData("englishcombe", "bathwick");
String[] lngLat = pairs[0].split(",");
// STARTING POINT
GeoPoint startGP = new GeoPoint(
(int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double
.parseDouble(lngLat[0]) * 1E6));
myMC = myMapView.getController();
geoPoint = startGP;
myMC.setCenter(geoPoint);
myMC.setZoom(15);
myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));
// NAVIGATE THE PATH
GeoPoint gp1;
GeoPoint gp2 = startGP;
for (int i = 1; i < pairs.length; i++) {
lngLat = pairs[i].split(",");
gp1 = gp2;
// watch out! For GeoPoint, first:latitude, second:longitude
gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
(int) (Double.parseDouble(lngLat[0]) * 1E6));
myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
Log.d("xxx", "pair:" + pairs[i]);
}
// END POINT
myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));
myMapView.getController().animateTo(startGP);
myMapView.setBuiltInZoomControls(true);
myMapView.displayZoomControls(true);
}
And what I basically need the code to do, is break up the below data in a very similar way that the original code does
</Point></Placemark><Placemark scribbleType="6"><styleUrl>#style_0</styleUrl><Point> <coordinates>-2.385408629669139,51.36864265765638,0</coordinates></Point></Placemark><Placemark scribbleType="6"><styleUrl>#style_0</styleUrl><Point><coordinates>-2.384807814849823,51.37197818489322,0</coordinates></Point></Placemark></Document></kml>
so instead of this being performed
NodeList nl = doc.getElementsByTagName("LineString");
This should be performed
NodeList nl = doc.getElementsByTagName("Point");
to pull out the correct data and as you can see the comparison between value format of the LineString and the value format of the Point, as shown below<coordinates>-2.384764899505565,51.372219297926534,0 -2.3842928307189437,51.372781890066285,0 -2.3853442566527816......there are just comma that seperates between each value. From this (-2.384764899505565,51.372219297926534,0) the code loops and separates this value like this (51.372219, -2.384764) and so on to get the rest which is used to draw a path between all those coordinates, but in my case I need the code to perform this too but from a different KML layout >>
<coordinates>-2.385408629669139,51.36864265765638,0</coordinates> .... </coordinates></Point></Placemark><Placemark scribbleType="6"><styleUrl>#style_0</styleUrl><Point><coordinates>-2.384807814849823,51.37197818489322,0</coordinates>as you can see the difference this is separated by coordinates and other unwanted stuff so once I use the code above it only collect the first block of values and can not reach the rest of the values as they are not all separated by commas. I need the code to carry on looping to get all the values surrounded in <coordinates> the code should stop looping once it has collected all the values and formatted them in such as way again: (51.372219, -2.384764) Latitude being first then the longitude.
Thank you ever so much to whom that read this.

New Topic/Question
Reply




MultiQuote





|