Android Open Source - smartnavi Google Road Manager






From Project

Back to project page smartnavi.

License

The source code is released under:

Apache License

If you think the Android project smartnavi listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.osmdroid.bonuspack.routing;
/*from w  w w  .j a v a 2 s .c  om*/
import android.util.Log;

import org.osmdroid.bonuspack.utils.BonusPackHelper;
import org.osmdroid.bonuspack.utils.HttpConnection;
import org.osmdroid.bonuspack.utils.PolylineEncoder;
import org.osmdroid.util.BoundingBoxE6;
import org.osmdroid.util.GeoPoint;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Locale;

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

/**
 * class to get a route between a start and a destination point,
 * going through a list of waypoints. <br>
 * https://developers.google.com/maps/documentation/directions/<br>
 * Note that displaying a route provided by Google on a non-Google map (like OSM) is not allowed by Google T&C.
 *
 * @author M.Kergall
 *         TODO: replace GoogleDirectionsHandler mString by a StringBuilder
 */
public class GoogleRoadManager extends RoadManager {

    static final String GOOGLE_DIRECTIONS_SERVICE = "http://maps.googleapis.com/maps/api/directions/xml?";

    /**
     * Build the URL to Google Directions service returning a route in XML format
     */
    protected String getUrl(ArrayList<GeoPoint> waypoints) {
        StringBuffer urlString = new StringBuffer(GOOGLE_DIRECTIONS_SERVICE);
        urlString.append("origin=");
        GeoPoint p = waypoints.get(0);
        urlString.append(geoPointAsString(p));
        urlString.append("&destination=");
        int destinationIndex = waypoints.size() - 1;
        p = waypoints.get(destinationIndex);
        urlString.append(geoPointAsString(p));

        for (int i = 1; i < destinationIndex; i++) {
            if (i == 1)
                urlString.append("&waypoints=");
            else
                urlString.append("%7C"); // the pipe (|), url-encoded
            p = waypoints.get(i);
            urlString.append(geoPointAsString(p));
        }
        urlString.append("&units=metric&sensor=false");
        Locale locale = Locale.getDefault();
        urlString.append("&language=" + locale.getLanguage());
        urlString.append(mOptions);
        return urlString.toString();
    }

    /**
     * @param waypoints list of GeoPoints. Must have at least 2 entries, start and end points.
     * @return the road
     */
    @Override
    public Road getRoad(ArrayList<GeoPoint> waypoints) {
        String url = getUrl(waypoints);
        Log.d(BonusPackHelper.LOG_TAG, "GoogleRoadManager.getRoad:" + url);
        Road road = null;
        HttpConnection connection = new HttpConnection();
        connection.doGet(url);
        InputStream stream = connection.getStream();
        if (stream != null)
            road = getRoadXML(stream);
        connection.close();
        if (road == null || road.mRouteHigh.size() == 0) {
            //Create default road:
            road = new Road(waypoints);
        } else {
            //finalize road data update:
            for (RoadLeg leg : road.mLegs) {
                road.mDuration += leg.mDuration;
                road.mLength += leg.mLength;
            }
            road.mStatus = Road.STATUS_OK;
        }
        Log.d(BonusPackHelper.LOG_TAG, "GoogleRoadManager.getRoad - finished");
        return road;
    }

    protected Road getRoadXML(InputStream is) {
        GoogleDirectionsHandler handler = new GoogleDirectionsHandler();
        try {
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
            parser.parse(is, handler);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return handler.mRoad;
    }

}

class GoogleDirectionsHandler extends DefaultHandler {
    Road mRoad;
    RoadLeg mLeg;
    RoadNode mNode;
    boolean isPolyline, isOverviewPolyline, isLeg, isStep, isDuration, isDistance, isBB;
    int mValue;
    double mLat, mLng;
    double mNorth, mWest, mSouth, mEast;
    private StringBuilder mStringBuilder = new StringBuilder(1024);

    public GoogleDirectionsHandler() {
        isOverviewPolyline = isBB = isPolyline = isLeg = isStep = isDuration = isDistance = false;
        mRoad = new Road();
    }

    public void startElement(String uri, String localName, String name,
                             Attributes attributes) throws SAXException {
        if (localName.equals("polyline")) {
            isPolyline = true;
        } else if (localName.equals("overview_polyline")) {
            isOverviewPolyline = true;
        } else if (localName.equals("leg")) {
            mLeg = new RoadLeg();
            isLeg = true;
        } else if (localName.equals("step")) {
            mNode = new RoadNode();
            isStep = true;
        } else if (localName.equals("duration")) {
            isDuration = true;
        } else if (localName.equals("distance")) {
            isDistance = true;
        } else if (localName.equals("bounds")) {
            isBB = true;
        }
        mStringBuilder.setLength(0);
    }

    /**
     * Overrides org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
     */
    public
    @Override
    void characters(char[] ch, int start, int length)
            throws SAXException {
        mStringBuilder.append(ch, start, length);
    }

    public void endElement(String uri, String localName, String name)
            throws SAXException {
        if (localName.equals("points")) {
            if (isPolyline) {
                //detailed piece of road for the step, to add:
                ArrayList<GeoPoint> polyLine = PolylineEncoder.decode(mStringBuilder.toString(), 10);
                mRoad.mRouteHigh.addAll(polyLine);
            } else if (isOverviewPolyline) {
                //low-def polyline for the whole road:
                mRoad.setRouteLow(PolylineEncoder.decode(mStringBuilder.toString(), 10));
            }
        } else if (localName.equals("polyline")) {
            isPolyline = false;
        } else if (localName.equals("overview_polyline")) {
            isOverviewPolyline = false;
        } else if (localName.equals("value")) {
            mValue = Integer.parseInt(mStringBuilder.toString());
        } else if (localName.equals("duration")) {
            if (isStep)
                mNode.mDuration = mValue;
            else
                mLeg.mDuration = mValue;
            isDuration = false;
        } else if (localName.equals("distance")) {
            if (isStep)
                mNode.mLength = mValue / 1000.0;
            else
                mLeg.mLength = mValue / 1000.0;
            isDistance = false;
        } else if (localName.equals("html_instructions")) {
            if (isStep) {
                String value = mStringBuilder.toString();
                //value = value.replaceAll("<[^>]*>", " "); //remove everything in <...>
                //value = value.replaceAll("&nbsp;", " ");
                mNode.mInstructions = value;
                //Log.d(BonusPackHelper.LOG_TAG, mString);
            }
        } else if (localName.equals("start_location")) {
            if (isStep)
                mNode.mLocation = new GeoPoint(mLat, mLng);
        } else if (localName.equals("step")) {
            mRoad.mNodes.add(mNode);
            isStep = false;
        } else if (localName.equals("leg")) {
            mRoad.mLegs.add(mLeg);
            isLeg = false;
        } else if (localName.equals("lat")) {
            mLat = Double.parseDouble(mStringBuilder.toString());
        } else if (localName.equals("lng")) {
            mLng = Double.parseDouble(mStringBuilder.toString());
        } else if (localName.equals("northeast")) {
            if (isBB) {
                mNorth = mLat;
                mEast = mLng;
            }
        } else if (localName.equals("southwest")) {
            if (isBB) {
                mSouth = mLat;
                mWest = mLng;
            }
        } else if (localName.equals("bounds")) {
            mRoad.mBoundingBox = new BoundingBoxE6(mNorth, mEast, mSouth, mWest);
            isBB = false;
        }
    }

}




Java Source Code List

com.ilm.sandwich.BackgroundService.java
com.ilm.sandwich.BuildConfig.java
com.ilm.sandwich.Config.java
com.ilm.sandwich.GoogleMapActivity.java
com.ilm.sandwich.Info.java
com.ilm.sandwich.MySupportMapFragment.java
com.ilm.sandwich.OsmMapActivity.java
com.ilm.sandwich.Settings.java
com.ilm.sandwich.StartChooser.java
com.ilm.sandwich.TouchableWrapper.java
com.ilm.sandwich.tools.Core.java
com.ilm.sandwich.tools.HttpRequests.java
com.ilm.sandwich.tools.Locationer.java
com.ilm.sandwich.tools.MapDownload.java
com.ilm.sandwich.tools.MyItemizedOverlay.java
com.ilm.sandwich.tools.PlacesAutoComplete.java
com.ilm.sandwich.tools.PlacesTextSearch.java
com.ilm.sandwich.tools.Statistics.java
com.ilm.sandwich.tools.SuggestionsAdapter.java
org.osmdroid.bonuspack.BuildConfig.java
org.osmdroid.bonuspack.BuildConfig.java
org.osmdroid.bonuspack.cachemanager.CacheManager.java
org.osmdroid.bonuspack.clustering.GridMarkerClusterer.java
org.osmdroid.bonuspack.clustering.MarkerClusterer.java
org.osmdroid.bonuspack.clustering.StaticCluster.java
org.osmdroid.bonuspack.kml.ColorStyle.java
org.osmdroid.bonuspack.kml.IconStyle.java
org.osmdroid.bonuspack.kml.KmlDocument.java
org.osmdroid.bonuspack.kml.KmlFeature.java
org.osmdroid.bonuspack.kml.KmlFolder.java
org.osmdroid.bonuspack.kml.KmlGeometry.java
org.osmdroid.bonuspack.kml.KmlGroundOverlay.java
org.osmdroid.bonuspack.kml.KmlLineString.java
org.osmdroid.bonuspack.kml.KmlMultiGeometry.java
org.osmdroid.bonuspack.kml.KmlPlacemark.java
org.osmdroid.bonuspack.kml.KmlPoint.java
org.osmdroid.bonuspack.kml.KmlPolygon.java
org.osmdroid.bonuspack.kml.LineStyle.java
org.osmdroid.bonuspack.kml.Style.java
org.osmdroid.bonuspack.location.FlickrPOIProvider.java
org.osmdroid.bonuspack.location.GeoNamesPOIProvider.java
org.osmdroid.bonuspack.location.GeocoderGisgraphy.java
org.osmdroid.bonuspack.location.GeocoderNominatimOld.java
org.osmdroid.bonuspack.location.GeocoderNominatim.java
org.osmdroid.bonuspack.location.NominatimPOIProvider.java
org.osmdroid.bonuspack.location.POI.java
org.osmdroid.bonuspack.location.PicasaPOIProvider.java
org.osmdroid.bonuspack.mapsforge.GenericMapView.java
org.osmdroid.bonuspack.mapsforge.MapsForgeTileModuleProvider.java
org.osmdroid.bonuspack.mapsforge.MapsForgeTileProvider.java
org.osmdroid.bonuspack.mapsforge.MapsForgeTileSource.java
org.osmdroid.bonuspack.overlays.DefaultInfoWindow.java
org.osmdroid.bonuspack.overlays.ExtendedOverlayItem.java
org.osmdroid.bonuspack.overlays.FolderOverlay.java
org.osmdroid.bonuspack.overlays.GroundOverlay.java
org.osmdroid.bonuspack.overlays.InfoWindow.java
org.osmdroid.bonuspack.overlays.ItemizedOverlayWithBubble.java
org.osmdroid.bonuspack.overlays.MapEventsOverlay.java
org.osmdroid.bonuspack.overlays.MapEventsReceiver.java
org.osmdroid.bonuspack.overlays.MarkerInfoWindow.java
org.osmdroid.bonuspack.overlays.Marker.java
org.osmdroid.bonuspack.overlays.Polygon.java
org.osmdroid.bonuspack.overlays.Polyline.java
org.osmdroid.bonuspack.routing.GoogleRoadManager.java
org.osmdroid.bonuspack.routing.MapQuestRoadManager.java
org.osmdroid.bonuspack.routing.OSRMRoadManager.java
org.osmdroid.bonuspack.routing.RoadLeg.java
org.osmdroid.bonuspack.routing.RoadManager.java
org.osmdroid.bonuspack.routing.RoadNode.java
org.osmdroid.bonuspack.routing.Road.java
org.osmdroid.bonuspack.utils.BonusPackHelper.java
org.osmdroid.bonuspack.utils.DouglasPeuckerReducer.java
org.osmdroid.bonuspack.utils.HttpConnection.java
org.osmdroid.bonuspack.utils.PolylineEncoder.java
org.osmdroid.bonuspack.utils.WebImageCache.java