在Google地图V2上从KML文件绘制路径

3

我想知道如何在Google Maps V2上显示包含在KML文件中的路线?

我能够提取KML文件中“坐标”字段的值,但我不知道为什么我的类不能在地图组件中显示路径。

这是我的类:

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-xxxxxxxxx-xxxxxxxx
yyyyyyyyy-yyyyyyyyy-yyyyyyyy
zzzzzzzzz-zzzzzzzzz-zzzzzzzz
但是,我无法在地图上显示它们。您能帮我吗?
1个回答

4
我从自己身上找到了问题!
在上面的代码中,经度和纬度被交换了...
在AsyncTask类中,正确的做法是这样的:
temp.add(new LatLng(Double.parseDouble(vett[j].split("\\,")[**1**]),Double.parseDouble(vett[j].split("\\,")[**0**])));

在KML文件中,坐标的顺序是经度-纬度-高度,这就是为什么我交换了索引!很抱歉问了这个问题!

1
糟糕的KML,真是太糟糕了。你可以接受自己的答案,这样想要帮助的人就不会再看这个问题了。 - MaciejGórski
非常感谢StarScream提供的精彩代码,它节省了我的时间并且没有出现任何错误 :) - jyomin
很高兴听到这个消息!谢谢jyomin。 - starscream

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接