在Google地图上有效地绘制大距离的折线

时间:2017-06-03 17:52:38

标签: android google-maps android-fragments google-direction

我目前正在尝试利用Google的Directions API中请求的JSON数据在两个目的地之间绘制路线。我目前的版本适用于150英里范围内的目的地。然而,当我尝试跨状态绘制多边形线时,应用程序崩溃了。以下是我的异步任务的片段。

public class FetchRouteStepsFromService extends AsyncTask<Void,Void,StringBuilder> {

    private LocalBroadcastManager manager;
    private String currentAddress;
    private String destinationAddress;


    public FetchRouteStepsFromService(String currentAddress, String destinationAddress, Context context){
        this.currentAddress = currentAddress;
        this.destinationAddress = destinationAddress;
        manager = LocalBroadcastManager.getInstance(context);

    }


    private List<LatLng> decodePoly(String encoded) {

        List<LatLng> poly = new ArrayList<LatLng>();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            LatLng p = new LatLng((((double) lat / 1E5)),
                    (((double) lng / 1E5)));
            poly.add(p);
        }

        return poly;
    }

    @Override
    protected void onPostExecute(StringBuilder result) {
        super.onPostExecute(result);
        try{

            JSONObject jsonObj = new JSONObject(result.toString());
            JSONArray routesJSONArray = jsonObj.getJSONArray("routes");
            JSONObject beforeLegsJSONObject = routesJSONArray.getJSONObject(0);
            JSONArray legsJSONArray = beforeLegsJSONObject.getJSONArray("legs");

            JSONObject beforeStepsJSONObject = legsJSONArray.getJSONObject(0);
            JSONArray stepsJSONArray = beforeStepsJSONObject.getJSONArray("steps");

            List<LatLng> test = new ArrayList<>();
            ArrayList<PolylineOptions> options = new ArrayList<>();
            map.clear();
            for(int i = 0; i < stepsJSONArray.length(); i++){
                JSONObject object = stepsJSONArray.getJSONObject(i);
                JSONObject polyLineObject = object.getJSONObject("polyline");
                String encodedPoly = polyLineObject.getString("points");//Holds the code for the polyline (String)
                test = decodePoly(encodedPoly);
                //Todo: Maybe create a separate asynctask to add latlngs on separate thread?
                for(int j = 0; j < test.size();j++){

                    PolylineOptions options1;
                    if(j != test.size() -1) {
                        LatLng startLocation = test.get(j);
                        LatLng nextLocation = test.get(j + 1);
                        options1 = new PolylineOptions().add(startLocation, nextLocation).width(5).color(Color.GREEN).geodesic(true);
                        map.addPolyline(options1);
                    }else{
                        LatLng startLocation = test.get(j);
                        LatLng nextLocation = test.get(j);
                        options1 = new PolylineOptions().add(startLocation, nextLocation).width(5).color(Color.GREEN).geodesic(true);
                        map.addPolyline(options1);

                    }

                }

            }
            dialog.dismiss();
            updateUI();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected StringBuilder doInBackground(Void... params) {
        try{

            StringBuilder jsonResults = new StringBuilder();
            String googleMapUrl = "https://maps.googleapis.com/maps/api/directions/json?" +
                    "origin="+currentAddress+"&" +
"destination="+destinationAddress+"&key=MY_KEY";

            URL url = new URL(googleMapUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            InputStreamReader in = new InputStreamReader(conn.getInputStream());
            int read;
            char[] buff = new char[3000];

            while((read = in.read(buff,0,3000)) != -1 ){
                jsonResults.append(buff,0,read);

            }
            return jsonResults;

        }catch (Exception e){

            Log.d("PlanTrip","doInBackgroud exception");
            e.printStackTrace();
        }
        return null;
    }



}

这是我的片段中的本地课程,其中包含Google地图。我以前曾试图让这个AsyncTask成为自己的类。该类将广播由PolyLineOptions组成的意图,该意图将由片段的BroadcastReceiver接收。虽然这也不起作用。任何资源,建议或反馈将不胜感激。

编辑1:大请求期间的Logcat

enter image description here

enter image description here

0 个答案:

没有答案