Draw a Path in Google Maps Mapview Using Json

2019-07-04 00:22发布

问题:

I'm refering to this great example: See here

I'm getting the following errors and I can't find a solution. Please help! The NoSuchElementException is fired. In the MainActivity I'm doing it as described. In my Android-Manifest I also include

<uses-library android:name="com.google.android.maps" />

After starting the application the mapview shortly appears (grey patterns) and then suddenly the force close dialog appears.

My MainActivity:

public class MainActivity extends MapActivity {



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MapView mapView = (MapView) findViewById(R.id.mapview); //or you can declare it directly with the API key
    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);

}

private Route directions(final GeoPoint start, final GeoPoint dest) {
    Parser parser;
    String jsonURL = "http://maps.google.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;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
}

My activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.google.android.maps.MapView
    android:id="@+id/mapview" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:clickable="true"
    android:apiKey="..........................................." />

</RelativeLayout>

Errolog:

11-19 11:22:17.782: I/MapActivity(10703): Handling network change notification:CONNECTED
11-19 11:22:17.782: E/MapActivity(10703): Couldn't get connection factory client
11-19 11:22:17.822: D/AndroidRuntime(10703): Shutting down VM
11-19 11:22:17.822: W/dalvikvm(10703): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
11-19 11:22:17.872: E/AndroidRuntime(10703): FATAL EXCEPTION: main
11-19 11:22:17.872: E/AndroidRuntime(10703): java.util.NoSuchElementException
11-19 11:22:17.872: E/AndroidRuntime(10703):    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:579)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at com.example.testjsongooglemap.RouteOverlay.redrawPath(RouteOverlay.java:90)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at com.example.testjsongooglemap.RouteOverlay.draw(RouteOverlay.java:60)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at com.google.android.maps.Overlay.draw(Overlay.java:179)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at com.google.android.maps.MapView.onDraw(MapView.java:530)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.View.draw(View.java:7014)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.View.draw(View.java:7017)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.View.draw(View.java:7017)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2054)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewRoot.draw(ViewRoot.java:1632)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1335)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.os.Looper.loop(Looper.java:150)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at android.app.ActivityThread.main(ActivityThread.java:4385)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at java.lang.reflect.Method.invoke(Method.java:507)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
11-19 11:22:17.872: E/AndroidRuntime(10703):    at dalvik.system.NativeStart.main(Native Method)

回答1:

What you want to do is put the method directions() in another thread using AsyncTask because you cannot run network method over the UI thread. Also, use mapView.invalidate() in order to refresh (redraw the overlays) the MapView and see the route after drawing it.



回答2:

I believe line Route r = parser.parse(); is returning an empty route (i.e. no points inside).

To confirm, try adding the following after the above line:

Lod.d("MyApp", "Route size: " + r.getPoints().size());

And probably you will get size 0.

If this is the case, you need to chech why parser is not being able to process you paths points.

Regards.



回答3:

Here is how it looks like:

public class MainActivity extends MapActivity {

MapView mapView = null;
MapController mapController;
double long1 = 50.123*1E6;
double lati1 = 13.123*1E6;
double long2 = 50.221*1E6;
double lati2 = 13.221*1E6;
GeoPoint start, dest;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     mapView = (MapView) findViewById(R.id.mapview);


     DirectionsTask getDirectionsTask = new DirectionsTask();
     getDirectionsTask.execute(new GeoPoint((int)(long1),(int)(lati1)), new GeoPoint((int)(long2),(int)(lati2)));



}

private class DirectionsTask extends AsyncTask<GeoPoint, Void, Route> {

    protected Route doInBackground(GeoPoint...geoPoints) {
        start = geoPoints[0];
        dest =  geoPoints[1];

           Parser parser;
            String jsonURL = "http://maps.google.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");
            Log.v("I came in URL", sBuf.toString());
            parser = new GoogleParser(sBuf.toString());
            Route r =  parser.parse();
            return r;
    }




protected void onPostExecute(Route route) {
    RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
     mapView.getOverlays().add(routeOverlay);
     mapView.invalidate();
     mapController = mapView.getController();
     mapController.setZoom(14); // Zoom 1 is world view
     mapView.getMapCenter();


}

}