如何将Google地图说明流式传输为使用JSON

时间:2014-08-11 20:14:58

标签: java android json google-maps-api-2

我正在为Android创建一个GPS应用程序,它将用户从当前位置引导到2个预定义位置之一。我正在从Google服务器下载指示,然后暂时将其存储在JSONObject中。如果方向变得太长或太复杂,则应用程序会因内存不足错误而崩溃。我想我需要使JSON对象可流化或将其更改为JSON阅读器,但我已经搜索了答案,但我不明白如何实现。任何人都可以使用下面的代码提供示例吗?提前谢谢!

public DrivingDirection(JSONObject response) {

    try {
        JSONArray jsonArray = response.getJSONArray(TAG_ROUTES);
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        summary = jsonObject.getString(TAG_SUMMARY);
        copyrights = jsonObject.getString(TAG_COPYRIGHTS);
        overviewPolyline =     decodePolyline(jsonObject.getJSONObject(TAG_OVERVIEW_POLYLINE).getString("points"));
        warnings = jsonObject.getJSONArray(TAG_WARNINGS);
        waypointOrder = jsonObject.getJSONArray(TAG_WAYPOINT_ORDER);
        JSONObject bounds = jsonObject.getJSONObject(TAG_BOUNDS);
        boundNorthEast = interpretLatLng(bounds.getJSONObject(TAG_BOUND_NORTH_EAST));
        boundSouthWest = interpretLatLng(bounds.getJSONObject(TAG_BOUND_SOUTH_WEST));
        JSONArray legs = jsonObject.getJSONArray(TAG_LEGS);
        directionLegs = new ArrayList<DirectionLeg>(legs.length());
        for (int i=0; i<legs.length(); i++){
            directionLegs.add(new DirectionLeg(legs.getJSONObject(i)));
        }
        directions = response.toString();
    } catch (JSONException e) {
       // Log.e(Debug.TAG, e.getMessage(), e);
    }
}

以下是LogCat关于错误的输出。

08-09 16:32:26.564: E/AndroidRuntime(2258): java.lang.IllegalStateException: Could not execute method of the activity
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.view.View$1.onClick(View.java:3599)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.view.View.performClick(View.java:4204)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.view.View$PerformClick.run(View.java:17355)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.os.Handler.handleCallback(Handler.java:725)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.os.Looper.loop(Looper.java:137)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at java.lang.reflect.Method.invoke(Method.java:511)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at dalvik.system.NativeStart.main(Native Method)
08-09 16:32:26.564: E/AndroidRuntime(2258): Caused by: java.lang.reflect.InvocationTargetException
08-09 16:32:26.564: E/AndroidRuntime(2258):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at java.lang.reflect.Method.invoke(Method.java:511)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at     android.view.View$1.onClick(View.java:3594)
08-09 16:32:26.564: E/AndroidRuntime(2258):     ... 11 more
08-09 16:32:26.564: E/AndroidRuntime(2258): Caused by: java.lang.OutOfMemoryError
08-09 16:32:26.564: E/AndroidRuntime(2258):     at oat.a(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at ova.j(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at oux.b(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at oux.a(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at oyf.a(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at grl.onTransact(SourceFile:137)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at android.os.Binder.transact(Binder.java:310)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addPolyline(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at com.google.android.gms.maps.GoogleMap.addPolyline(Unknown Source)
08-09 16:32:26.564: E/AndroidRuntime(2258):     at com.L00081183.mygpstool.MainActivity.onClick_GetDirections(MainActivity.java:177)
08-09 16:32:26.564: E/AndroidRuntime(2258):     ... 14 more
08-09 16:32:26.816: I/dalvikvm-heap(2258): Clamp target GC heap from 257.458MB to 256.000MB
08-09 16:32:26.816: D/dalvikvm(2258): GC_CONCURRENT freed 446K, 1% free 261480K/262144K,     paused 1ms+10ms, total 252ms

这是我调用getDirections方法的地方

public void onClick_GetDirections(View v) {

    LocationManager lm;    

    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location lastKnownLoc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);


    double longTemp = (double)(lastKnownLoc.getLongitude());
    double latTemp = (double)(lastKnownLoc.getLatitude());
    LatLng Start =  new LatLng(latTemp, longTemp);




            LatLng start = new LatLng(55.1082, -6.69342);
             LatLng end = new LatLng(54.509373, -8.180787);
             DirectionAPI directionAPI = new DirectionAPI(Start, LOCATION_LETTERKENNY);
             GoogleResponse googleResponse = directionAPI.execute();

             if (googleResponse.isOk()){
                 DrivingDirection drivingDirection =  new DrivingDirection(googleResponse.getJsonObject());

                 ArrayList<LatLng> polyline = drivingDirection.getTotalPolyline();
                 //PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);

                 PolylineOptions rectline = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
                 for (int z = 0; z < polyline.size(); z++) {
                     LatLng point = polyline.get(z);
                     rectline.add(point);

                     GPSToolMap.addPolyline(rectline);
                 }

             //GP

}

这就是addPolyline

中发生的事情
public void addPolyline(Polyline polyline){
    polyline.setPoints(this.overviewPolyline);
}

0 个答案:

没有答案