question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem of getting my current location.

See original GitHub issue

Hallo, i have tried to get my current location using osmdroid, then getting the location of the client address from database.

First, i make it sure that i have the permissions inside manifestfile.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

I am implementing the map related code inside a Fragment. My problem is that i can not get my current location, i have tried the following:

private void trackingMyLocationTow() { locationProvider = new GpsMyLocationProvider(this); myLocationoverlay = new MyLocationNewOverlay(locationProvider, mapView); myLocationoverlay.enableMyLocation(); myLocationoverlay.setDrawAccuracyEnabled(true); myLocationoverlay.runOnFirstFix(new Runnable() { public void run() { myLatitude = myLocationoverlay.getMyLocation().getAltitude(); myLongitude = myLocationoverlay.getMyLocation().getLongitude(); } }); mapView.getOverlays().add(myLocationoverlay); mapView.invalidate(); }

Then i got 0 for both myLatitude and myLongitude.

I have also tried to get my current location using another code:

`import android.app.Service; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.widget.Toast;

public class LocationService extends Service { public static final String BROADCAST_ACTION = “Hello World”; private static final int TWO_MINUTES = 1000 * 60 * 2; public LocationManager locationManager; public MyLocationListener listener; public Location previousBestLocation = null;

Intent intent;
int counter = 0;

@Override
public void onCreate()
{
    super.onCreate();
    intent = new Intent(BROADCAST_ACTION);
}

@Override
public void onStart(Intent intent, int startId)
{
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    listener = new MyLocationListener();
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
}

@Override
public IBinder onBind(Intent intent)
{
    return null;
}

protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
        // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}



/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
        return provider2 == null;
    }
    return provider1.equals(provider2);
}

@Override
public void onDestroy() {
    // handler.removeCallbacks(sendUpdatesToUI);
    super.onDestroy();
    Log.v("STOP_SERVICE", "DONE");
    locationManager.removeUpdates(listener);
}

public static Thread performOnBackgroundThread(final Runnable runnable) {
    final Thread t = new Thread() {
        @Override
        public void run() {
            try {
                runnable.run();
            } finally {

            }
        }
    };
    t.start();
    return t;
}


public class MyLocationListener implements LocationListener {

    public void onLocationChanged(final Location loc) {
        if(isBetterLocation(loc, previousBestLocation)) {
            Toast.makeText( getApplicationContext(), "Your Lat is:"+loc.getLatitude(), Toast.LENGTH_LONG).show();
            Toast.makeText( getApplicationContext(), "Your Lng is:"+loc.getLongitude(), Toast.LENGTH_LONG).show();
            loc.getLatitude();
            loc.getLongitude();
            intent.putExtra("Latitude", loc.getLatitude());
            intent.putExtra("Longitude", loc.getLongitude());
            intent.putExtra("Provider", loc.getProvider());
            sendBroadcast(intent);
        }
    }

    public void onProviderDisabled(String provider) {
        Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_LONG).show();
    }


    public void onProviderEnabled(String provider) {
        Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_LONG).show();
    }


    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

}

} ` And again i got 0 for both lat and lng.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16

github_iconTop GitHub Comments

1reaction
spyhunter99commented, Nov 23, 2017

are you outside with gps service? does the device even have gps? is the gps turned on?

1reaction
spyhunter99commented, Nov 23, 2017

Hmm, did you do the whole permissions thing?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Find & improve your location's accuracy - - Google Help
Google Maps may have trouble finding your location. If the GPS location of your blue dot on the map is inaccurate or missing,...
Read more >
Top 6 Ways to Fix Location Services Not Working on Android
Top 6 Ways to Fix Location Services Not Working on Android · Open the Settings app on your phone and scroll down to...
Read more >
Fix Location Problem In Google Maps! [Android] - YouTube
Google Maps giving you trouble, such as Map GPS not working, showing wrong location, the direction arrow not working and so on?
Read more >
If Maps isn't working on your Apple device
Turn on Location Services and Location Access for Maps. In the Settings app, tap Privacy, then tap Location Services. · Set the date,...
Read more >
How to Fix Google Maps When It's Not Working on Android
No matter the issue, you should always keep the Maps app up to date on your Android device. Older app versions often have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found