/*
* Copyright 2012 osmdroid: M.Kergall
* Copyright 2012 Hannes Janetzek
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see .
*/
package org.oscim.app;
import android.app.Activity;
import android.location.Address;
import android.os.AsyncTask;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.oscim.android.canvas.AndroidGraphics;
import org.oscim.core.GeoPoint;
import org.oscim.layers.PathLayer;
import org.oscim.layers.marker.MarkerInterface;
import org.oscim.layers.marker.MarkerSymbol;
import org.oscim.layers.marker.MarkerSymbol.HotspotPlace;
import org.osmdroid.location.GeocoderNominatim;
import org.osmdroid.overlays.DefaultInfoWindow;
import org.osmdroid.overlays.ExtendedMarkerItem;
import org.osmdroid.overlays.ItemizedOverlayWithBubble;
import org.osmdroid.routing.Route;
import org.osmdroid.routing.RouteProvider;
import org.osmdroid.routing.provider.OSRMRouteProvider;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class RouteSearch {
private static int START_INDEX = -2, DEST_INDEX = -1;
private final PathLayer mRouteOverlay;
//private final ItemizedOverlayWithBubble mRouteMarkers;
private final ItemizedOverlayWithBubble mItineraryMarkers;
private final RouteBar mRouteBar;
private GeoPoint mStartPoint, mDestinationPoint;
private final ArrayList mViaPoints;
private ExtendedMarkerItem markerStart, markerDestination;
private UpdateRouteTask mRouteTask;
RouteSearch() {
mViaPoints = new ArrayList();
// Itinerary markers:
ArrayList waypointsItems = new ArrayList<>();
mItineraryMarkers = new ItemizedOverlayWithBubble(App.map,
App.activity,
null,
waypointsItems,
new ViaPointInfoWindow(R.layout.itinerary_bubble));
//updateIternaryMarkers();
//Route and Directions
//ArrayList routeItems = new ArrayList();
//mRouteMarkers = new ItemizedOverlayWithBubble(App.map, App.activity,
// null, routeItems);
mRouteOverlay = new PathLayer(App.map, 0xAA0000FF, 3);
// TODO use LayerGroup
App.map.layers().add(mRouteOverlay);
//App.map.getOverlays().add(mRouteMarkers);
App.map.layers().add(mItineraryMarkers);
mRouteBar = new RouteBar(App.activity);
}
/**
* Retrieve route between p1 and p2 and update overlays.
*/
public void showRoute(GeoPoint p1, GeoPoint p2) {
clearOverlays();
mStartPoint = p1;
markerStart = putMarkerItem(markerStart, mStartPoint, START_INDEX,
R.string.departure, R.drawable.marker_departure, -1);
mDestinationPoint = p2;
markerDestination = putMarkerItem(markerDestination, mDestinationPoint, DEST_INDEX,
R.string.destination,
R.drawable.marker_destination, -1);
getRouteAsync();
}
/**
* Reverse Geocoding
*/
public String getAddress(GeoPoint p) {
GeocoderNominatim geocoder = new GeocoderNominatim(App.activity);
String theAddress;
try {
double dLatitude = p.getLatitude();
double dLongitude = p.getLongitude();
List addresses = geocoder.getFromLocation(dLatitude, dLongitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
int n = address.getMaxAddressLineIndex();
for (int i = 0; i <= n; i++) {
if (i != 0)
sb.append(", ");
sb.append(address.getAddressLine(i));
}
theAddress = new String(sb.toString());
} else {
theAddress = null;
}
} catch (IOException e) {
theAddress = null;
}
if (theAddress != null) {
return theAddress;
}
return "";
}
// Async task to reverse-geocode the marker position in a separate thread:
class GeocodingTask extends AsyncTask