在asynctask android

时间:2015-10-07 14:38:40

标签: android google-maps android-asynctask

我在添加折线时遇到了性能问题,并认为可能会将它们添加到扩展AsyncTask的单独类中。但是,当我了解到UI元素无法以这种方式添加(并且折线是UI元素)。

为什么我在绘制折线时遇到性能问题?好吧,我的折线不是从pos A到pos B,而是从我当前的位置到目的地(为了应用atm而硬编码)。因此,当执行onLocationChange侦听器时会绘制折线,因此我的应用程序需要大量的处理能力。

在这种情况下如何使用AsyncTask的任何想法?

这是主要课程:

 mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

                @Override
                public void onMyLocationChange(Location arg0) {
                    // Get positions!
                    currentPOS = new LatLng(arg0.getLatitude(), arg0.getLongitude());
                    LatLng dst = new LatLng(58.378249, 26.714673);
                    CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(currentPOS, 13);
                    mMap.animateCamera(yourLocation);
                    mMap.addMarker(new MarkerOptions().position(dst).title("SCHOOL!"));
                    /*
                        // Remove comments to add marker to Liivi 2!
                        mMap.addMaker(new MarkerOptions().position(currentPOS).title("My POS"));
                     */
                    if (currentPOS != null) {
                        //This is supposed to show directions
                        DirectionAPI directionAPI = new DirectionAPI(currentPOS, dst);
                        GoogleResponse googleResponse = null;
                        try {
                            googleResponse = (GoogleResponse) directionAPI.execute().get();
                        } catch (InterruptedException e) {
                            Log.e("CATCH","INTERRUPDED");
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            Log.e("CATCH","EXECUTIONEXCEPTION");
                            e.printStackTrace();
                        }
                        if (googleResponse.isOk()){
                            DrivingDirection drivingDirection =  new DrivingDirection(googleResponse.getJsonObject());
                            polyline = drivingDirection.getTotalPolyline();
                            new drawPath(mMap,polyline).execute();
                        }
                    }
                }
            });

这是路径绘图的异步(由于UI冲突会导致错误):

import android.graphics.Color;
import android.os.AsyncTask;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

import java.util.ArrayList;

/**
 * Created by Kevin on 7.10.2015.
 */
public class drawPath extends AsyncTask{

    private static ArrayList<LatLng> polyline;
    private static GoogleMap mMap;

    public drawPath(GoogleMap map, ArrayList<LatLng> polyline){
        this.mMap = map;
        this.polyline = polyline;
    }

    @Override
    protected Object doInBackground(Object[] params) {
        for (int i = 0; i < polyline.size() - 1; i++) {
            LatLng src = polyline.get(i);
            LatLng dest = polyline.get(i + 1);

            // mMap is the Map Object
            Polyline line = mMap.addPolyline(
                    new PolylineOptions().add(
                            new LatLng(src.latitude, src.longitude),
                            new LatLng(dest.latitude,dest.longitude)
                    ).width(2).color(Color.BLUE).geodesic(true)
            );
        }

        return null;
    }
}

2 个答案:

答案 0 :(得分:1)

我解决了这个问题,我没有单独添加每个折线图,而是整个折线。例如,在我离目的地大约4公里的位置之前,它之间有280条折线。在每个onLocationChange上,这些折线一个接一个地绘制成地图。现在他们一次全部添加 - AsyncTask将在后台创建折线,在后执行中他们将被添加。

@Override
protected Object doInBackground(Object[] params) {
    PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
    for (int z = 0; z < polyline.size(); z++) {
        LatLng point = polyline.get(z);
        options.add(point);
    }
    return options;
}

protected void onPostExecute(Object result) {
    Polyline line = mMap.addPolyline((PolylineOptions) result);
}

答案 1 :(得分:0)

您可以使用此代码

ublic class DrawrootTask extends AsyncTask<String, String, String> {
private Context context;
public static boolean flagCompleted = false;
private GoogleMap googleMap;
private double source_lat = 0.0;
private double source_long = 0.0;
private double dest_lat = 0.0;
private double dest_long = 0.0;
Userdata userdata;
String tag = "DrawRootTask";
private ProgressDialog progressDialog;
public static double dist, time;
private Polyline line;
String distanceText = "";
String durationText = "";

public DrawrootTask(Context context, LatLng source, LatLng destination,
        GoogleMap googleMap) {
    source_lat = source.latitude;
    source_long = source.longitude;
    dest_lat = destination.latitude;
    dest_long = destination.longitude;

    this.googleMap = googleMap;
    this.context = context;
    userdata = Userdata.getinstance(context);

}

protected void onPreExecute() {
    // // TODO Auto-generated method stub
    super.onPreExecute();
    progressDialog = new ProgressDialog(context);
    progressDialog.setMessage(context.getResources().getString(
            R.string.please_wait));
    progressDialog.setIndeterminate(true);
    progressDialog.show();

}

@Override
protected String doInBackground(String... params) {

    String json = "";

    // constructor
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json");
    HashMap<String, String> keyValue = new HashMap<String, String>();
    urlString.append("?origin=");// from
    urlString.append(Double.toString(source_lat));
    urlString.append(",");
    urlString.append(Double.toString(source_long));
    urlString.append("&destination=");// to
    urlString.append(Double.toString(dest_lat));
    urlString.append(",");
    urlString.append(Double.toString(dest_long));
    urlString.append("&sensor=false&mode=driving&alternatives=true");

    // defaultHttpClient
    String url = urlString.toString();
    FetchUrl fetchurl = new FetchUrl();
    json = fetchurl.fetchUrl(url, keyValue);

    Log.e("Buffer Error", json);
    return json;

}

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    try {
        progressDialog.dismiss();
        final JSONObject json = new JSONObject(result);
        JSONArray routeArray = json.getJSONArray("routes");
        JSONObject routes = routeArray.getJSONObject(0);
        JSONObject overviewPolylines = routes
                .getJSONObject("overview_polyline");
        String encodedString = overviewPolylines.getString("points");
        List<LatLng> list = decodePoly(encodedString);

        for (int z = 0; z < list.size() - 1; z++) {
            LatLng src = list.get(z);
            LatLng dest = list.get(z + 1);
            line = googleMap.addPolyline(new PolylineOptions()
                    .add(new LatLng(src.latitude, src.longitude),
                            new LatLng(dest.latitude, dest.longitude))
                    // .width(8).color(Color.BLUE).geodesic(true));
                    .width(8)
                    .color(context.getResources().getColor(
                            R.color.actionbar_color)).geodesic(true));
            Log.i("draw root", "" + "" + line.toString());
        }
        JSONArray legs = routes.getJSONArray("legs");
        JSONObject steps = legs.getJSONObject(0);
        JSONObject duration = steps.getJSONObject("duration");
        JSONObject distance = steps.getJSONObject("distance");
        distanceText = distance.getString("text");
        durationText = duration.getString("text");
        Log.i("draw root", "" + distance.toString());
        dist = Double.parseDouble(distance.getString("text").replaceAll(
                "[^\\.0123456789]", ""));
        time = Double.parseDouble(duration.getString("text").replaceAll(
                "[^\\.0123456789]", ""));
        userdata.setDistance(distanceText);
        userdata.setTime(durationText);
        Log.d(tag, "distace is " + dist + " time is " + time);
        flagCompleted = true;
    } catch (JSONException e) {
        Log.d("draw root", "" + e);

    }
}

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;
}

}