3

我想知道如何在Google Maps V2上显示包含在KML文件中的路线?从谷歌地图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

YYYYYYYY - YYYYYYYYY - YYYYYYYY

zzzzzzzz - zzzzzzzzz - zzzzzzzz

但我无法在地图上显示它们。 你能帮我吗?

回答

4

我发现自己的问题!

在经度和交换纬度上面的代码...

这是的AsyncTask类做正确的事情:

temp.add(new LatLng(Double.parseDouble(vett[j].split("\\,")[**1**]),Double.parseDouble(vett[j].split("\\,")[**0**]))); 

在KML文件中的坐标提供在这种方式LONGITUDE - LATITUDE - ALTITUDE这就是为什么我交换索引!

对不起,这个问题!

+1

不好的KML。对此感到羞耻。你可以接受你自己的答案,所以想要帮助的人不会看到这个问题。 –

+0

KML文件不是我自己制作的... – starScream

+0

非常感谢你starScream为它保存的时间和工作没有任何错误的奇妙代码:) – jyomin