Get GPS location

Asked

Viewed 86 times

2

I want to get the user’s location. My code is below, but when I run, I do not see anything, nor map the mark.

public class Gmapfragement extends Fragment implements OnMapReadyCallback {
    private GoogleMap mgoogleMap;
    private Location mlastLocation;
    LocationManager mgr;
    AlertDialog mnogps;
    String provider;

    public static Gmapfragement newInstance() {
        Gmapfragement fragment = new Gmapfragement();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.gmapsfrag, container, false);

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


        mgr = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

        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) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return null;
        }
        if(mgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
            mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new android.location.LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    double latitude=location.getLatitude();
                    double longitude=location.getLongitude();
                    LatLng latlang= new LatLng(latitude,longitude);
                    Geocoder geocoder= new Geocoder(getActivity());
                    try {
                        List<android.location.Address> adresslist= geocoder.getFromLocation(latitude,longitude,1);
                        String str=adresslist.get(0).getLocality()+",";
                        str+=adresslist.get(0).getCountryName();
                        mgoogleMap.addMarker(new MarkerOptions().position(latlang).title(str));
                        mgoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlang,10.2f));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {

                }

                @Override
                public void onProviderEnabled(String s) {

                }

                @Override
                public void onProviderDisabled(String s) {

                }
            });
        } else if(mgr.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new android.location.LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    double latitude=location.getLatitude();
                    double longitude=location.getLongitude();
                    LatLng latlang= new LatLng(latitude,longitude);
                    Geocoder geocoder= new Geocoder(getActivity());
                    try {
                        List<android.location.Address> adresslist= geocoder.getFromLocation(latitude,longitude,1);
                        String str=adresslist.get(0).getLocality()+",";
                        str+=adresslist.get(0).getCountryName();
                        mgoogleMap.addMarker(new MarkerOptions().position(latlang).title(str));
                        mgoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlang,10.2f));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {

                }

                @Override
                public void onProviderEnabled(String s) {

                }

                @Override
                public void onProviderDisabled(String s) {

                }
            });
        }



        return v;


    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mgoogleMap=googleMap;



    }





}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.