HelloGoogleMaps.java :  » App » ski-on-sofa » com » example » HelloGoogleMaps » Android Open Source

Android Open Source » App » ski on sofa 
ski on sofa » com » example » HelloGoogleMaps » HelloGoogleMaps.java
package com.example.HelloGoogleMaps;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class HelloGoogleMaps extends MapActivity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ///setContentView(R.layout.directionmap);
            setContentView(R.layout.main);

            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( Double.toString(42.350080));
            urlString.append(",");
            ///urlString.append( Double.toString(lastKnownLocation.getLongitude() ));
            urlString.append( Double.toString(1.900205));
            urlString.append("&daddr=");//to
            urlString.append( Double.toString((double)42500000/1.0E6 ));
            urlString.append(",");
            urlString.append( Double.toString((double)1950000/1.0E6 ));
            urlString.append("&ie=UTF8&0&om=0&output=kml");
            Log.d("MapActivity onCreate", "url=" + urlString.toString());
            
            try{
                // setup the url
                /// URL url = new URL(urlString.toString());
              /// URL url = new URL("http://dl.dropbox.com/u/4275724/Masella_route.kml");
              /// URL url = new URL("http://dl.dropbox.com/u/4275724/Masella_2routes.kml");
              URL url = new URL("http://dl.dropbox.com/u/4275724/test_ruta_automatica.kml");
              /// El siguiente codigo falla debido a que se ha aadido el interlineado al kml
              ///URL url = new URL("http://dl.dropbox.com/u/4275724/test_ruta_automatica_SOLO_Route_LineString.kml");
              
                // 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 );              
                
                ///GeoPoint destPoint = new GeoPoint(dest[0],dest[1]);
                GeoPoint destPoint = new GeoPoint(42500000,1950000);
                ///GeoPoint currentPoint = new GeoPoint( new Double(lastKnownLocation.getLatitude()*1E6).intValue()
                ///                                    ,new Double(lastKnownLocation.getLongitude()*1E6).intValue() );
                GeoPoint currentPoint = new GeoPoint(42350080,1900205);              
                /// Drawable dot = this.getResources().getDrawable(R.drawable.pixel);
                Drawable dot = this.getResources().getDrawable(R.drawable.neu);
                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.");
                Log.d("DirectionMap","error: "+e.toString());
            }
    }
        
    /**
     * Does the actual drawing of the route, based on the geo points provided in the nav set
     *
     * @param navSet     Navigation set bean that holds the route information, incl. geo pos
     * @param color      Color in which to draw the lines
     * @param mMapView01 Map view to draw onto
     */
    public void drawPath(NavigationDataSet navSet, int color, MapView mMapView01) {

        Log.d("drawPath", "map color before: " + color);

        // color correction for dining, make it darker
        if (color == Color.parseColor("#add331")) color = Color.parseColor("#6C8715");
        Log.d("drawPath", "map color after: " + color);

        Collection overlaysToAddAgain = new ArrayList();
        for (Iterator iter = mMapView01.getOverlays().iterator(); iter.hasNext();) {
            Object o = iter.next();
            Log.d("drawPath", "overlay type: " + o.getClass().getName());
            if (!RouteOverlay.class.getName().equals(o.getClass().getName())) {
                // mMapView01.getOverlays().remove(o);
                overlaysToAddAgain.add(o);
            }
        }
        mMapView01.getOverlays().clear();
        mMapView01.getOverlays().addAll(overlaysToAddAgain);

        String path = navSet.getRoutePlacemark().getCoordinates();
        Log.d("drawPath", "path=" + path);
        if (path != null && path.trim().length() > 0) {
            String[] pairs = path.trim().split(" ");

            Log.d("drawPath", "pairs.length=" + pairs.length);

            String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude lngLat[2]=height

            Log.d("drawPath", "lnglat =" + lngLat + ", length: " + lngLat.length);

            if (lngLat.length<3) lngLat = pairs[1].split(","); // if first pair is not transferred completely, take seconds pair //TODO 

            try {
                GeoPoint startGP = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double.parseDouble(lngLat[0]) * 1E6));
                mMapView01.getOverlays().add(new RouteOverlay(startGP, startGP, 1));
                GeoPoint gp1;
                GeoPoint gp2 = startGP;

                for (int i = 1; i < pairs.length; i++) // the last one would be crash
                {
                    lngLat = pairs[i].split(",");

                    gp1 = gp2;

                    if (lngLat.length >= 2 && gp1.getLatitudeE6() > 0 && gp1.getLongitudeE6() > 0
                            && gp2.getLatitudeE6() > 0 && gp2.getLongitudeE6() > 0) {

                        // for GeoPoint, first:latitude, second:longitude
                        gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double.parseDouble(lngLat[0]) * 1E6));

                        if (gp2.getLatitudeE6() != 22200000) { 
                            mMapView01.getOverlays().add(new RouteOverlay(gp1, gp2, 2, color));
                            Log.d("drawPath", "draw:" + gp1.getLatitudeE6() + "/" + gp1.getLongitudeE6() + " TO " + gp2.getLatitudeE6() + "/" + gp2.getLongitudeE6());
                        }
                    }
                    Log.d("drawPath","pair:" + pairs[i]);
                }
                //routeOverlays.add(new RouteOverlay(gp2,gp2, 3));
                mMapView01.getOverlays().add(new RouteOverlay(gp2, gp2, 3));
            } catch (NumberFormatException e) {
                Log.e("drawPath", "Cannot draw route.", e);
            }
        }
        // mMapView01.getOverlays().addAll(routeOverlays); // use the default color
        mMapView01.setEnabled(true);
    }  
    
        @Override
        protected boolean isRouteDisplayed() {
            return false;
        }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.