How do I draw a line Overlay on Google Maps Android
-
I am trying to draw a line on my maps project, but can't get the line to draw. Where and how do I declare the overlay? I've tried various methods, but can't get it to work. ie, code just displays errors in Eclipse. What I am NOT trying to do is draw a route from A to B, but rather draw the route as I am moving. // Creating a MapView public class Gpstrack extends MapActivity { private MapView map; private MapController controller; private Projection projection; ArrayList<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>(); /** Called when the activity is first created. */ @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.map_Tab); spec.setIndicator("Map"); th.addTab(spec); spec = th.newTabSpec("tag2"); spec.setContent(R.id.log_Tab); spec.setIndicator("Log"); th.addTab(spec); spec = th.newTabSpec("tag3"); spec.setContent(R.id.details_Tab); spec.setIndicator("Details"); th.addTab(spec); spec = th.newTabSpec("tag4"); spec.setContent(R.id.student_Tab); spec.setIndicator("Student Info"); 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 class MyOverlay extends Overlay { 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); } } }
-
Answer:
Please follow this tutorial http://codemagician.wordpress.com/2010/05/06/android-google-mapview-tutorial-done-right/
sKwok12 at Stack Overflow Visit the source
Other answers
Have you try with this demo to implement the http://eagle.phys.utk.edu/guidry/android/mapOverlayDemo.html. Hope you got the idea with this demo. Enjoy. :)
iDroid Explorer
In you overlay class you can draw line like this public class MapOverlay extends com.google.android.maps.Overlay { Canvas canvas; @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { // TODO Auto-generated method stub super.draw(canvas, mapView, shadow); this.canvas=canvas; Point screenpoint = new Point(); mapView.getProjection().toPixels(p, screenpoint); Bitmap bmp = BitmapFactory.decodeResource( getResources(), R.drawable.pushpin); canvas.drawBitmap(bmp, canvas.getWidth()/4, canvas.getHeight()/4, null); Paint paint = new Paint(); paint.setColor(Color.BLACK); canvas.drawLine(canvas.getWidth()/4, canvas.getHeight()/4, canvas.getWidth()/2, canvas.getHeight()/2, paint); return true; } return true; } } the draw line function is like public void drawLine (float startX, float startY, float stopX, float stopY, Paint paint) Since: API Level 1 Draw a line segment with the specified start and stop x,y coordinates, using the specified paint. Parameters startX The x-coordinate of the start point of the line startY The y-coordinate of the start point of the line paint The paint used to draw the line and you can reffer this quetion if you want to draw a path http://stackoverflow.com/questions/2023669/j2me-android-blackberry-driving-directions-route-between-two-locations/2023685#2023685
Avi Kumar Manku
I think you should try this link http://stackoverflow.com/questions/2176397/drawing-a-line-path-on-google-maps and this one http://stackoverflow.com/questions/2023669/j2me-android-blackberry-driving-directions-route-between-two-locations .I think this should help you.
Nitin
Related Q & A:
- How to draw a line in LibGDX?Best solution by Stack Overflow
- How can I get a line cook job at Olive Garden?Best solution by Yahoo! Answers
- How do I get a job with a cruise line?Best solution by Yahoo! Answers
- How can I promote a new product for Google?Best solution by Quora
- How can i start a handbag line?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.