0

Scenario:Using OSM in android ,user draws the path manually on map and clicks on save button, to save in database as a string.

        Polyline myPolyline;

      /*
          updating myPolyline  here
      */

      SavePathButton.setOnClickListener(new View.OnClickListener() 
      {
        public void onClick(View arg0) 
        {   
                String tempGeoPointString="Begin";
                String fullPathStore="Begin ";
            List<GeoPoint> myPathPoints=new ArrayList<GeoPoint>(myPathOverLay.getNumberOfPoints());
            Collections.copy(myPathPoints, (myPolyline.getPoints()));   
            for (int i = 0; i < myPathPoints.size(); i++) 
            {
                    GeoPoint tempGeoPoint = myPathPoints.get(i);
                    tempGeoPointString=     String.valueOf(tempGeoPoint.getLatitude())+"#"+String.valueOf(tempGeoPoint.getLongitude());
                    fullPathStore=fullPathStore+tempGeoPointString;

            }
        }

    Log.d("Full Path is ",fullPathStore);
    Log.d("PolyLine Size",myPolyline.getNumberOfPoints());
    Log.d("PathPoints size",myPathPoints.size()
    });

Here's reference to Polyline class

myPolyline.getNumberOfPoints() is non zero,but the fullPathStore is not updated and myPathPoints.size() remains zero in the log. Collecions.copy is not used correctly?

kurtzmarc
  • 3,110
  • 1
  • 24
  • 40

1 Answers1

0

I'm not sure that "Collections.copy(myPathPoints, (myPolyline.getPoints()));" copies properly GeoPoints.

But myPolyline.getPoints() already provides a copy of the polyline points. So don't copy it again, wasting time and memory. Just do:

List<GeoPoint> myPathPoints = myPolyline.getPoints();
MKer
  • 3,430
  • 1
  • 13
  • 18
  • myPathPoints.size() still remains zero and fullPathStore is not updated :( – user3596356 Jun 21 '14 at 13:47
  • I think I have an idea of the root cause (Polyline inheriting from PathOverlay). How do you put your points in the Polyline? with addPoint? If yes, only use setPoints. – MKer Jun 21 '14 at 19:22
  • Cannot use setPoints because I'll have only one point to be added to path. – user3596356 Jun 22 '14 at 13:59
  • to add a point: polyline.setPoints(polyline.getPoints().add(newPoint)); – MKer Jun 22 '14 at 18:41
  • Thanks,that works.`List a=myPolyline.getPoints(); a.add(position); myPolyline.setPoints(a);`..Clearpath had problems,so used setPoints() to clear path also.. – user3596356 Jun 23 '14 at 18:33
  • New version of OSMBonusPack has been published (v4.6), where Polyline do not inherit from PathOverlay anymore. This will remove this kind of issue (no more access to PathOverlay methods). – MKer Jul 01 '14 at 12:11