Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

google maps - Adding polylines in asynctask android

i'm having performance issues adding polylines and thought that maybe it'll be possible to add them in a separate class extending AsyncTask. However as i learned that UI elements can't be added in such way (and polylines are UI elements).

Why i'm having performance issues while drawing polylines? Well, my polylines are drawn not from pos A to pos B but from my current location to destination (which is hardcoded for the sake of application atm). So the polylines are drawn when onLocationChange listener is executed and thus my application requires lots of proccessing power.

Any ideas how to use AsyncTask on this occasion?

This is the main class:

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

This is the Async for path drawing (which will result in an error due to UI conflict):

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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved this issue in a way that i did not add every polyline map separately but whole polyline. For example, before i had my location about 4km away from destination and it had 280 polylines between. On every onLocationChange these polylines were drawn one-by-one to map. Now they're all added at once - AsyncTask will create polylines in the background and in the post-execute they will be added.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...