-1

when my location is null my application force closes. I'm certain it's because the location returns null. I tried using the code with Mock GPS and the code works when it passes through the "if (myLocation != null)". I just can't "return myLocation;" after the "if (myLocation == null)"

Please help. Thank you.

    @Override
public void onMapReady(GoogleMap googleMap) {


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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.INTERNET,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,

            }, 10);
        }
        return;
    }

    gMap = googleMap;

        if(!gMap.isMyLocationEnabled())
            gMap.setMyLocationEnabled(true);

        LocationManager lm = (LocationManager)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);
    }

if I add "return myLocation;" after the "if( myLocation == null )"

LIKE THIS

It returns an error. IMAGE OF THE ERROR IMAGE OF THE ERROR 2

  • 1
    Your method is void, which means it doesn't return a value back. You have it set up to call this method and pass something to it, but it does not return something back. – Adam Gardner Mar 04 '17 at 15:16
  • simply create a function and use `myLocation` to do whatever you want to do in that function – Pavneet_Singh Mar 04 '17 at 15:17
  • @PavneetSingh - function outside the onMapReady? – Simone Walter Mar 04 '17 at 16:21
  • you can create a function , move your logic that required the use of `myLocation` and call that function after this line `myLocation = lm.getLastKnownLocation(provider);` – Pavneet_Singh Mar 04 '17 at 16:27

1 Answers1

1

its because you are using void type return

public void onMapReady(GoogleMap googleMap)

use Location type.

public Location onMapReady(GoogleMap googleMap)

public void onMapReady(GoogleMap googleMap) is a abstract "call back" usage method, but i can see that you want to have a method to return a Location obj to use. There is several way to do. One is use a global object to store the returned result like:

private Location location = null;
@Override
public void onMapReady(GoogleMap googleMap){
......
this.location =  myLocation;
}
public void someMethodIUseLocation(){
//TO CALL this.location if need to use 
}

just depends on how you wanted to call the way to implement is different

CbL
  • 734
  • 5
  • 22