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;
}
}
Related
I have problem with app lagging when I have big zoom.
I have maps with markers and area, when I make zoom biger the map is lagging.
I was looking for solution, but I don't find.
Please help me with this code.
I don't have idea how I can fix it.
This is my code:
public class LiniaOverlay extends Overlay {
private List<GeoPoint> m_areas;
private Paint m_paintFill;
private Paint m_paintStroke;
private static final int ALPHA = 0x30ffffff;
private static int COLORS = Color.YELLOW;
Path areaPaths;
static {LiniaOverlay.COLORS &= LiniaOverlay.ALPHA;}
public LiniaOverlay(final List<GeoPoint> points) {
m_areas = points;
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();
areaPaths = getPath(projection, m_areas);
drawPaths(canvas, areaPaths);
}
private Path getPath(final Projection projection, final List<GeoPoint> areas) {
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
Iterator<GeoPoint> it = areas.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();
return path;
}
private void drawPaths(final Canvas canvas, final Path path) {
int currentColor = LiniaOverlay.COLORS;
m_paintStroke.setColor(currentColor & 0xff7f7f7f);
canvas.drawPath(path, m_paintStroke);
int currentColor2 = LiniaOverlay.COLORS;
m_paintFill.setColor(currentColor2);
canvas.drawPath(path, m_paintFill);
}
private Point nextDrawPoint(final Projection projection, final Iterator<GeoPoint> it) {
GeoPoint geo = it.next();
Point p = new Point();
projection.toPixels(geo, p);
return p;
}
}
Please Help me.
I am implementing an android application for Map activity.
I am using Location listener for getting location updates. After updating location i am saving those updated latitude and longitude values in database.
I am able draw Route path From Source to destination using source latitude, longitude and destination latitude, longitude values. But using multiple latitude and longitude values of database at a time i want to draw the route path. I am using my RoutePath.java class to draw the route path for multiple latitudes and longitudes.Using those list of latitudes and longitudes i am able to draw the path, but it shows point to point stright line not shows the route path. See the below image...
If you observe carefully some points are on the route and some points are outside of route path. See again below image with full zooming...
Please help me if anyone knows the solution for this problem...
RoutePath.java:
public class RoutePath extends Overlay {
private int _pathColor;
private final List<GeoPoint> _points;
private boolean _drawStartEnd;
public RoutePath(List<GeoPoint> points) {
this(points, Color.RED, true);
}
public RoutePath(List<GeoPoint> points, int pathColor,
boolean drawStartEnd) {
_points = points;
_pathColor = pathColor;
_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(2);
int _radius = 6;
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(5);
paint.setAlpha(90);
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;
}
}
You got Straight line because the the GeoPoints are like that only..... means if you have a complete route paths then it will show according to your Geopoints....It all depends on GeoPoints.!
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;
}
Now below is my code that draws path between geopoints in map. This works perfectly fine. What I'm trying to implement is instead of drawing a line,display this path with the dots(.) as in iPhone. I want it to be like this gp1.........gp2 instead of drawing in a single straight line like gp1______gp2.
I have tried almost all the options for doing this but still no success on this, any one can help me solving this?
private void drawPath(List geoPoints, int color) {
List overlays = objMapView.getOverlays();
int loopcount = geoPoints.size() - 1;
for (int i = 0; i < loopcount; i++) {
GeoPoint p1 = (GeoPoint) geoPoints.get(i);
GeoPoint p2 = (GeoPoint) geoPoints.get(i + 1);
MyPathOverLay p = null;
/**** Marking the start and end of the trailpath ****/
if (i == 0) {
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.greenflag);
p = new MyPathOverLay(p1, p2, 0xFFFF0000, true, false, bmp);
} else if (i == loopcount - 1) {
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.redflag);
p = new MyPathOverLay(p1, p2, 0xFFFF0000, false, true, bmp);
} else {
p = new MyPathOverLay(p1, p2, 0xFFFF0000, false, false, null);
}
overlays.add(p);
}
}
public class MyPathOverLay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
private int color;
Boolean isFirstPOI;
Boolean isLastPOI;
AudioMap audioMap;
Bitmap bmp;
public MyPathOverLay(GeoPoint gp1, GeoPoint gp2, int color,Boolean first, Boolean last,Bitmap bitMap) {
this.gp1 = gp1;
this.gp2 = gp2;
this.color = color;
this.isFirstPOI= first;
this.isLastPOI = last;
this.bmp = bitMap;
}
#Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Paint paint = new Paint();
Point point = new Point();
projection.toPixels(gp1, point);
paint.setColor(color);
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);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(gp1, screenPts);
//---translate the GeoPoint to screen pixels---
Point screenPts1 = new Point();
mapView.getProjection().toPixels(gp2, screenPts1);
if(isFirstPOI == true){
canvas.drawBitmap(bmp,screenPts.x-20,screenPts.y-40, null);
}
else if(isLastPOI == true) {
canvas.drawBitmap(bmp,screenPts1.x-20,screenPts1.y-35, null);
}
super.draw(canvas, mapView, shadow);
}
}
Thanks to this guy, I referred this example and it is working for me.
How do I make a dotted/dashed line in Android?
Just need to add these 2 lines,
paint.setPathEffect(new DashPathEffect(new float[] {10,10}, 5));
canvas.drawLine(point.x, point.y, point2.x, point2.y, paint);
after I set,
paint.setAlpha(120);
#Frankenstein, thank you
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