Drawing a path with an array of GeoPoints, Points, and Paths

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

Drawing a path with an array of GeoPoints, Points, and Paths

Postby hadoken13 » Wed Mar 31, 2010 7:36 pm

Hi,
I am drawing a route on the map surface, but as I will intend to use a large number or coordinates ~50, I cannot seem to get it to display properly. It works when I hard code it in, but that would take ages for all the coordinates.

Here is some sample code in the draw() of the MapActivity class with the arrays. It draws the path with just 3 sets of coordinates but if I go over that it starts to draw something like a star where all the paths come from one point. It seems like it is drawing the beginning of every path from GeoPoint2.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ...........................................................................
  2.  
  3.                   int[] xCoordArray = getXArray();
  4.                   int[] yCoordArray = getYArray();
  5.                 //int[] xCoordArray = {53343391,53344223,53344198,53344697,53345069,53345011,53344976,53345063,53345120,53345229};
  6.                 //int[] yCoordArray = {-6271820,-6271927,-6273483,-6273483,-6273494,-6272684,-6272120,-6270897,-6270055,-6269143};
  7.                        
  8.                         int counter = xCoordArray.length;
  9.                         GeoPoint[] gpArray = new GeoPoint[counter];
  10.                         Point[] pPoints = new Point[counter];
  11.                         Path[] thePaths = new Path[counter];
  12.                        
  13.                         for (int i = 0; i < counter; i++)
  14.                         {
  15.                                 gpArray[i] = new GeoPoint(xCoordArray[i], yCoordArray[i]);
  16.                                 pPoints[i] = new Point();
  17.                                 thePaths[i] = new Path();
  18.                         }
  19.                        
  20.                        
  21.                         for (int i = 0; i < counter; i++)
  22.                         {
  23.                                 projection.toPixels(gpArray[i], pPoints[i]);
  24.                         }
  25.                        
  26.                         for (int i = 0; i < counter; i++)
  27.                         {
  28.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

int theCounter = 1;
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                                 thePaths[i].moveTo(pPoints[theCounter].x, pPoints[theCounter].y);
  2.                                 thePaths[i].lineTo(pPoints[i].x, pPoints[i].y);
  3.                                 theCounter++;
  4.                                 canvas.drawPath(thePaths[i], mPaint);
  5.                         }
  6.  
  7. .................................................................
  8.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


I see what I was doing wrong I was setting theCounter to 1 each time it goes around so every path was starting at point 2.

But I still cannot get it to work from the array.

and here is the hard-coded one:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ...............................................
  2.  
  3. GeoPoint gP1 = new GeoPoint(53343391, -6271820);
  4.                 GeoPoint gP2 = new GeoPoint(53344223, -6271927);
  5.                 GeoPoint gP3 = new GeoPoint(53344198, -6273483);
  6.                 GeoPoint gP4 = new GeoPoint(53344697, -6273483);
  7.                 GeoPoint gP5 = new GeoPoint(53345069, -6273494);
  8.                 GeoPoint gP6 = new GeoPoint(53345011, -6272684);
  9.                 GeoPoint gP7 = new GeoPoint(53344976, -6272120);
  10.                 GeoPoint gP8 = new GeoPoint(53345063, -6270897);
  11.                 GeoPoint gP9 = new GeoPoint(53345120, -6270055);
  12.                 GeoPoint gP10 = new GeoPoint(53345229, -6269143);
  13.                
  14.                 Point p1 = new Point();
  15.                 Point p2 = new Point();
  16.                 Point p3 = new Point();
  17.                 Point p4 = new Point();
  18.                 Point p5 = new Point();
  19.                 Point p6 = new Point();
  20.                 Point p7 = new Point();
  21.                 Point p8 = new Point();
  22.                 Point p9 = new Point();
  23.                 Point p10 = new Point();
  24.  
  25.                 path1 = new Path();
  26.                 projection.toPixels(gP1, p1);
  27.                 projection.toPixels(gP2, p2);
  28.                 path1.moveTo(p2.x, p2.y);
  29.                 path1.lineTo(p1.x,p1.y);
  30.                 canvas.drawPath(path1, mPaint);
  31.                
  32.                 path2 = new Path();
  33.                 projection.toPixels(gP2, p2);
  34.                 projection.toPixels(gP3, p3);
  35.                 path2.moveTo(p3.x, p3.y);
  36.                 path2.lineTo(p2.x,p2.y);
  37.                 canvas.drawPath(path2, mPaint);
  38.              
  39.  
  40.                 path3 = new Path();
  41.                 projection.toPixels(gP3, p3);
  42.                 projection.toPixels(gP4, p4);
  43.                 path3.moveTo(p4.x, p4.y);
  44.                 path3.lineTo(p3.x,p3.y);
  45.                 canvas.drawPath(path3, mPaint);
  46.  
  47.               .............................
  48.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


[s]Any ideas or pointing out what I am doing wrong would be appreciated.[/s]

Never mind got it working.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. for (int i = 0, theCounter = 1; i < counter-1; i++)
  2.                         {
  3.                                 projection.toPixels(gpArray[i], pPoints[i]);
  4.                                 projection.toPixels(gpArray[theCounter], pPoints[theCounter]);
  5.                                 thePaths[i].moveTo(pPoints[theCounter].x, pPoints[theCounter].y);
  6.                                 thePaths[i].lineTo(pPoints[i].x, pPoints[i].y);
  7.                                 theCounter++;
  8.                                 canvas.drawPath(thePaths[i], mPaint);
  9.                         }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


If anyone knows of a simpler solution post it here.
Thanks.
hadoken13
Freshman
Freshman
 
Posts: 5
Joined: Fri Mar 26, 2010 12:07 pm
Location: Ireland

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 1 guest