当我的汽车通过擦除前一行或更新新行来移动靠近用户时,如何动态地在地图上添加折线

时间:2018-03-09 14:16:38

标签: android google-maps dictionary google-polyline google-maps-direction-api

我正在为汽车预订做一个应用程序,在这里我需要在地图上添加行,当汽车从驾驶员位置移动到用户位置时。当我试图使用递归调用方向API时首先它在地图上添加行并将更新近6次并突然崩溃说空指针异常,但为什么?

TheLibabry.executeScript(scriptCode, apiObjects, timeout);

//这里我正在调用另一种在地图上添加行的方法,该方法工作正常但是当我这样做时,它会在6-7调用后崩溃。为什么?或者是他们以任何其他方式动态添加线条,因为汽车在用户i android附近移动

enter code here

        public void set_path_onmap( ) {

        LatLng driver_location = new LatLng(global_location.getLatitude(), global_location.getLongitude());
        Log.i("loc_drive", String.valueOf(driver_location));
        Log.i("loc_user", String.valueOf(user_location));
        String url = getDirectionsUrl(user_location, driver_location);

        DownloadTask downloadTask = new DownloadTask();

        downloadTask.execute(url);


}
public void timecalling() {

    new CountDownTimer(5000, 1000) {

        public void onTick(long millisUntilFinished) {

        }

        public void onFinish() {
            set_path_onmap();
            timecalling();

        }
    }.start();

}

//这是我在地图上添加行的调用       //方向api开始********************************************* ************************************************** ************

enter code here

//在Google Map中为第i条路线绘制折线             mMap.addPolyline(lineOptions);

private class DownloadTask  extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... url) {
        String data = "";

        try {
            data = downloadUrl(url[0]);
        } catch (Exception e) {
            Log.d("Background Task", e.toString());
        }
        return data;
    }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);

        ParserTask parserTask = new ParserTask();


        parserTask.execute(result);


    }
}
private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>> {


    @Override
    protected List<List<HashMap<String, String>>> doInBackground(String... jsonData) {

        JSONObject jObject;
        List<List<HashMap<String, String>>> routes = null;

        try {
            jObject = new JSONObject(jsonData[0]);
            ParserTask.DirectionsJSONParser parser = new ParserTask.DirectionsJSONParser();

            routes = parser.parse(jObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return routes;
    }

    @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) {


      /*  List<LatLng> points = polyline.getPoints();
        points.add(newPoint);
        polyline.setPoints(points); */

         points = null;
        lineOptions = null;

        MarkerOptions markerOptions = new MarkerOptions();

        for (int i = 0; i < result.size(); i++) {
            points = new ArrayList();
            lineOptions = new PolylineOptions();

            List<HashMap<String, String>> path = result.get(i);

            for (int j = 0; j < path.size(); j++) {
                HashMap point = path.get(j);

                double lat = Double.parseDouble((String) point.get("lat"));
                double lng = Double.parseDouble((String) point.get("lng"));
                LatLng position = new LatLng(lat, lng);

                points.add(position);
                Log.i("pointsloop", String.valueOf(points));

            }

            lineOptions.addAll(points);
            lineOptions.width(8);
            lineOptions.color(Color.BLACK);
            lineOptions.geodesic(true);

        }
        Log.i("pointsoutside", String.valueOf(points));
        Log.i("mapline", String.valueOf(lineOptions));

0 个答案:

没有答案