M
M
MacKenlly2018-03-16 11:27:43
Android
MacKenlly, 2018-03-16 11:27:43

How to place GoogleMaps and ViewPager on the same fragment?

Good afternoon, can anyone come across this problem.
It is necessary to make the following picture in one window:
1. At the top of the map (most of it)
2. At the bottom, the nearest points (as in Google maps), which can be reached on the ViewPager.
Here is the code itself:
1. Main window.

public class MapFragment extends Fragment implements OnMapReadyCallback {

    private ClusterManager<MarkerMaps> mClusterManager;
    private GoogleMap mMap;
    private List<MarkerMaps> mMarkers;
    private LatLng mUserLocation;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mMarkers = new ArrayList<>();
        UpdateUI();
        View view = inflater.inflate(R.layout.activity_maps_fragment, container, false);

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        Intent intent = MarkersPagerActivity.newIntent(getActivity(), mMarkers.get(1).getId());
        startActivityForResult(intent, 1);
        return view;
    }

    //обновление фрагмента
    private void UpdateUI() {
        MarkerLab markerLab = MarkerLab.get(getActivity());
        mMarkers = markerLab.getMarkers();
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
       // UpdateUI();
        if (ActivityCompat.checkSelfPermission(getContext(),
                android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(getContext(),
                        android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION,
                            android.Manifest.permission.ACCESS_FINE_LOCATION},
                    1);
        } else {
            Log.e("DB", "PERMISSION GRANTED");
        }

        mMap.setMyLocationEnabled(true);
        LocationManager lm = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
        Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (myLocation == null) {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String provider = lm.getBestProvider(criteria, true);
            myLocation = lm.getLastKnownLocation(provider);
        }
        LatLng userLocation = null;
        if (myLocation != null) {
            Log.w("Нам Пиздец", "Это точно");
            userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 14), 1500, null);
        }
        mUserLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
        setUpClusterer();
    }

    public static double CalculationDistance(LatLng StartP, LatLng EndP) {
        double distance = CalculationDistanceByCoord(StartP.latitude, StartP.longitude, EndP.latitude, EndP.longitude);
        Log.w("Distance", String.valueOf(distance));
        return distance;
    }

    private static double CalculationDistanceByCoord(double startPointLat,double startPointLon,double endPointLat,double endPointLon){
        float[] results = new float[1];
        Location.distanceBetween(startPointLat, startPointLon, endPointLat, endPointLon, results);
        return results[0];
    }

    private void setUpClusterer() {
        mClusterManager = new ClusterManager<MarkerMaps>(getContext(), mMap);
        MarkerRenderer customRenderer = new MarkerRenderer(getActivity(), mClusterManager, mMap);
        mClusterManager.setRenderer(customRenderer);


        mMap.setOnCameraIdleListener(mClusterManager);
        mMap.setOnMarkerClickListener(mClusterManager);
        addItems();
        mClusterManager.cluster();
    }

    private void addItems() {
        for(MarkerMaps marker : mMarkers) {
            LatLng place = marker.getPosition();
            if (CalculationDistance(place, mUserLocation) < 2000) {
                mClusterManager.addItem(marker);
            }
        }
    }
}

2.PagerActivity.
public class MarkersPagerActivity extends FragmentActivity {
    private static final String EXTRA_MARKER_ID =
            "com.bokoch.android.SportApp.Maps.marker_id";
    private ViewPager mViewPager;
    private List<MarkerMaps> mMarkers;
    public static Intent newIntent(Context packageContext, int markerId) {
        Intent intent = new Intent(packageContext, MarkersPagerActivity.class);
        intent.putExtra(EXTRA_MARKER_ID, markerId);
        return intent;
    }
   
    @Override
    protected void onCreate(Bundle savedInstaceState) {
        super.onCreate(savedInstaceState);
        setContentView(R.layout.activity_maps_fragment);
        int mMarkerId = (int) getIntent().getSerializableExtra(EXTRA_MARKER_ID);

        mViewPager = findViewById(R.id.marker_view_pager);
        mMarkers = MarkerLab.get(this).getMarkers();
        FragmentManager fragmentManager = getSupportFragmentManager();
        
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                MarkerMaps markerMaps = mMarkers.get(position);
                return MarkerFragment.newInstance(markerMaps.getId());
            }

            @Override
            public int getCount() {
                return mMarkers.size();
            }
        });
        for (int i = 0; i < mMarkers.size(); i++) {
            if (mMarkers.get(i).getId() == mMarkerId) {
                mViewPager.setCurrentItem(i);
                break;
            }
        }
    }

}

Thanks in advance)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question