Unable to get the current location on real android device

Du Tran

I need to get the Lat and the Long of the current location to draw a route, my code works on the emulator but when I debug on real device, it does not. This is how I get the current location:

private void gotoCurrentLocation(){
     mMap.setMyLocationEnabled(true);
     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,listener);
     Location myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

     if(myLocation==null) { MYPLACE = null; return;}
     MYPLACE = new LatLng(myLocation.getLatitude(),myLocation.getLongitude());
     mMap.addMarker(new MarkerOptions().position(ll).title("You're here"));
}

LocationListener listener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        MYPLACE = new LatLng(location.getLatitude(),location.getLongitude());
    }

This is the map on the GenyMotion virtual device. When I debug on the real device, the map also has the tiny blue point at the current location like on the virtual device but MYPLACE is null and it could not place a marker at the current location. Is there something with the code? Could someone help me out please!!

route

This is on the real device:

enter image description here

Prokash Sarkar

This could happen because on some devices the GPS takes some time to initialize. If you request for your current location before the GPS gets initialized you'll surely get a null location value, since the GPS hasn't been initialized yet.

There are several ways of preventing this scenario. I would suggest you to use Play Services for location detection or you can simply use a library called ,

Android-ReactiveLocation

You can request the Reactive Location to get the last known location which will prevent from getting a null value,

ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);
locationProvider.getLastKnownLocation()
    .subscribe(new Action1<Location>() {
        @Override
        public void call(Location location) {
            doSthImportantWithObtainedLocation(location);
        }
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

unable to get longitude and latitude in android for real device

From Dev

Android: get current device location without asking permission at run time

From Dev

How to get current location IOS device

From Java

How to get current location in Android

From Dev

Android GPS get current location

From Dev

Unable to get current location from LocationListener

From Dev

Unable to get current location using FusedLocationApi

From Dev

Unable to get current location with google map

From Dev

android: unable to get updated location

From Dev

How to get the latitude and longitude of current device location in C#

From Dev

Get current android device time, if it was set manually

From Dev

Strange error into code to get current location with android

From Java

How to get the current location latitude and longitude in android

From Dev

how to get current location in google map android

From Dev

Get current location using GPS in Android

From Dev

How to get user's current location in Android

From Dev

Get current location with marker in android studio

From Dev

get the current location fast and once in android

From Dev

How to get Accurate Current location in android?

From Dev

How to get the current location temperature in android

From Dev

Android Location - how to get current and avoiding nullpointerexception

From Dev

How to get Accurate Current location in android?

From Dev

Get current location every n minutes in android

From Dev

How to get user's current location in Android

From Dev

android studio get Current location with googleapiclient

From Dev

How to get my current location in android maps?

From Dev

Unable to run an android app at a real device due to gradle being outdated

From Dev

Get Marker Location From Current Location Android Google Map

From Dev

Can't get current location android on location changed

Related Related

  1. 1

    unable to get longitude and latitude in android for real device

  2. 2

    Android: get current device location without asking permission at run time

  3. 3

    How to get current location IOS device

  4. 4

    How to get current location in Android

  5. 5

    Android GPS get current location

  6. 6

    Unable to get current location from LocationListener

  7. 7

    Unable to get current location using FusedLocationApi

  8. 8

    Unable to get current location with google map

  9. 9

    android: unable to get updated location

  10. 10

    How to get the latitude and longitude of current device location in C#

  11. 11

    Get current android device time, if it was set manually

  12. 12

    Strange error into code to get current location with android

  13. 13

    How to get the current location latitude and longitude in android

  14. 14

    how to get current location in google map android

  15. 15

    Get current location using GPS in Android

  16. 16

    How to get user's current location in Android

  17. 17

    Get current location with marker in android studio

  18. 18

    get the current location fast and once in android

  19. 19

    How to get Accurate Current location in android?

  20. 20

    How to get the current location temperature in android

  21. 21

    Android Location - how to get current and avoiding nullpointerexception

  22. 22

    How to get Accurate Current location in android?

  23. 23

    Get current location every n minutes in android

  24. 24

    How to get user's current location in Android

  25. 25

    android studio get Current location with googleapiclient

  26. 26

    How to get my current location in android maps?

  27. 27

    Unable to run an android app at a real device due to gradle being outdated

  28. 28

    Get Marker Location From Current Location Android Google Map

  29. 29

    Can't get current location android on location changed

HotTag

Archive