plot a route on google maps - android

I want to draw a route on google map with the change in my position using GPS. As my location changes(when new geopoints are created), the dot moves on the google map but i'm unable to draw the line on the map.
Please help in plotting the route on google maps. Below is my code
`
LocationManager locman;
LocationListener loclis;
Location location;
private MapView map;
List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
private MapController controller;
String provider = LocationManager.GPS_PROVIDER;
double lat;
double lon;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMapView();
initMyLocation();
locman = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//locman.requestLocationUpdates(provider,60000, 100,loclis);
//Location = locman.getLastKnownLocation(provider);
}
/** Find and initialize the map view. */
private void initMapView() {
map = (MapView) findViewById(R.id.mapView);
controller = map.getController();
map.setSatellite(false);
map.setBuiltInZoomControls(true);
}
/** Find Current Position on Map. */
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass(); // does not work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location
controller.setZoom(16);
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays().add(overlay);
}
public void onLocationChanged(Location location) {
if (location != null){
lat = location.getLatitude();
lon = location.getLongitude();
GeoPoint New_geopoint = new GeoPoint((int)(lat*1e6),(int)(lon*1e6));
controller.animateTo(New_geopoint);
}
}
class MyOverlay extends Overlay{
public MyOverlay(){
}
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Paint paint;
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3);
Projection projection = map.getProjection();
Path p = new Path();
for (int i = 0; i < geoPointsArray.size(); i++) {
if (i == geoPointsArray.size() - 1) {
break;
}
Point from = new Point();
Point to = new Point();
projection.toPixels(geoPointsArray.get(i), from);
projection.toPixels(geoPointsArray.get(i + 1), to);
p.moveTo(from.x, from.y);
canvas.drawLine(from.x, from.y, to.x, to.y, paint);
//p.lineTo(to.x, to.y);
}
}
}
`

Why don't you just draw a polyline? You just need LatLng instances.
var flightPlanCoordinates = [
new google.maps.LatLng(43.290307,-2.884174),
new google.maps.LatLng(41.3973,2.158964),
new google.maps.LatLng(40.462046,-3.809694),
new google.maps.LatLng(38.976895,-1.858366)
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap( map );

I wrote this code quite some time ago so forgive me, it could be cleaned up a lot, but i believe it should do what you need.
public class RouteSegmentOverlay extends Overlay {
private Paint paint;
private ArrayList<GeoPoint> routePoints;
private boolean routeIsActive;
private int numberRoutePoints;
private Path path;
// Constructor permitting the route array to be passed as an argument.
public RouteSegmentOverlay(ArrayList<GeoPoint> routePoints) {
this.routePoints = routePoints;
numberRoutePoints = routePoints.size();
routeIsActive = true;
}
// Method to turn route display on and off
public void setRouteView(boolean routeIsActive){
this.routeIsActive = routeIsActive;
}
public void setColor(int c){
color = c;
}
private int color = 0;
Paint.Style paintStyle = Paint.Style.STROKE;
public void setFillStyle(Paint.Style style){
paintStyle = style;
}
#Override
public void draw(Canvas canvas, MapView mapview, boolean shadow) {
super.draw(canvas, mapview, shadow);
if(! routeIsActive) return;
if(paint==null){
paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(7);
paint.setStyle(paintStyle);
paint.setAntiAlias(true);
paint.setARGB(255, 0, 0, 255);
paint.setColor(color);
}
if(bitmap==null){
wMin = Integer.MAX_VALUE;
wMax = Integer.MIN_VALUE;
hMin = Integer.MAX_VALUE;
hMax = Integer.MIN_VALUE;
lonMin = Integer.MAX_VALUE;
lonMax = Integer.MIN_VALUE;
latMin = Integer.MAX_VALUE;
latMax = Integer.MIN_VALUE;
Boolean newSegment = true;
Point pt = new Point();
GeoPoint point = null;
ArrayList<Point> points = new ArrayList<Point>();
for(int i=0; i<numberRoutePoints; i++){
point = routePoints.get(i);
int tempLat = point.getLatitudeE6();
int tempLon = point.getLongitudeE6();
if(tempLon<lonMin)lonMin = tempLon;
if(tempLon>lonMax)lonMax = tempLon;
if(tempLat<latMin)latMin = tempLat;
if(tempLat>latMax)latMax = tempLat;
mapview.getProjection().toPixels(routePoints.get(i), pt);
points.add(new Point(pt.x,pt.y));
if(pt.x<wMin)wMin = pt.x;
if(pt.x>wMax)wMax = pt.x;
if(pt.y<hMin)hMin = pt.y;
if(pt.y>hMax)hMax = pt.y;
}
topLeftIn = new GeoPoint(latMax, lonMin);
bottomRightIn = new GeoPoint(latMin, lonMax);
int width = (wMax-wMin);
int height = (hMax-hMin);
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
Canvas c = new Canvas(bitmap);
Path bitmapPath = new Path();
bitmapPath.incReserve(numberRoutePoints);
newSegment = true;
for(Point p : points){
if (newSegment) {
bitmapPath.moveTo(p.x - wMin, p.y - hMin);
newSegment = false;
} else {
bitmapPath.lineTo(p.x - wMin, p.y - hMin);
}
}
c.drawPath(bitmapPath, paint);
}
mapview.getProjection().toPixels(topLeftIn, topLeftOut);
mapview.getProjection().toPixels(bottomRightIn, bottomRightOut);
int l = topLeftOut.x;
int t = topLeftOut.y;
int r = bottomRightOut.x;
int b = bottomRightOut.y;
Rect rect = new Rect(l,t,r,b);
canvas.drawBitmap(bitmap, new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()),rect,null);
}
GeoPoint topLeftIn = null;
GeoPoint bottomRightIn = null;
Point topLeftOut = new Point();
Point bottomRightOut = new Point();
Bitmap bitmap = null;
int wMin = Integer.MAX_VALUE;
int wMax = 0;
int hMin = Integer.MAX_VALUE;
int hMax = 0;
int lonMin = Integer.MAX_VALUE;
int lonMax = 0;
int latMin = Integer.MAX_VALUE;
int latMax = 0;
}

Related

Zoom mapview is lost draw line direction

I doing building a appplication direction from this point to that point on map google. My code run success, but when i zoom in map, draw line direction on map is lost. When zoom out map, my line draw appear. Can you help me!
this source code draw line between two point!
class MyOverLay extends Overlay {
private final List<GeoPoint> points;
private boolean drawStartEnd;
private int pathColor;
public MyOverLay(List<GeoPoint> pointToDraw) {
// TODO Auto-generated constructor stub
this(pointToDraw, Color.BLUE, true);
}
public MyOverLay(List<GeoPoint> points, int pathColor,
boolean drawStartEnd) {
this.points = points;
this.pathColor = pathColor;
this.drawStartEnd = drawStartEnd;
}
private void drawOval(Canvas canvas, Paint paint, Point point) {
Paint ovalPaint = new Paint(paint);
ovalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
ovalPaint.setStrokeWidth(3);
ovalPaint.setColor(Color.BLUE);
int _radius = 7;
RectF oval = new RectF(point.x - _radius, point.y - _radius,
point.x + _radius, point.y + _radius);
canvas.drawOval(oval, ovalPaint);
}
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
Projection projection = mapView.getProjection();
if (shadow == false && points != null) {
Point startPoint = null, endPoint = null;
Path path = new Path();
// We are creating the path
for (int i = 0; i < points.size(); i++) {
GeoPoint gPointA = points.get(i);
Point pointA = new Point();
projection.toPixels(gPointA, pointA);
if (i == 0) { // This is the start point
startPoint = pointA;
path.moveTo(pointA.x, pointA.y);
} else {
if (i == points.size() - 1)// This is the end point
endPoint = pointA;
path.lineTo(pointA.x, pointA.y);
}
}
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(pathColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
paint.setAlpha(100);
if (getDrawStartEnd()) {
if (startPoint != null) {
drawOval(canvas, paint, startPoint);
}
if (endPoint != null) {
drawOval(canvas, paint, endPoint);
}
}
if (!path.isEmpty())
canvas.drawPath(path, paint);
}
return super.draw(canvas, mapView, shadow, when);
}
public boolean getDrawStartEnd() {
return drawStartEnd;
}
public void setDrawStartEnd(boolean markStartEnd) {
drawStartEnd = markStartEnd;
}
}

HowTo OnTouch on a paint overlay (Google maps)?

I paint a overlay on my Google maps view (closed polygon, filled). But now I don't know how to pop up a toast when I tap on the overlay. The most examples which I found work with marker and looks very different to my code.
Main Activity:
public class BOSLstItemDetail extends MapActivity{
ArrayList<HashMap<String, Object>> boslst;
MapView mapView;
MapController mapcontrol;
GeoPoint p;
Polygon polygon;
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boslst_item_detail);
String coordinates[] = {"48.098056", "9.788611"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
MapView mapView = (MapView) findViewById(R.id.mapview);
mapcontrol = mapView.getController();
mapcontrol.animateTo(p);
mapcontrol.setZoom(10);
mapView.setBuiltInZoomControls(true);
ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
try{
InputStream koord = getAssets().open("gps.txt");
if (koord != null) {
InputStreamReader input = new InputStreamReader(koord);
BufferedReader buffreader = new BufferedReader(input);
String line;
while (( line = buffreader.readLine()) != null) {
String[] point_t = line.split(",");
double y = Double.parseDouble(point_t[0]);
double x = Double.parseDouble(point_t[1]);
points.add(new GeoPoint((int)(x*1e6), (int)(y*1e6)));
}
koord.close();
polygon = new Polygon(points);
}
}catch (Exception e) {
Log.e("APP","Failed", e);
}
mapView.getOverlays().clear();
mapView.getOverlays().add(polygon);
mapView.invalidate();
}
}
Polygon.java
public class Polygon extends Overlay {
ArrayList<GeoPoint> geoPoints;
public Polygon(ArrayList<GeoPoint> points){
geoPoints = points;
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow){
//Set the color and style
Paint paint = new Paint();
paint.setColor(Color.parseColor("#88ff0000"));
paint.setAlpha(50);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
//Create path and add points
Path path = new Path();
Point firstPoint = new Point();
mapView.getProjection().toPixels(geoPoints.get(0), firstPoint);
path.moveTo(firstPoint.x, firstPoint.y);
for(int i = 1; i < geoPoints.size(); ++i){
Point nextPoint = new Point();
mapView.getProjection().toPixels(geoPoints.get(i), nextPoint);
path.lineTo(nextPoint.x, nextPoint.y);
}
//Close polygon
path.lineTo(firstPoint.x, firstPoint.y);
path.setLastPoint(firstPoint.x, firstPoint.y);
canvas.drawPath(path, paint);
super.draw(canvas, mapView, shadow);
}
}
gps.txt:
9.34669876098644,48.2405319213867
9.36384963989269,48.2296714782715
9.3639497756958,48.2259712219238
9.87827968597418,48.2786293029785
9.87261867523205,48.2822494506837
9.87254810333263,48.2859611511232
9.88368034362787,48.2898597717285
9.8835382461549,48.2972793579102
9.72781181335461,47.9827613830566
9.72225093841558,47.9826812744141
9.72232818603527,47.9789619445801
9.71129894256597,47.9750900268555
9.70574092864985,47.9750099182129
9.70557022094732,47.9824409484864
9.69992923736572,47.9860801696778
9.69436073303234,47.9860000610352
9.33546066284174,48.2403602600099
9.34669876098644,48.2405319213867
If you're extending Overlay, you can override the OnTap method:
protected boolean onTap(final int index)
{
OverlayItem item = mOverlays.get(index);
if(item != null){
Toast.makeText(mContext, textToShow, Toast.LENGTH_SHORT).show();
}
//return true to indicate we've taken care of it
return true;
}

Drawing shapes in Android MapView independent of the zoom level

I need to draw shapes just like those you would create with custom maps on Google Maps, using Android's MapView.
In other words, if I draw a shape on the map, when I zoom out it should shrink, covering the same area of the map regardless of the zoom level.
Example following the Android Reference:
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
Paint boxPaint = new Paint();
boxPaint.setColor(android.graphics.Color.WHITE);
boxPaint.setStyle(Paint.Style.FILL);
boxPaint.setAlpha(140);
canvas.drawCircle(screenPts.x, screenPts.y, 20, boxPaint);
return true;
}
This shows a white circle on the map, but if you zoom out, the circle is the same size. Perhaps using canvas is not right approach?
I need something like how Google Maps highlights neighborhoods or cities:
Any ideas? Thanks in advance!
I had the same problem myself, here's a full solution with demo locations:
import java.util.*;
import android.graphics.*;
import android.graphics.Paint.Style;
import android.graphics.Region.Op;
import android.os.Bundle;
import com.google.android.maps.*;
public class ShapeOverlayTest extends MapActivity {
private MapView m_map;
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_map = (MapView) findViewById(R.id.mapview);
m_map.displayZoomControls(true);
m_map.setBuiltInZoomControls(true);
}
#Override
protected void onStart() {
super.onStart();
Loc[][] areas = {
{
new Loc(51.51695436113811, -0.28686325139653757),
new Loc(51.5268179962453, -0.28118722558738923),
new Loc(51.526498459594215, -0.27779666308279755),
new Loc(51.52521530775356, -0.26943974607777654),
new Loc(51.52292555645698, -0.25813738590178786),
new Loc(51.52054465991048, -0.2498381618983569),
new Loc(51.51012230470141, -0.24509233633017083),
new Loc(51.50884762913046, -0.24465130560570497),
new Loc(51.50732063336974, -0.2441767643132881),
new Loc(51.50431624597833, -0.24473900326760137),
new Loc(51.49756328092904, -0.2714528238165076),
new Loc(51.50092541797557, -0.28360267232347336),
new Loc(51.50205958485736, -0.28490018935582045),
new Loc(51.50488447379555, -0.28681164237730944)
},
{
new Loc(51.50368617913765, -0.25313579464343156),
new Loc(51.51978611305675, -0.24842567405905958),
new Loc(51.51039382684418, -0.24460628015366626),
new Loc(51.508792552597576, -0.24397604687682156),
new Loc(51.50713008309719, -0.24346350415674722),
new Loc(51.502411013302684, -0.2508501075008919),
new Loc(51.502377240039664, -0.25160073203846817),
new Loc(51.50274364303565, -0.25204783703705536)
},
{
new Loc(51.49924084955314, -0.2858705706471945),
new Loc(51.50212820259818, -0.2791479893522646),
new Loc(51.49724510427319, -0.27427453152961206),
new Loc(51.49429724502515, -0.2799184038304611),
new Loc(51.494270969987404, -0.28180678948730314)
}
};
String[] areaNames = { "W3 Ealing", "W3 Hammersmith & Fulham", "W3 Hounslow" };
// for (Map.Entry<String, List<Loc>> area : m_areas.entrySet()) {
// // to have much less points and make sure they are in order
// // the demo data already has these properties
// // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm#Pseudocode
// area.setValue(Algo.convexHull(area.getValue()));
// }
Map<String, List<GeoPoint>> areaMap = new HashMap<String, List<GeoPoint>>();
for (int i = 0; i < areaNames.length; i++) {
List<GeoPoint> points = new ArrayList<GeoPoint>();
for (int j = 0; j < areas[i].length; j++) {
points.add(areas[i][j].toGeoPoint());
}
areaMap.put(areaNames[i], points);
}
m_map.getOverlays().add(new AreasOverlay(areaMap));
// TODO determine location better, e.g. averaging area points
GeoPoint center = new GeoPoint(51509704, -270710);
m_map.getController().setCenter(center);
m_map.getController().setZoom(15);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
static class Loc {
private double m_lat;
private double m_lon;
public Loc(final double lat, final double lon) {
m_lat = lat;
m_lon = lon;
}
public GeoPoint toGeoPoint() {
return new GeoPoint((int) (m_lat * 1e6), (int) (m_lon * 1e6));
}
};
static class AreasOverlay extends Overlay {
private final Map<String, List<GeoPoint>> m_areas;
private final Paint m_paintFill;
private final Paint m_paintStroke;
private static final int ALPHA = 0x30ffffff; // 48 out of 255 transparent
private static final int[] COLORS =
{ Color.YELLOW, Color.MAGENTA, Color.CYAN, Color.RED, Color.GREEN, Color.BLUE };
static {
for (int i = 0; i < AreasOverlay.COLORS.length; i++) {
AreasOverlay.COLORS[i] &= AreasOverlay.ALPHA;
}
}
public AreasOverlay(final Map<String, List<GeoPoint>> areaMap) {
m_areas = areaMap;
// prepare paints
m_paintFill = new Paint();
m_paintFill.setStyle(Paint.Style.FILL);
m_paintStroke = new Paint(Paint.ANTI_ALIAS_FLAG);
m_paintStroke.setStyle(Style.STROKE);
m_paintStroke.setAntiAlias(true);
m_paintStroke.setStrokeWidth(3);
}
#Override
public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
super.draw(canvas, mapView, shadow);
if (shadow) {
return;
}
Projection projection = mapView.getProjection();
List<Path> areaPaths = getPaths(projection, m_areas);
drawPaths(canvas, areaPaths);
}
private List<Path> getPaths(final Projection projection, final Map<String, List<GeoPoint>> areas) {
List<Path> areaPaths = new ArrayList<Path>(areas.size());
for (Map.Entry<String, List<GeoPoint>> entry : areas.entrySet()) {
List<GeoPoint> sourceList = entry.getValue();
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
Iterator<GeoPoint> it = sourceList.iterator();
Point point = nextDrawPoint(projection, it);
path.moveTo(point.x, point.y);
while (it.hasNext()) {
point = nextDrawPoint(projection, it);
path.lineTo(point.x, point.y);
}
path.close();
areaPaths.add(path);
}
return areaPaths;
}
/**
* <ul>
* <li>Draw with different colors.
* <li>Draw strokes first for them to be always visible.
* <li>Draw fills next with each removing from the drawable area.
* </ul>
*/
private void drawPaths(final Canvas canvas, final List<Path> areaPaths) {
int currentColorIndex;
currentColorIndex = 0;
for (Path path : areaPaths) {
int currentColor = AreasOverlay.COLORS[currentColorIndex++];
currentColorIndex %= AreasOverlay.COLORS.length;
m_paintStroke.setColor(currentColor & 0xff7f7f7f); // make it darker by clearing the high bit
canvas.drawPath(path, m_paintStroke);
}
currentColorIndex = 0;
for (Path path : areaPaths) {
int currentColor = AreasOverlay.COLORS[currentColorIndex++];
currentColorIndex %= AreasOverlay.COLORS.length;
m_paintFill.setColor(currentColor);
canvas.drawPath(path, m_paintFill);
canvas.clipPath(path, Op.DIFFERENCE); // don't allow to draw over each other
}
}
private Point nextDrawPoint(final Projection projection, final Iterator<GeoPoint> it) {
GeoPoint geo = it.next();
Point p = new Point();
projection.toPixels(geo, p);
return p;
}
}
}
The radius for drawCircle is in pixels so it make sense that the circle is always the same size. You have to scale the radius based on the zoom level. The example below will graph a geometry from the JTS Topology Suite that will scale.
public class MapOverlay extends Overlay {
private static final String TAG = MapOverlay.class.getName();
// Allocate once and reuse
private final Paint mPaint = new Paint();
private final Path mPath = new Path();
// Region to highlight
private Geometry mGeometry;
/**
* #param geometry Region to highlight on map
*/
public MapOverlay(Geometry geometry) {
// Set region
mGeometry = geometry;
// Edit paint style
mPaint.setDither(true);
mPaint.setColor(Color.rgb(128, 136, 231));
mPaint.setAlpha(100);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(6);
}
/**
* Draw the overlay over the map.
*
* #see com.google.android.maps.Overlay#draw(Canvas, MapView, boolean)
*/
#Override
public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, shadow);
if (mGeometry != null) {
// TODO There could be more than one geometries
Geometry g = mGeometry.getGeometryN(0);
final Point p = new Point();
boolean first = true;
mPath.reset();
for (Coordinate c : g.getCoordinates()) {
// Convert lat/lon to pixels on screen
// GeoPoint is immutable so allocation is unavoidable
Projection projection = mapv.getProjection();
projection.toPixels(new GeoPoint((int) (c.y * 1E6), (int) (c.x * 1E6)), p);
// Set path starting point to first coordinate
// otherwise default start is (0,0)
if (first) {
mPath.moveTo(p.x, p.y);
first = false;
}
// Add new point to path
mPath.lineTo(p.x, p.y);
}
}
// Draw the path with give paint
canvas.drawPath(mPath, mPaint);
}
Adapted from here: MapSelectionOverlay.java
This maybe what you are looking for: Can "overlay" size be zoomed together with the google map on android?
public class ImpactOverlay extends Overlay {
private static int CIRCLERADIUS = 0;
private GeoPoint geopoint;
public ImpactOverlay(GeoPoint point, int myRadius) {
geopoint = point;
CIRCLERADIUS = myRadius;
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// Transfrom geoposition to Point on canvas
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(geopoint, point);
// the circle to mark the spot
Paint circle = new Paint();
circle.setColor(Color.BLACK);
int myCircleRadius = metersToRadius(CIRCLERADIUS, mapView, (double)geopoint.getLatitudeE6()/1000000);
canvas.drawCircle(point.x, point.y, myCircleRadius, circle);
}
public static int metersToRadius(float meters, MapView map, double latitude) {
return (int) (map.getProjection().metersToEquatorPixels(meters) * (1/ Math.cos(Math.toRadians(latitude))));
}
}
What you need is a list of lat/lon points for each shape you want to draw on the map. In the onDraw method, you need to iterate over that list (for each shape you want to draw), and do this:
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
then draw the shape on the canvas. IIRC that works correctly regardless of zoom, because the mapView is aware of the zoom level, and gives you the appropriate pixel location for the lat/long pair at that zoom level.

How do you draw a line from previous point to current point in Android Google Maps?

I am trying to create a GPS tracking app, and is stuck on how I can draw a line from the previous point to current point. I've tried using an Overlay, but it does not display... I am not THAT good on Java, so please speak to me like I'm 4 years old...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMapView();
initMyLocation();
TabHost.TabSpec spec;
TabHost th = (TabHost)findViewById(R.id.tabhost);
th.setup();
spec = th.newTabSpec("tag1");
spec.setContent(R.id.mapTab);
spec.setIndicator("Map");
th.addTab(spec);
spec = th.newTabSpec("tag2");
spec.setContent(R.id.logTab);
spec.setIndicator("Log");
th.addTab(spec);
spec = th.newTabSpec("tag3");
spec.setContent(R.id.detailsTab);
spec.setIndicator("Details");
th.addTab(spec);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
//Map and Controls
private void initMapView() {
map = (MapView) findViewById(R.id.mvMain);
controller = map.getController();
map.setSatellite(true);
//map.setStreetView(true);
map.setBuiltInZoomControls(true);
}
//Creates an Overlay that marks current position
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass();
overlay.runOnFirstFix(new Runnable() {
public void run() {
controller.setZoom(17);
controller.animateTo(overlay.getMyLocation());
map.getOverlays().add(overlay);
}
});
}
//Experiment
public class detailsTab extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.detailsTab);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
}
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.detailsText);
if(location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong" + lng;
}
else {
latLongString = "No Location Found";
}
myLocationText.setText("Your current position is: \n" + latLongString);
}
}
public class NewOverlay extends Overlay {
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Double lat = lati *1E6;
Double lng = longi *1E6;
GeoPoint geoPoint = new GeoPoint(lat.intValue(), lng.intValue());
if (shadow == false) {
Point myPoint = new Point();
projection.toPixels(geoPoint, myPoint);
//Creating and setting up the paint brush
Paint paint = new Paint();
paint.setARGB(250, 255, 0, 0);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
//Create circle
int rad = 25;
RectF oval = new RectF(myPoint.x-rad, myPoint.y-rad, myPoint.x+rad, myPoint.y+rad);
canvas.drawOval(oval, paint);
canvas.drawText("Red Circle", myPoint.x+rad, myPoint.y, paint);
}
}
}
}
I do that this way, inside draw() method,
int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
Paint paint = new Paint();
paint.setColor(Color.rgb(0x7b, 0x7b, 0xff));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
if(mPoints!=null || mPoints.size()<2)
{
for (int i = 0; i < mPoints.size(); i++) {
Point point = new Point();
mv.getProjection().toPixels(mPoints.get(i), point);
Point loc = new Point();
mv.getProjection().toPixels(new GeoPoint((int) (location.getLatitude()*1.0E6),(int) (location.getLongitude()*1.0E6)), loc);
x2 = point.x;
y2 = point.y;
if (i == 0)
{
x2 = loc.x;
y2 = loc.y;
}
if (i > 0) {
canvas.drawLine(x1, y1, x2, y2, paint);
}
x1 = x2;
y1 = y2;
}
}
This code will draw for me a whole route, mPoints is an array of GeoPoint that I want to draw them. This should be useful for you.
public void draw(Canvas canvas, MapView mapv, boolean shadow){
super.draw(canvas, mapv, shadow);
Projection projection = mapv.getProjection();
Path p = new Path();
for (int i = 0; i < geoPointsArray.size(); i++) {
if (i == geoPointsArray.size() - 1) {
break;
}
Point from = new Point();
Point to = new Point();
projection.toPixels(geoPointsArray.get(i), from);
projection.toPixels(geoPointsArray.get(i + 1), to);
p.moveTo(from.x, from.y);
p.lineTo(to.x, to.y);
}
Paint mPaint = new Paint();
mPaint.setStyle(Style.STROKE);
mPaint.setColor(Color.GREEN);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(5);
canvas.drawPath(p, mPaint);
mapv.invalidate();
super.draw(canvas, mapv, shadow);
}//draw()
we can use the above method to draw route as we move place to place. where geoPointsArray is array of locations received. And in your onCreate() you need to call mapv.invalidate(); to draw route continously

Draw Route on Android Map not working...?

I've been trying to work this piece of code for a week now. The route does not come up. My code is below.
I am trying to draw a route between two geopoints - the location which I'm retrieving from a web service.
My log doesn't show any error.
public class TesterGTC extends MapActivity {
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:2488/Service1.asmx";
private static final String METHOD_NAME1 = "lastKnownLocationAllValues";
private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME1;
private List<Overlay> mapOverlays;
private Projection projection;
MapView mapView;
double latitude;
double longitude;
double endlat;
double endlong;
GeoPoint geoPoint;
MapController myMC;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // Suppress title bar to give more space
setContentView(R.layout.googletrackingclient);
final String orderID = GoogleTrackingMenu.epcID;
final String vehicleid = GoogleTrackingMenu.vehicleid;
Thread t = new Thread(new Runnable() {
public void run() {
String u = orderID;
String v = vehicleid;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
PropertyInfo propInfo = new PropertyInfo();
propInfo.name = "OID";
propInfo.type = PropertyInfo.STRING_CLASS;
request.addProperty(propInfo, u);
PropertyInfo propInfo2 = new PropertyInfo();
propInfo2.name = "vehicleID";
propInfo2.type = PropertyInfo.STRING_CLASS;
request.addProperty(propInfo2, v);
final TextView textview = (TextView) findViewById(R.id.id1);
final TextView myLoc = (TextView) findViewById(R.id.id2);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
final ArrayList<EPCISGPSResult> resultList = new ArrayList<EPCISGPSResult>();
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
final SoapObject resultRequestSOAP = (SoapObject) envelope
.getResponse();
final int resultInt = resultRequestSOAP.getPropertyCount();
for (int i = 0; i < resultInt; i++) {
SoapObject resultRequest = (SoapObject) resultRequestSOAP
.getProperty(i);
String vehicleID = resultRequest.getProperty("vehicleID").toString();
String driverName = resultRequest.getProperty("driverName").toString();
String latitude = resultRequest.getProperty("latitude").toString();
String longitude = resultRequest.getProperty("longitude").toString();
String startVenue = resultRequest.getProperty("startVenue").toString();
String destination = resultRequest.getProperty("destination").toString();
String dateReceived = resultRequest.getProperty("dateReceived").toString();
String utc = resultRequest.getProperty("utc").toString();
String orderID = resultRequest.getProperty("orderID").toString();
EPCISGPSResult e = new EPCISGPSResult(vehicleID,driverName, latitude, longitude, startVenue,destination, dateReceived, utc, orderID);
resultList.add(e);
}
latitude = Double.parseDouble(resultList.get(0).getLatitude());
longitude = Double.parseDouble(resultList.get(0).getLongitude());
endlat = Double.parseDouble(resultList.get(resultList.size()-1).getLatitude());
endlong = Double.parseDouble(resultList.get(resultList.size()-1).getLongitude());
int beglat = (int)latitude* 1000000;
int endinglat = (int)endlat* 1000000;
int beglong = (int)longitude* 1000000;
int endinglong = (int)endlong* 1000000;
final GeoPoint gP1 = new GeoPoint(beglat, beglong);
final GeoPoint gP2 = new GeoPoint(endinglat, endinglong);
TesterGTC.this.runOnUiThread(new Runnable() {
public void run() {
int pointer = 0;
pointer = 1;
TextView tview = (TextView) findViewById(R.id.id1);
tview.setText("before map");
mapView = (MapView) findViewById(R.id.myGMap);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
myMC = mapView.getController();
myMC.setZoom(15);
int color = Color.RED;
mapOverlays = mapView.getOverlays();
projection = mapView.getProjection();
TextView txxview = (TextView) findViewById(R.id.id2);
txxview.setText("after map");
MyOverlay newO = new MyOverlay(gP1, gP2, color);
/* TextView textview = (TextView) findViewById(R.id.id1);
textview.setText("This is happening");*/
mapOverlays.add(newO);
} });
}
catch (final Exception e) {
TesterGTC.this.runOnUiThread(new Runnable() {
public void run() {
TextView textview = (TextView) findViewById(R.id.id1);
textview.setText("Your error is: " + e.getMessage().toString());
}
});
} finally {
}
}
});
t.start();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
class MyOverlay extends Overlay{
GeoPoint gp1;
GeoPoint gp2;
int color;
public MyOverlay(GeoPoint gp1, GeoPoint gp2, int color){
this.gp1 = gp1;
this.gp2 = gp2;
this.color = color;
}
public void draw(Canvas canvas, MapView mapView, boolean shadow, GeoPoint gP1 , GeoPoint gP2){
super.draw(canvas, mapView, shadow);
TextView textview = (TextView) findViewById(R.id.id1);
textview.setText("This is ALSO happening");
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
projection.toPixels(gP1, p1);
projection.toPixels(gP2, p2);
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x,p1.y);
canvas.drawPath(path, mPaint);
}
}
}
Here saddr = source & daddr = destination location.
public void showDirections(View view) {
final Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?" + "saddr="+ latitude + "," + longitude + "&daddr=" + latitude + "," + longitude));
intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(intent);
}
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext = getApplicationContext();
Bitmap bmp = BitmapFactory.decodeFile("pushpin.png");
Drawable drawable = new BitmapDrawable(bmp);
public HelloItemizedOverlay(Drawable drawable) {
super(boundCenterBottom(drawable));
populate();
}
public HelloItemizedOverlay(Drawable drawable, Context context) {
super(boundCenterBottom(drawable));
mContext = getApplicationContext();
populate();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(
GoogleTrackingPage.this);
// add the overlay item's title and snippet or create owner
String newMessage = "Your order has the following location details \n\nVehicleID: "
+ vehicle
+ "\nDriver: "
+ driver
+ "\nStart Venue: "
+ starting
+ "\nFinal Destination: "
+ desti
+ "\nLast Updated on:" + dateR;
dialog.setTitle("Order Details for " + orderidfrommenu + " at "
+ locationString);
dialog.setMessage(newMessage);
dialog.show();
return true;
}
#Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,long when)
{
Paint paint = null;
int x = myList.size();
int lastPoint = x-1;
if(myList.size() <= 2){
latitude = Double.parseDouble(myList.get(0).getLatitude());
longitude = Double.parseDouble(myList.get(0).getLongitude());
}
else{
int lP = myList.size()-2;
latitude = Double.parseDouble(myList.get(lP).getLatitude());
longitude = Double.parseDouble(myList.get(lP).getLongitude());
}
endlat = Double.parseDouble(myList.get(myList.size() - 1).getLatitude());
endlong = Double.parseDouble(myList.get(myList.size() - 1).getLongitude());
int beglat = (int)latitude* 1000000;
int endinglat = (int)endlat* 1000000;
int beglong = (int)longitude* 1000000;
int endinglong = (int)endlong* 1000000;
GeoPoint gP1 = new GeoPoint(beglat, beglong);
GeoPoint gP2 = geoPoint;
gP1=mOverlays.get(0).getPoint();
Projection projection = mapView.getProjection();
if (shadow == false)
{
paint = new Paint();
paint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gP1, point);
paint.setColor(Color.RED);
Point point2 = new Point();
projection.toPixels(gP2, point2);
paint.setStrokeWidth(5);
canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
(float) point2.y, paint);
}
return super.draw(canvas, mapView, shadow, when);
}
}

Categories

Resources