How to get Google Maps object inside a Fragment

前端 未结 7 1915
走了就别回头了
走了就别回头了 2021-02-19 01:26

I am using fragments in my application and I am new for this. There is a fragment in which I want to display Google Map and want to get its object, for this I have a fragment in

7条回答
  •  面向向阳花
    2021-02-19 02:10

    Try this i have made this as a sample according to your need

    public class LocationMapActivity extends FragmentActivity {
        private GoogleMap mMap;
        static boolean Iscamera = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.locatio_map);
    
            setUpMapIfNeeded();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            setUpMapIfNeeded();
    
        }
    
        private void setUpMapIfNeeded() {
    
            if (mMap == null) {
    
                // Try to obtain the map from the SupportMapFragment.
                mMap = ((SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map)).getMap();
                mMap.setMyLocationEnabled(true);
    
                if (mMap != null) {
    
                    mMap.setMyLocationEnabled(true);
                    mMap.getUiSettings().setCompassEnabled(true);
                    mMap.getUiSettings().setZoomControlsEnabled(true);
                    mMap.getMaxZoomLevel();
                    mMap.getMinZoomLevel();
                    mMap.getUiSettings();
                    mMap.animateCamera(CameraUpdateFactory.zoomIn());
                    mMap.animateCamera(CameraUpdateFactory.zoomOut());
                    mMap.animateCamera(CameraUpdateFactory.zoomTo(20));
    
                    mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
                        @Override
                        public void onMyLocationChange(Location arg0) {
                            // TODO Auto-generated method stub
    
                            mMap.clear();
                            mMap.addMarker(new MarkerOptions()
                            .position(
                                    new LatLng(arg0.getLatitude(), arg0
                                            .getLongitude()))
                            .title("I am Here!!")
                            .icon(BitmapDescriptorFactory
                                    .fromResource(R.drawable.map_icon)));
                            if (!Iscamera) {
                                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                                        new LatLng(arg0.getLatitude(), arg0
                                                .getLongitude()), 14));
    
    
                                Iscamera = true;
                            }
                            try {
    
                                if (Constant.FORTNAME.equals("Arad Fort")) {
                                    mMap.addMarker(new MarkerOptions()
                                            .position(new LatLng(26.2525, 50.6269))
                                            .title(Constant.FORTNAME)
                                            .icon(BitmapDescriptorFactory
                                                    .fromResource(R.drawable.greenicon)));
    
                                } else if (Constant.FORTNAME.equals("Bahrain Fort")) {
                                    mMap.addMarker(new MarkerOptions()
                                            .position(
                                                    new LatLng(26.2333598,
                                                            50.52035139))
                                            .title(Constant.FORTNAME)
    
                                            .icon(BitmapDescriptorFactory
                                                    .fromResource(R.drawable.greenicon)));
                                } else if (Constant.FORTNAME
                                        .equals("International Circuit")) {
                                    mMap.addMarker(new MarkerOptions()
                                            .position(
                                                    new LatLng(26.0303251,
                                                            50.51121409))
                                            .title(Constant.FORTNAME)
    
                                            .icon(BitmapDescriptorFactory
                                                    .fromResource(R.drawable.greenicon)));
    
                                } else if (Constant.FORTNAME
                                        .equals("Khamis Mousque")) {
                                    mMap.addMarker(new MarkerOptions()
                                            .position(
                                                    new LatLng(26.158719, 50.516426))
                                            .title(Constant.FORTNAME)
    
                                            .icon(BitmapDescriptorFactory
                                                    .fromResource(R.drawable.greenicon)));
    
                                } else if (Constant.FORTNAME
                                        .equals("king fahad causeway")) {
                                    mMap.addMarker(new MarkerOptions()
                                            .position(
                                                    new LatLng(26.1723987,
                                                            50.4579942))
                                            .title(Constant.FORTNAME)
    
                                            .icon(BitmapDescriptorFactory
                                                    .fromResource(R.drawable.greenicon)));
    
                                } else if (Constant.FORTNAME.equals("Riffa Fort")) {
                                    mMap.addMarker(new MarkerOptions()
                                            .position(
                                                    new LatLng(26.11771965026855,
                                                            50.56298065185547))
                                            .title(Constant.FORTNAME)
    
                                            .icon(BitmapDescriptorFactory
                                                    .fromResource(R.drawable.greenicon)));
    
                                } else if (Constant.FORTNAME
                                        .equals("Royal Golf Club")) {
                                    mMap.addMarker(new MarkerOptions()
                                            .position(
                                                    new LatLng(26.13000, 50.55500))
                                            .title(Constant.FORTNAME)
    
                                            .icon(BitmapDescriptorFactory
                                                    .fromResource(R.drawable.greenicon)));
    
                                } else if (Constant.FORTNAME.equals("Tree of life")) {
                                    mMap.addMarker(new MarkerOptions()
                                            .position(
                                                    new LatLng(25.9940396,
                                                            50.583135500000026))
                                            .title(Constant.FORTNAME)
    
                                            .icon(BitmapDescriptorFactory
                                                    .fromResource(R.drawable.greenicon)));
    
                                }
    
                            } catch (Exception e) {
                                Toast.makeText(getApplicationContext(),
                                        e.getMessage(), Toast.LENGTH_LONG).show();
                                Log.e(e.getClass().getName(), e.getMessage(), e);
                            }
    
                        }
                    });
    
                }
            }
    
        }
    
        // ********************************************************************************************
        @SuppressWarnings("unused")
        private void setUpMap() {
    
            mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title(
                    "Marker"));
        }
    }
    

提交回复
热议问题