Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.mapexample.mapexample;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.google.android.maps.GeoPoint;
public class Directions {
public static ArrayList<GeoPoint> getDirections(double lat1, double lon1,
double lat2, double lon2) {
double lat,lng;
String url = String.format("%s=%s,%s&destination=%s,%s%s",
"http://maps.googleapis.com/maps/api/directions/xml?origin",
lat1, lon1, lat2, lon2, "&sensor=false");
String tag[] = { "lat", "lng" };
ArrayList<GeoPoint> list_of_geopoints = new ArrayList<GeoPoint>();
HttpResponse response = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
response = httpClient.execute(httpPost, localContext);
InputStream in = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(in);
if (doc != null) {
NodeList nl1, nl2;
nl1 = doc.getElementsByTagName(tag[0]);
nl2 = doc.getElementsByTagName(tag[1]);
list_of_geopoints.add(new GeoPoint((int) (lat1 * 1E6),
(int) (lon1 * 1E6)));
if (nl1.getLength() > 0) {
list_of_geopoints = new ArrayList<GeoPoint>();
for (int i = 0; i < nl1.getLength(); i++) {
Node node1 = nl1.item(i);
Node node2 = nl2.item(i);
lat = Double.parseDouble(node1.getTextContent());
lng = Double.parseDouble(node2.getTextContent());
list_of_geopoints.add(new GeoPoint((int) (lat * 1E6),
(int) (lng * 1E6)));
}
} else {
// No points found
}
}
} catch (Exception e) {
e.printStackTrace();
}
return list_of_geopoints;
}
}
void setupMapView() {
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
void drawRoute(double slat,double slon,double dlat,double dlon) {
ArrayList<?> all_geo_points = Directions.getDirections(
slat, slon, dlat,dlon );
mapOverlays.add(new MyOverlay(all_geo_points));
controller.setZoom(12);
mapView.invalidate();
}
void drawLocation(GeoPoint current_location) {
Drawable drawable = this.getResources().getDrawable(R.id.map_icon);
OverlayItem overlayItem = new OverlayItem(current_location, "", "");
itemizedOverlay = new MyItemizedOverlay(drawable, this);
itemizedOverlay.addOverlay(overlayItem);
mapView.getOverlays().add(itemizedOverlay);
mapView.invalidate();
}
package com.mapexample.mapexample;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyItemizedOverlay(Drawable defaultMarker, Context ctx) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
return false;
}
}
public class MyOverlay1 extends Overlay {
private ArrayList all_geo_points;
public MyOverlay1(ArrayList allGeoPoints) {
super();
this.all_geo_points = allGeoPoints;
}
@Override
public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {
super.draw(canvas, mv, shadow);
drawPath(mv, canvas);
return true;
}
public void drawPath(MapView mv, Canvas canvas) {
int xPrev = -1, yPrev = -1, xNow = -1, yNow = -1;
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(4);
paint.setAlpha(100);
if (all_geo_points != null)
for (int i = 0; i < all_geo_points.size() - 4; i++) {
GeoPoint gp = (GeoPoint) all_geo_points.get(i);
Point point = new Point();
mv.getProjection().toPixels(gp, point);
xNow = point.x;
yNow = point.y;
if (xPrev != -1) {
canvas.drawLine(xPrev, yPrev, xNow, yNow, paint);
}
xPrev = xNow;
yPrev = yNow;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.