使用另一个片段中调用的AsyncTask中的Google Map打开和重绘片段

时间:2015-12-29 13:43:02

标签: android google-maps android-asynctask

我有一个带有导航抽屉的Android应用程序和两个片段。其中一个包含谷歌地图。单击另一个片段中的按钮(findRoute方法)会调用AsyncTask(RouteAsyncTask)来获取路径的坐标。现在我想在地图上绘制路线,因此我必须使用Google Map打开第一个片段,并在AsyncTask的onPostExecute方法中更新我的地图。我怎么能这样做?

这是我的片段设置和onClick方法:

public class MainActivity extends AppCompatActivity {

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpToolbar();
        setUpNavDrawer();
        setUpContentFragment(MapFragment.class);
        mNavigationView = (NavigationView) findViewById(R.id.nav_view);
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                Class fragmentClass;

                menuItem.setChecked(true);
                switch (menuItem.getItemId()) {
                    case R.id.navigation_item_1:
                        mCurrentSelectedPosition = 0;
                        fragmentClass = MapFragment.class;
                        break;
                    case R.id.navigation_item_2:
                        mCurrentSelectedPosition = 1;
                        fragmentClass = RoutesFragment.class;
                        break;                     
                    default:
                        fragmentClass = MapFragment.class;
                        break;
                }
                mDrawerLayout.closeDrawers();
                setUpContentFragment(fragmentClass);
                return true;
            }
        });
    }

    public void setUpContentFragment(Class fragmentClass) {
        Fragment fragment = null;
        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception ex) {
            Log.e("ERROR", "Ex: ", ex);
        }
        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
    }
   ...
public void findRoute(View view) {
        ...
        RouteAsyncTask routeAsyncTask = new RouteAsyncTask(this, urlString);
        routeAsyncTask.execute();
}

这是我的RouteAsyncTask:

public class RouteAsyncTask extends AsyncTask<String, Void, ArrayList<LatLng>> {
    public static ArrayList<LatLng> polyline = new ArrayList<>();
    String urlString;
    Context context;

    public RouteAsyncTask(Context context, String urlString) {
        this.context = context;
        this.urlString = urlString;
    }

    @Override
    protected ArrayList<LatLng> doInBackground(String... strings) {
        ...
        return listGeopoints;
    }

@Override
    protected void onPostExecute(ArrayList<LatLng> latLngs) {
        super.onPostExecute(latLngs);
        if (latLngs != null) {
            polyline = latLngs;
        }
    }

以下是我在update中绘制路线的MapFragment方法:

public void update() {
        if (mMap != null && RouteAsyncTask.polyline != null) {
            ArrayList<LatLng> directionPoint = RouteAsyncTask.polyline;
            PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);
            for (int i = 0; i < directionPoint.size(); i++) {
                rectLine.add(directionPoint.get(i));
            }
            mMap.addPolyline(rectLine);
        }
    }

0 个答案:

没有答案