Google maps draw a path

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Google maps draw a path

Postby loteodor » Mon Jun 01, 2009 6:50 pm

Hi! I'm a new Android user, so I'm not so prepared. I'm trying to create a program with android that should show a path, and it should recover that path through the Gps receiver of the phone, but I'm not able to draw a path from a start point to a end point. Which java class I've to use??This is the first question. The second question concerns about draw a path on the map using the google maps library. Namely the path(a set of coordinates that I've saved in some way) shouldn't be a simple rect from a start point to a end point ,but it should follow the street on the map, like what happens when in google maps I ask a path from A to B and on the map is showed the path by car(or by walk).
So in this case which class I've to use for saving and draw the path on the map??
Please answer and if you have some code to explain it might be better!!thanks a lot to everyone!!!
LT

n.B: sorry for my bad english!!:(
loteodor
Freshman
Freshman
 
Posts: 3
Joined: Mon Jun 01, 2009 6:17 pm

Top

Re: Google maps draw a path

Postby sumit_android » Thu Jul 08, 2010 10:10 am

hii..i also want such type of implementation in my application...
i see no one post a reply for this problem...
still no one got any success on this issue.
plz plz plz..post a helpful reply for this.
Really Thanks from the bottom of my heart in advance...... :)
sumit_android
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Feb 19, 2010 6:59 am
Location: India

Re: Google maps draw a path

Postby Kirisawa » Fri Aug 20, 2010 3:35 pm

You'll need to subclass the Overlay class to create an overlay in your map activity. This overlay will contain your path from point1 to point 2.

At minimum you will need something like this:
note: I left out the imports but I guess you can figure that one out yourself :)
MyMapActivity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyMapActivity extends MapActivity {
  2.  
  3.     private MyOverlay myOverlay;
  4.     private MapView mapView;
  5.     private List<Overlay> mapOverlays;
  6.  
  7.     @Override
  8.     public void onCreate(Bundle sis){
  9.         super.onCreate(sis);
  10.         setContentView(R.layout.main); // in my main.xml I defined a MapView
  11.         mapView = (MapView) findViewById(R.id.mapview);
  12.         mapOverlays = mapView.getOverlays();
  13.         myOverlay = new MyOverlay(mapView);
  14.         mapOverlays.add(myOverlay);
  15.     }
  16. }
  17.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

MyOverlay.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyOverlay extends Overlay {
  2.     private GeoPoint geoPoint1;
  3.     private GeoPoint geoPoint2;
  4.     private Point point1;
  5.     private Point point2;
  6.     private Projection projection;
  7.  
  8.     public MyOverlay(MapView mapView){
  9.         projection = mapView.getProjection);
  10.     }
  11.  
  12.     public void draw(Canvas canvas, MapView mapView, boolean shadow){
  13.         super.draw(canvas, mapView, shadow);
  14.         paint = new Paint();
  15.         paint.setDither(true);
  16.         paint.setColor(Color.BLUE);
  17.         paint.setStyle(Paint.Style.FILL_AND_STROKE);
  18.         paint.setStrokeJoin(Paint.Join.ROUND);
  19.         paint.setStrokeCap(Paint.Cap.ROUND);
  20.         paint.setStrokeWidth(2);
  21.        
  22.         point1 = new Point();
  23.         point2 = new Point();
  24.         path = new Path();
  25.        
  26.         /* define your own GeoPoints here
  27.             example: geoPoint1 = new GeoPoint(int latitudeE6, int longitudeE6);
  28.             see documentation: http://code.google.com/android/add-ons/ ... Point.html
  29.         */
  30.        
  31.         projection.toPixels(geoPoint1, point1);
  32.         projection.toPixels(geoPoint2, point2);
  33.        
  34.         path.moveTo(point2.x, point2.y);
  35.         path.moveTo(point1.x, point1.y);
  36.        
  37.         canvas.drawPath(path, paint);
  38.     }
  39. }
  40.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Kirisawa
Freshman
Freshman
 
Posts: 5
Joined: Fri Aug 20, 2010 3:06 pm

Re: Google maps draw a path

Postby hupus » Thu Apr 07, 2011 11:55 pm

Hi,
I have tried to draw a path with this tutorial, but this doesnt work for me.

I got no errors while compiling, but I did not see any path in my mapView..!

What could I do..?
hupus
Freshman
Freshman
 
Posts: 2
Joined: Thu Apr 07, 2011 11:52 pm

Re: Google maps draw a path

Postby sumit_android » Fri Apr 08, 2011 9:03 am

please provide your code snippt, so its easy for me to debug your problem.
sumit_android
Junior Developer
Junior Developer
 
Posts: 19
Joined: Fri Feb 19, 2010 6:59 am
Location: India

Re: Google maps draw a path

Postby hupus » Fri Apr 08, 2011 2:21 pm

sumit_android wrote:please provide your code snippt, so its easy for me to debug your problem.


hi,
It's the same code from Krisawa.

My main-activity (Mapactivity) looks like this (Testical_googleMaps.java) ..:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.test.maps;
  2.  
  3. import java.util.List;
  4.  
  5. import android.os.Bundle;
  6.  
  7. import com.google.android.maps.MapActivity;
  8. import com.google.android.maps.MapView;
  9. import com.google.android.maps.Overlay;
  10.  
  11. public class Testical_googelMaps extends MapActivity {
  12.        
  13.         private MapView mapView;
  14.  
  15.         private MyOverlay myOverlay;
  16.        
  17.         private List<Overlay> mapOverlays;
  18.        
  19.     /** Called when the activity is first created. */
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.main);
  24.        
  25.         mapView = (MapView) findViewById(R.id.mapview);
  26.         mapView.setBuiltInZoomControls(true);
  27.  
  28.        
  29.         mapOverlays = mapView.getOverlays();
  30.         myOverlay = new MyOverlay(mapView);
  31.         mapOverlays.add(myOverlay);
  32.    
  33.     }
  34.  
  35.         @Override
  36.         protected boolean isRouteDisplayed() {
  37.                 // TODO Auto-generated method stub
  38.                 return false;
  39.         }
  40. }
  41.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


and my Overlay-class looks like this (MyOverlay.class)..:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.test.maps;
  2.  
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.Path;
  7. import android.graphics.Point;
  8.  
  9. import com.google.android.maps.GeoPoint;
  10. import com.google.android.maps.MapView;
  11. import com.google.android.maps.Overlay;
  12. import com.google.android.maps.Projection;
  13.  
  14. public class MyOverlay extends Overlay {
  15.  
  16.     private Point point1;
  17.     private Point point2;
  18.     private Projection projection;
  19.  
  20.     public MyOverlay(MapView mapView){
  21.         projection = mapView.getProjection();
  22.     }
  23.  
  24.     public void draw(Canvas canvas, MapView mapView, boolean shadow){
  25.         super.draw(canvas, mapView, shadow);
  26.         Paint paint = new Paint();
  27.         paint.setDither(true);
  28.         paint.setColor(Color.BLUE);
  29.         paint.setStyle(Paint.Style.FILL_AND_STROKE);
  30.         paint.setStrokeJoin(Paint.Join.ROUND);
  31.         paint.setStrokeCap(Paint.Cap.ROUND);
  32.         paint.setStrokeWidth(3);
  33.        
  34.         point1 = new Point();
  35.         point2 = new Point();
  36.         Path path = new Path();
  37.        
  38.         /* define your own GeoPoints here
  39.             example: geoPoint1 = new GeoPoint(int latitudeE6, int longitudeE6);
  40.             see documentation: http://code.google.com/android/add-ons/ ... Point.html
  41.         */
  42.        
  43.         Double longitude = 44.532542*1E6;   // first point
  44.         Double latitude = 8.939209*1E6;
  45.        
  46.         Double longitude_1 = 40.368311*1E6; // second point
  47.         Double latitude_1 = -3.695069*1E6;
  48.        
  49.         GeoPoint geoPoint1 = new GeoPoint(latitude.intValue(),longitude.intValue());
  50.         GeoPoint geoPoint2 = new GeoPoint(latitude_1.intValue(),longitude_1.intValue());
  51.  
  52.        
  53.         projection.toPixels(geoPoint1, point1);
  54.         projection.toPixels(geoPoint2, point2);
  55.        
  56.         path.moveTo(point2.x, point2.y);
  57.         path.moveTo(point1.x, point1.y);
  58.        
  59.         canvas.drawPath(path, paint);
  60.     }
  61. }
  62.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


As I said, its the same Code. When I compile, I didn't get any errors.
I see the map in the activity, but I dont see any path...!
hupus
Freshman
Freshman
 
Posts: 2
Joined: Thu Apr 07, 2011 11:52 pm

Top

Re: Google maps draw a path

Postby oobayly » Sat Apr 16, 2011 12:25 pm

Rather than using moveTo, prehaps try lineTo
Code: Select all
path.lineTo(point2.x, point2.y);
path.lineTo(point1.x, point1.y);


All moveTo is moving to a specific point, no lines are actually being added to the path.

It's also a bad idea to cache the projection between draws, as it is very likely to change when the mapview has been panned or zoomed. See the documentation.
oobayly
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Apr 16, 2011 12:20 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: Exabot [Bot] and 6 guests