How to draw a path on a map using kml file?

前端 未结 4 674
温柔的废话
温柔的废话 2020-11-22 01:35

Can I parse kml file in order to display paths or points in Android? Please could you help me with that?

This is kml sample code which I would like to display in and

4条回答
  •  没有蜡笔的小新
    2020-11-22 01:53

    Thank Mathias Lin, tested and it works!

    In addition, sample implementation of Mathias's method in activity can be as follows.

    public class DirectionMapActivity extends MapActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.directionmap);
    
            MapView mapView = (MapView) findViewById(R.id.mapview);
            mapView.setBuiltInZoomControls(true);
    
            // Acquire a reference to the system Location Manager
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    
            String locationProvider = LocationManager.NETWORK_PROVIDER;
            Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
    
            StringBuilder urlString = new StringBuilder();
            urlString.append("http://maps.google.com/maps?f=d&hl=en");
            urlString.append("&saddr=");//from
            urlString.append( Double.toString(lastKnownLocation.getLatitude() ));
            urlString.append(",");
            urlString.append( Double.toString(lastKnownLocation.getLongitude() ));
            urlString.append("&daddr=");//to
            urlString.append( Double.toString((double)dest[0]/1.0E6 ));
            urlString.append(",");
            urlString.append( Double.toString((double)dest[1]/1.0E6 ));
            urlString.append("&ie=UTF8&0&om=0&output=kml");
    
            try{
                // setup the url
                URL url = new URL(urlString.toString());
                // create the factory
                SAXParserFactory factory = SAXParserFactory.newInstance();
                // create a parser
                SAXParser parser = factory.newSAXParser();
                // create the reader (scanner)
                XMLReader xmlreader = parser.getXMLReader();
                // instantiate our handler
                NavigationSaxHandler navSaxHandler = new NavigationSaxHandler();
                // assign our handler
                xmlreader.setContentHandler(navSaxHandler);
                // get our data via the url class
                InputSource is = new InputSource(url.openStream());
                // perform the synchronous parse           
                xmlreader.parse(is);
                // get the results - should be a fully populated RSSFeed instance, or null on error
                NavigationDataSet ds = navSaxHandler.getParsedData();
    
                // draw path
                drawPath(ds, Color.parseColor("#add331"), mapView );
    
                // find boundary by using itemized overlay
                GeoPoint destPoint = new GeoPoint(dest[0],dest[1]);
                GeoPoint currentPoint = new GeoPoint( new Double(lastKnownLocation.getLatitude()*1E6).intValue()
                                                    ,new Double(lastKnownLocation.getLongitude()*1E6).intValue() );
    
                Drawable dot = this.getResources().getDrawable(R.drawable.pixel);
                MapItemizedOverlay bgItemizedOverlay = new MapItemizedOverlay(dot,this);
                OverlayItem currentPixel = new OverlayItem(destPoint, null, null );
                OverlayItem destPixel = new OverlayItem(currentPoint, null, null );
                bgItemizedOverlay.addOverlay(currentPixel);
                bgItemizedOverlay.addOverlay(destPixel);
    
                // center and zoom in the map
                MapController mc = mapView.getController();
                mc.zoomToSpan(bgItemizedOverlay.getLatSpanE6()*2,bgItemizedOverlay.getLonSpanE6()*2);
                mc.animateTo(new GeoPoint(
                        (currentPoint.getLatitudeE6() + destPoint.getLatitudeE6()) / 2
                        , (currentPoint.getLongitudeE6() + destPoint.getLongitudeE6()) / 2));
    
            } catch(Exception e) {
                Log.d("DirectionMap","Exception parsing kml.");
            }
    
        }
        // and the rest of the methods in activity, e.g. drawPath() etc...
    

    MapItemizedOverlay.java

    public class MapItemizedOverlay extends ItemizedOverlay{
        private ArrayList mOverlays = new ArrayList();
        private Context mContext;
    
        public MapItemizedOverlay(Drawable defaultMarker, Context context) {
              super(boundCenterBottom(defaultMarker));
              mContext = context;
        }
    
        public void addOverlay(OverlayItem overlay) {
            mOverlays.add(overlay);
            populate();
        }
    
        @Override
        protected OverlayItem createItem(int i) {
          return mOverlays.get(i);
        }
    
        @Override
        public int size() {
          return mOverlays.size();
        }
    
    }
    

提交回复
热议问题