0

I want to save my position (latitude,longitude) in the map,when I tested this code in the genymotion emulator it worked but in my phone the application crashed despite the GPS is activated.

 if (googleMap == null) {

            googleMap = ((MapFragment) getFragmentManager().
                  findFragmentById(R.id.map)).getMap();

        }
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {


        }

        googleMap.setMyLocationEnabled(true);
        googleMap.getUiSettings().setZoomControlsEnabled(true);
        googleMap.getUiSettings().setCompassEnabled(true);
        googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        googleMap.getUiSettings().setAllGesturesEnabled(true);
        googleMap.setTrafficEnabled(true);

        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this);

        Button curent = (Button)findViewById(R.id.curent_button);
        curent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                LocationManager locationManager1 = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
                if (locationManager1.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

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

     if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                        return;
                    }
 Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  System.out.println("lat" + location.getLatitude() + "lng" + location.getLongitude());

       }
        });
marwa
  • 85
  • 3
  • 10
  • Could you post your logcat error? – antonio Jun 09 '16 at 09:34
  • Please post your logs where the error occured. Here's a related SO ticket creating Android App GPS get current location: http://stackoverflow.com/questions/36513543/android-gps-get-current-location?rq=1. Also, here's the Official Documentation for Maps Android API: https://developers.google.com/maps/documentation/android-api/location – Android Enthusiast Jun 10 '16 at 16:16

0 Answers0