Reputation: 741
I'd like to know how can I show a route contained in a KML file on Google Maps V2?
I'm able to extract the values of the filed "coordinates" in the KML file, but I don't know why my class doen't show the path into the map component.
This is my class:
public class Mappa extends FragmentActivity implements LocationListener {
private GoogleMap googleMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mappa);
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
if(status != ConnectionResult.SUCCESS) {
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}
else {
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
new LoadPath().execute(Environment.getExternalStorageDirectory().getPath() + "FILE.kml");
}
}
@Override
public void onLocationChanged(Location location) {
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
@Override
public vo id onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
private class LoadPath extends AsyncTask<String, Void, Void>{
Vector<PolylineOptions> path;
Vector<Vector<LatLng>> path_fragment;
Vector<Polyline> lines;
@Override
p rotected void onPreExecute() {
super.onPreExecute();
path_fragment = new Vector<Vector<LatLng>>();
path = new Vector <PolylineOptions>();
lines = new Vector<Polyline>();
}
@Override
protected Void doInBackground(String... params) {
try {
InputStream inputStream = new FileInputStream(params[0]);
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = docBuilder.parse(inputStream);
NodeList listCoordinateTag = null;
if (document == null) {
return null;
}
listCoordinateTag = document.getElementsByTagName("coordinates");
for (int i = 0; i < listCoordinateTag.getLength(); i++) {
String coordText = listCoordinateTag.item(i).getFirstChild().getNodeValue().trim();
String[] vett = coordText.split("\\ ");
Vector<LatLng> temp = new Vector<LatLng>();
for(int j=0; j < vett.length; j++){
temp.add(new LatLng(Double.parseDouble(vett[j].split("\\,")[0]),Double.parseDouble(vett[j].split("\\,")[1])));
}
path_fragment.add(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
googleMap.clear();
for(int i=0; i < path_fragment.size(); i++){
// Poliline options
PolylineOptions temp = new PolylineOptions();
for(int j=0; j< path_fragment.get(i).size(); j++)
temp.add(path_fragment.get(i).get(j));
path.add(temp);
}
for(int i = 0; i < path.size(); i++)
lines.add(googleMap.addPolyline(path.get(i)));
for(int i = 0; i < lines.size(); i++){
lines.get(i).setWidth(4);
lines.get(i).setColor(Color.RED);
lines.get(i).setGeodesic(true);
lines.get(i).setVisible(true);
}
}
}
}
In this way, I can get all the different coordinates int the following way:
Latitude - Longitude - Altitude
xxxxxxxx - xxxxxxxxx - xxxxxxxx
yyyyyyyy - yyyyyyyyy - yyyyyyyy
zzzzzzzz - zzzzzzzzz - zzzzzzzz
But I'm not able to show them on the map. Can you help me please?
Upvotes: 3
Views: 2531
Reputation: 741
I found the problem from myself!
In the code above the Longitude and the Latitude are exchanged...
This is the right thing to do in the AsyncTask class:
temp.add(new LatLng(Double.parseDouble(vett[j].split("\\,")[**1**]),Double.parseDouble(vett[j].split("\\,")[**0**])));
IN A KML FILE THE COORDINATES ARE GIVEN IN THIS WAY LONGITUDE - LATITUDE - ALTITUDE this is why I exchanged the indexes!
Sorry for the question!
Upvotes: 4