Add Google Maps API V2 in a fragment

前端 未结 11 1970
误落风尘
误落风尘 2020-11-29 03:00

I\'m trying to show the map from the Google Maps API V2 in fragment. I tried with the SupportMapFragment, but I can\'t get the expected output. Also I\'m a beginner on this

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 03:22

    Here is my code.

    Create project in google map concole and generate api key.

    and then add dependency to build.gradle(app level).

    compile 'com.google.android.gms:play-services-maps:10.2.1'
    compile 'com.google.android.gms:play-services-location:10.2.1'
    compile 'com.google.android.gms:play-services-places:10.2.1'
    

    remember to add permissions in Android.manifest

    
        
        
        
        
        
        
        
        
    

    create activity_main.xml

    
    
        
    
    

    nothing to call from MainActivity.class

    Create fragment_location.xml

    
    
        
    
    

    Create FragmentLocation.class

    public class FragmentLocation extends Fragment implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener, LocationListener {
    
        public static final String TAG = FragmentLocation.class.getSimpleName();
        public static final int REQUEST_CODE_FOR_PERMISSIONS = 1;
        GoogleApiClient mGoogleApiClient;
        LatLng mLatLng;
        GoogleMap mGoogleMap;
        Marker mCurrLocationMarker;
        private LinearLayout linearMap;
        Location mLastLocation;
        LocationManager locationManager;
        boolean statusOfGPS;
        private Dialog mDialogGPS;
        View view;
        LocationRequest mLocationRequest;
        SupportMapFragment mFragment;
       FragmentManager fragmentManager;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            view=inflater.inflate(R.layout.fragment_location,container,false);
            fragmentManager=getChildFragmentManager();
            mFragment = (SupportMapFragment)fragmentManager.findFragmentById(R.id.map);
            mFragment.getMapAsync(this);
            if (!isGooglePlayServicesAvailable()) {
                Toast.makeText(getActivity(), "play services not available", Toast.LENGTH_SHORT).show();
            }
            locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
            statusOfGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            return view;
        }
    
        @Override
        public void onConnected(Bundle bundle) {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(1000); //5 seconds
            mLocationRequest.setFastestInterval(2000); //3 seconds
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            if (ContextCompat.checkSelfPermission(getActivity(),
                    Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            }
        }
    
        @Override
        public void onConnectionSuspended(int i) {
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
        }
    
        @Override
        public void onLocationChanged(Location location) {
            mLastLocation = location;
            if (mCurrLocationMarker != null) {
                mCurrLocationMarker.remove();
            }
            mLatLng = new LatLng(location.getLatitude(), location.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(mLatLng);
            markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
            mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));
            // Zoom in the Google Map
            mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    
        }
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mGoogleMap = googleMap;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (getActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED &&
                        getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                                == PackageManager.PERMISSION_GRANTED) {
                    buildGoogleApiClient();
                    mGoogleMap.setMyLocationEnabled(true);
    
                } else {
                    requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION
                                    , Manifest.permission.ACCESS_FINE_LOCATION},
                            REQUEST_CODE_FOR_PERMISSIONS);
                }
            } else {
                buildGoogleApiClient();
                mGoogleMap.setMyLocationEnabled(true);
            }
    
            //show dialog when click on location top-right side located on map.
            mGoogleMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
                @Override
                public boolean onMyLocationButtonClick() {
                    statusOfGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                    if (!statusOfGPS) {
                        turnOnGps();
                    } else {
                        getCurrentLocation(mLastLocation);
                    }
                    return false;
                }
            });
        }
    
        private boolean isGooglePlayServicesAvailable() {
            int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
            if (ConnectionResult.SUCCESS == status) {
                return true;
            } else {
                GooglePlayServicesUtil.getErrorDialog(status, getActivity(), 0).show();
                return false;
            }
        }
    
        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            switch (requestCode) {
                case REQUEST_CODE_FOR_PERMISSIONS:
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        if (getActivity().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
                                == PackageManager.PERMISSION_GRANTED &&
                                getActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                                        == PackageManager.PERMISSION_GRANTED) {
                            if (mGoogleApiClient == null) {
                                buildGoogleApiClient();
                            }
                            mGoogleMap.setMyLocationEnabled(true);
                        }
    
                    } else {
                        if (!(shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)) && (!(shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)))) {
                            Snackbar snackbar = Snackbar.make(linearMap, "Never asked"
                                    , Snackbar.LENGTH_INDEFINITE);
                            snackbar.setAction("Allow", new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent intent = new Intent();
                                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                    Uri uri = Uri.fromParts("package", getActivity().getPackageName(), null);
                                    intent.setData(uri);
                                    startActivity(intent);
                                }
                            });
                            snackbar.show();
                        }
                    }
                    break;
            }
        }
    
        private void getCurrentLocation(Location location) {
    
            mLastLocation = location;
    
            if (mCurrLocationMarker != null) {
                mCurrLocationMarker.remove();
            }
            if (locationManager != null) {
                if (ContextCompat.checkSelfPermission(getActivity(),
                        Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {
                    mLastLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);//getting last location
                }
                if (mLastLocation != null) {
                    if (mGoogleMap != null) {
                        Log.d("activity", "LOC by Network");
                        mLatLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
                        MarkerOptions markerOptions = new MarkerOptions();
                        markerOptions.position(mLatLng);
                        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                        mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
                        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(mLatLng));
                        mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
                    }
                }
            }
        }
    
        private void turnOnGps() {
            if (mGoogleMap != null) {
                mGoogleMap.clear();
            }
            statusOfGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);//getting status of gps whether gps is on or off.
            mDialogGPS = new Dialog(getActivity(), R.style.MyDialogTheme);
            mDialogGPS.requestWindowFeature(Window.FEATURE_NO_TITLE);
            mDialogGPS.setContentView(R.layout.dialog_turnongps);
            TextView txtCancel = (TextView) mDialogGPS.findViewById(R.id.txtCancel);
            TextView txtOK = (TextView) mDialogGPS.findViewById(R.id.txtSetting);
    
            ImageView imgLocation = (ImageView) mDialogGPS.findViewById(R.id.imgLocation);
    
            imgLocation.setImageResource(R.drawable.ic_location_my);
    
            txtCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialogGPS.dismiss();
                    //finish();
                    if (!statusOfGPS) {
                        getCurrentLocationByDefault();
                    } else {
                        getCurrentLocation(mLastLocation);
                    }
                }
            });
    
            txtOK.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //It is use to redirect to setting->location to turn on gps when press ok.
                    startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                    mDialogGPS.dismiss();
                    if (!statusOfGPS) {
                        getCurrentLocationByDefault();
                    } else {
                        getCurrentLocation(mLastLocation);
                    }
                }
            });
            mDialogGPS.show();
        }
    
        private void getCurrentLocationByDefault() {
            if (mCurrLocationMarker != null) {
                mCurrLocationMarker.remove();
            }
    
            if (mGoogleMap != null) {
                LatLng xFrom1 = new LatLng(0.0, 0.0);
                mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(xFrom1, (float) 0.0));
    
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(xFrom1);
                markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_taxi));
                mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
            } else {
                Log.i("MainActivity", "getCurrentLocationByDefault else");
            }
    
        }
    
        @Override
        public void onResume() {
            super.onResume();
            statusOfGPS = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            if (mDialogGPS != null) {
                if (mDialogGPS.isShowing()) {
                    mDialogGPS.dismiss();
                }
            }
            if (!statusOfGPS) {
                turnOnGps();
            } else {
                getCurrentLocation(mLastLocation);
            }
    
        }
        protected synchronized void buildGoogleApiClient() {
            if (mGoogleApiClient != null) {
                mGoogleApiClient = null;
            }
            mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
            mGoogleApiClient.connect();
        }
    }
    

    create dailog_gps.xml

    
    
    
        
    
            
    
        
    
        
    
            
    
        
    
        
    
            
    
            
    
        
    
        
    
            
    
            
    
        
    
    

    Hope this will help you :-)

提交回复
热议问题