0

I want get current location then i press one button in the app but it dont give me that instead it give unknown address but if i change my location to USA it gives the address? I dont get any errors everything is working good only the address dont show up? here is my code, If you could help me I would appreciate it.

    try {

        Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
        List<Address> addresses = geo.getFromLocation(currentLocation.getCoordinates().latitude, currentLocation.getCoordinates().longitude, 1);
        if (addresses.isEmpty()) {
            autocompleteFragmentFrom.setText(R.string.waiting_for_location);
        } else {
            addresses.size();
            if (addresses.get(0).getThoroughfare() == null) {
                pickupLocation.setName(addresses.get(0).getLocality());
            } else if (addresses.get(0).getLocality() == null) {
                pickupLocation.setName("unknown address");
            } else {
                pickupLocation.setName(addresses.get(0).getLocality() + ", " + addresses.get(0).getThoroughfare());
            }
            autocompleteFragmentFrom.setText(pickupLocation.getName());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

enter image description here

Diar Omar
  • 7
  • 4
  • If I understand you correct you want to test with a different location. Have you tried to add the `` permission as described here: https://stackoverflow.com/a/2559365 ? – Bruno Bieri Nov 22 '20 at 10:18
  • yes i have here is my manifests – Diar Omar Nov 22 '20 at 10:31
  • Please add this information to your question not as a comment. Could you please provide the code how you set the `currentLocation` variable? Can you also add if you test this on your device or on the emulator and if you have enabled the developer option `Allow Mock location`? – Bruno Bieri Nov 22 '20 at 10:36

1 Answers1

0

After request location permission and enable GPS use FusedLocationProviderClient.

private val mFusedLocationProviderClient: FusedLocationProviderClient by lazy {
                        LocationServices.getFusedLocationProviderClient(requireActivity())
                    }
                
    mFusedLocationProviderClient.lastLocation?.addOnSuccessListener(this) { location:Location? ->
    
                        val lat = location?.latitude
                        val lon = location?.longitude
                  
                    }
Ibrahim
  • 149
  • 4