0

So since the 21 July 2012 , as i can hear this is not possible anymore to use KLM to draw a path between 2 GeoPoint , i followed this :

Why retrieving Google Directions for Android using KML data is not working anymore?

But i get an Error , Here the onCreate() Method in my Activity :

  Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new     GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));
 RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
 mapView.getOverlays().add(routeOverlay);
 mapView.invalidate();

the Method Called in Activity :

 private Route directions(final GeoPoint start, final GeoPoint dest) {

//Changed from Parser to GoogleParser beacause i had Error
 GoogleParser parser;
//https://developers.google.com/maps/documentation/directions/#JSON <- get api
String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
final StringBuffer sBuf = new StringBuffer(jsonURL);
sBuf.append("origin=");
sBuf.append(start.getLatitudeE6()/1E6);
sBuf.append(',');
sBuf.append(start.getLongitudeE6()/1E6);
sBuf.append("&destination=");
sBuf.append(dest.getLatitudeE6()/1E6);
sBuf.append(',');
sBuf.append(dest.getLongitudeE6()/1E6);
sBuf.append("&sensor=true&mode=driving");
parser = new GoogleParser(sBuf.toString());
Route r =  parser.parse();
return r;
 }

LogCat Output :

 11-23 16:07:50.126: E/AndroidRuntime(24124): FATAL EXCEPTION: main
 11-23 16:07:50.126: E/AndroidRuntime(24124): java.util.NoSuchElementException
 11-23 16:07:50.126: E/AndroidRuntime(24124):   at      java.util.ArrayList$ArrayListIterator.next(ArrayList.java:572)
   11-23 16:07:50.126: E/AndroidRuntime(24124):     at         com.xxx.xxx.xxx.RouteOverlay.redrawPath(RouteOverlay.java:90)
  11-23 16:07:50.126: E/AndroidRuntime(24124):  at    com.xxx.xxx.xxx.RouteOverlay.draw(RouteOverlay.java:60)
 11-23 16:07:50.126: E/AndroidRuntime(24124):   at   com.google.android.maps.Overlay.draw(Overlay.java:179)
 11-23 16:07:50.126: E/AndroidRuntime(24124):   at 

Etc ...

Line Error (RouteOverlay.java) :

             prj.toPixels(it.next(), p);

On :

 private void redrawPath(final MapView mv) {
        final Projection prj = mv.getProjection();
        path.rewind();
        final Iterator<GeoPoint> it = routePoints.iterator();
        //Crash Here 
        prj.toPixels(it.next(), p);
        path.moveTo(p.x, p.y);
        while (it.hasNext()) {
                prj.toPixels(it.next(), p);
                path.lineTo(p.x, p.y);
        }
        path.setLastPoint(p.x, p.y);
}

Thanks .

Community
  • 1
  • 1
user1839514
  • 221
  • 1
  • 2
  • 10

1 Answers1

1

Seems like something changed and you're not getting any points now? To fix the crash, make your method more robust:

  if (it.hasNext()) {
    prj.toPixels(it.next(), p);
    path.moveTo(p.x, p.y);
    while (it.hasNext()) {
            prj.toPixels(it.next(), p);
            path.lineTo(p.x, p.y);
    }
    path.setLastPoint(p.x, p.y);
  }

Not sure why the array is empty now though.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • Yes , Thanks you this Fix the Crash , but the problem here is Why my Array is Empty , my GeoPoint are Valid i guess these are from the link i mentionned ... can it be related with sBuf.append("&sensor=true&mode=driving"); – user1839514 Nov 23 '12 at 16:13