/*******************************************************************************
* Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package fr.gotorennes;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import fr.gotorennes.util.BackgroundTask;
import fr.gotorennes.view.MapDrawable;
public class ItineraireMapActivity extends AbstractMapActivity {
double latitude;
double longitude;
double latitudeDest;
double longitudeDest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected String getTrackingName() {
return "carteItineraire";
}
@Override
protected void init() {
Intent intent = getIntent();
latitude = intent.getDoubleExtra("latitude", 0);
longitude = intent.getDoubleExtra("longitude", 0);
final Location origin = new Location(latitude, longitude, "");
latitudeDest = intent.getDoubleExtra("latitudeDest", 0);
longitudeDest = intent.getDoubleExtra("longitudeDest", 0);
final Location dest = new Location(latitudeDest, longitudeDest, "");
final int icon = intent.getIntExtra("icon", 0);
final int iconDest = intent.getIntExtra("iconDest", 0);
BackgroundTask<Route> backgroundTask = new BackgroundTask<Route>() {
@Override
protected Route execute() {
return getRoute(origin, dest);
}
@Override
protected void callback(Route result) {
Location locInit = result.locations.get(0);
GeoPoint gpInit = new GeoPoint((int) (locInit.latitude * 1E6), (int) (locInit.longitude * 1E6));
mapView.getOverlays().add(new RouteOverlay(gpInit, gpInit, 1));
if (icon != 0) {
MapItemOverlay overlay = new MapItemOverlay(new MapDrawable(getApplicationContext(), icon));
overlay.addItem(new MapLocationOverlay("", locInit.latitude, locInit.longitude, null));
mapView.getOverlays().add(overlay);
}
GeoPoint gp1;
GeoPoint gp2 = gpInit;
for (int i = 1; i < result.locations.size(); i++) {
gp1 = gp2;
Location loc = result.locations.get(i);
gp2 = new GeoPoint((int) (loc.latitude * 1E6), (int) (loc.longitude * 1E6));
mapView.getOverlays().add(new RouteOverlay(gp1, gp2, 2));
}
final GeoPoint gpFinal = new GeoPoint((int) (latitudeDest * 1E6), (int) (longitudeDest * 1E6));
mapView.getOverlays().add(new RouteOverlay(gp2, gpFinal, 3));
if (iconDest != 0) {
MapItemOverlay overlay = new MapItemOverlay(new MapDrawable(getApplicationContext(), iconDest));
overlay.addItem(new MapLocationOverlay("", latitudeDest, longitudeDest, null));
mapView.getOverlays().add(overlay);
}
mapController.setZoom(18);
mapController.animateTo(new GeoPoint((int) (latitude * 1e6), (int) (longitude * 1e6)));
}
};
backgroundTask.start(this);
}
public static class BitmapOverlay extends Overlay {
private GeoPoint geoPoint;
private Bitmap bitmap;
public BitmapOverlay(GeoPoint geoPoint, Bitmap bitmap) {
super();
this.geoPoint = geoPoint;
this.bitmap = bitmap;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
Point point = new Point();
Projection projection = mapView.getProjection();
projection.toPixels(geoPoint, point);
Bitmap scaled = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
canvas.drawBitmap(scaled, point.x - scaled.getWidth() / 2, point.y - scaled.getHeight(), new Paint());
return super.draw(canvas, mapView, shadow, when);
}
}
public static class RouteOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
private int mRadius = 6;
private int mode = 0;
public RouteOverlay(GeoPoint gp1, GeoPoint gp2, int mode) {
this.gp1 = gp1;
this.gp2 = gp2;
this.mode = mode;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
Projection projection = mapView.getProjection();
if (shadow == false) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Point point = new Point();
projection.toPixels(gp1, point);
if (mode == 1) {
RectF oval = new RectF(point.x - mRadius, point.y - mRadius, point.x + mRadius, point.y + mRadius);
canvas.drawOval(oval, paint);
} else if (mode == 2) {
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
} else if (mode == 3) {
Point point2 = new Point();
projection.toPixels(gp2, point2);
paint.setStrokeWidth(5);
paint.setAlpha(120);
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
RectF oval = new RectF(point2.x - mRadius, point2.y - mRadius, point2.x + mRadius, point2.y + mRadius);
paint.setAlpha(255);
canvas.drawOval(oval, paint);
}
}
return super.draw(canvas, mapView, shadow, when);
}
}
public void showExternalMap(View view) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr=" + latitude + "," + longitude + "&daddr=" + latitudeDest + "," + longitudeDest)));
}
@Override
protected void loadProximityData() {
// Nothing
}
}