我想知道如何在google maps v2上显示包含在kml文件中的路由?
我可以提取kml文件中字段“coordinates”的值,但我不知道为什么我的类不显示映射组件的路径。
这是我的课:
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);
}
}
}
}
这样,我可以用以下方法得到所有不同的坐标:
经纬度高度
xxxxxxxx-xxxxxxxx-xxxxxxxx
yyyyyyyy-yyyyyyyyyyyy-yyyyyyyyyyyyy
zzzzzzz-zzzzzzz-zzzzzzz
但我不能在地图上显示。
你能帮帮我吗?
最佳答案
我自己发现了问题!
在经纬度以上的代码中是交换的…
在asynctask类中,这样做是正确的:
temp.add(new LatLng(Double.parseDouble(vett[j].split("\\,")[**1**]),Double.parseDouble(vett[j].split("\\,")[**0**])));
在kml文件中,坐标是以这种方式给出的经纬度高度这就是我交换索引的原因!
对不起这个问题!