因此,自2012年7月21日起,我可以听到不再可能使用KLM在2 GeoPoint之间绘制路径,我跟着这个:
Why retrieving Google Directions for Android using KML data is not working anymore?
但我得到一个错误,这里是我的Activity中的onCreate()方法:
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();
在活动中调用的方法:
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输出:
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
等...
行错误(RouteOverlay.java):
prj.toPixels(it.next(), p);
开:
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);
}
谢谢。
答案 0 :(得分:1)
似乎有些事情发生了变化,你现在没有获得任何积分?要修复崩溃,请使方法更加健壮:
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);
}
现在不知道为什么数组是空的。