getLastKnownLocation returns null

Use this method to get the last known location: LocationManager mLocationManager; Location myLocation = getLastKnownLocation(); private Location getLastKnownLocation() { mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE); List<String> providers = mLocationManager.getProviders(true); Location bestLocation = null; for (String provider : providers) { Location l = mLocationManager.getLastKnownLocation(provider); if (l == null) { continue; } if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) … Read more

Android Location Providers – GPS or Network Provider?

There are 3 location providers in Android. They are: gps –> (GPS, AGPS): Name of the GPS location provider. This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission android.permission.ACCESS_FINE_LOCATION. network –> (AGPS, CellID, WiFi MACID): Name of the network location provider. … Read more

Calculate compass bearing / heading to location in Android

Ok I figured this out. For anyone else trying to do this you need: a) heading: your heading from the hardware compass. This is in degrees east of magnetic north b) bearing: the bearing from your location to the destination location. This is in degrees east of true north. myLocation.bearingTo(destLocation); c) declination: the difference between … Read more

How to mock location on device?

It seems the only way to do is to use a mock location provider. You have to enable mock locations in the development panel in your settings and add <uses-permission android:name=”android.permission.ACCESS_MOCK_LOCATION” /> to your manifest. Now you can go in your code and create your own mock location provider and set the location of this … Read more

Get User’s Current Location / Coordinates

To get a user’s current location you need to declare: let locationManager = CLLocationManager() In viewDidLoad() you have to instantiate the CLLocationManager class, like so: // Ask for Authorisation from the User. self.locationManager.requestAlwaysAuthorization() // For use in foreground self.locationManager.requestWhenInUseAuthorization() if CLLocationManager.locationServicesEnabled() { locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters locationManager.startUpdatingLocation() } Then in CLLocationManagerDelegate method you … Read more

Nginx serves .php files as downloads, instead of executing them

Try this: Edit /etc/nginx/sites-available/default Uncomment both listen lines to make nginx listen on port 80 IPv4 and IPv6. listen 80; ## listen for ipv4; this line is default and implied listen [::]:80 default_server ipv6only=on; ## listen for ipv6 Leave server_name alone # Make site accessible (…) server_name localhost; Add index.php to the index line root … Read more

What’s the difference between window.location= and window.location.replace()?

window.location adds an item to your history in that you can (or should be able to) click “Back” and go back to the current page. window.location.replace replaces the current history item so you can’t go back to it. See window.location: assign(url): Load the document at the provided URL. replace(url):Replace the current document with the one … Read more

How to check if Location Services are enabled?

You can use the below code to check whether gps provider and network providers are enabled or not. LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE); boolean gps_enabled = false; boolean network_enabled = false; try { gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch(Exception ex) {} try { network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch(Exception ex) {} if(!gps_enabled && !network_enabled) { // notify user … Read more