Skip to content

Commit

Permalink
Get location from best provider
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwedgbury committed May 25, 2018
1 parent ff6667b commit 1ae2bc6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.co.appoly.arcorelocation.sensor;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
Expand All @@ -27,19 +26,24 @@ public DeviceLocation(LocationScene locationScene) {
try {
// Getting LocationManager object
locationManager = (LocationManager) locationScene.mContext.getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object

boolean isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location location = null;

// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (isNetworkEnable) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, this);
} else if (isGPSEnable) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
} else {
Toast.makeText(locationScene.mContext, "All location providers are disabled.", Toast.LENGTH_SHORT).show();
return;
}

if (location != null)
onLocationChanged(location);
// Don't need this, as location may take a few seconds to come in
//else
// Toast.makeText(LocationScene.mContext, "Location can't be retrieved", Toast.LENGTH_SHORT).show();

if (location != null)
onLocationChanged(location);

} catch (SecurityException e) {
Toast.makeText(locationScene.mContext, "Enable location permissions from settings", Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -117,7 +121,7 @@ public void onProviderDisabled(String provider) {
public void onProviderEnabled(String provider) {
try {
locationManager.requestLocationUpdates(provider, 0, 0, this);
} catch(SecurityException e) {
} catch (SecurityException e) {

}
}
Expand Down
8 changes: 8 additions & 0 deletions examples/sceneform/local.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Sat May 19 17:01:22 CEST 2018
sdk.dir=C\:\\Users\\John\\AppData\\Local\\Android\\Sdk

0 comments on commit 1ae2bc6

Please sign in to comment.