如何在Google Map的View Pager中从适配器打开第一个选项?

时间:2019-02-23 12:10:32

标签: android

enter image description here我有一个地图屏幕。在其底部有从适配器获取的城市列表。当我打开此Google Map屏幕时,我希望该地图在打开后应从适配器聚焦到第一个位置。并且在移动列表之后,它也应相应移动。请帮助我。

我在片段中调用了此方法。

cityListViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {

            if (markerList.isEmpty())
                initMarkers();
            else {
                refreshMarker();
                performMarkerClick(position);
            }
            isIdle = false;

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

});

private void initMarkers(){         如果(courses.size()> 0){

        int size = courses.size();
        LatLngBounds.Builder allLatLng = new LatLngBounds.Builder();

        for (int i = 0; i < size; i++) {
            double latitude = courses.get(i).getLatitude();
            double longitude = courses.get(i).getLongitude();

            LatLng courseLatLang = new LatLng(latitude, longitude);

            allLatLng.include(courseLatLang);


            if (cityListViewPager.getCurrentItem() == i) {
                if (activeMarker != null) {
                    activeMarker.remove();
                    activeMarker = null;
                }
                activeMarker = googleMap.addMarker(new MarkerOptions().position(courseLatLang)
                        .icon(Utils.bitmapMarkerFromVector(getActivity(), R.drawable.ic_location_trans_active)));
                markerList.add(activeMarker);
            } else {
                Marker inactiveMarker = googleMap.addMarker(new MarkerOptions().position(courseLatLang).
                        icon(Utils.bitmapMarkerFromVector(getActivity(), R.drawable.ic_location_trans_inactive)));
                markerList.add(inactiveMarker);
            }
        }

        setBoundCameraPosition(allLatLng.build());
        tvNoOfCourse.setText(String.valueOf(markerList.size()));

    }


}


void refreshMarker() {
    for (int i = 0; i < markerList.size(); i++) {
        MarkerOptions markerOptions;
        markerList.get(i).remove();

        if (cityListViewPager.getCurrentItem() == i) {

            markerOptions = new MarkerOptions()
                    .icon(Utils.bitmapMarkerFromVector(getActivity(), R.drawable.ic_location_trans_active))
                    .position(markerList.get(i).getPosition());

        } else {
            markerOptions = new MarkerOptions()
                    .icon(Utils.bitmapMarkerFromVector(getActivity(), R.drawable.ic_location_trans_inactive))
                    .position(markerList.get(i).getPosition());
        }

        Marker marker = googleMap.addMarker(markerOptions);
        markerList.set(i, marker);
    }
    isIdle = false;
}

private void performMarkerClick(int position){

    CameraPosition camPos = googleMap.getCameraPosition();
    camPos = new CameraPosition(markerList.get(position).getPosition(), camPos.zoom, camPos.tilt, camPos.bearing);

    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
}

0 个答案:

没有答案