Skip to content

Location manager

Joan edited this page Apr 6, 2014 · 5 revisions

BeyondAR framework offers a few tools to make easier the usage of the location tools provided by Android, these tools are located in the BeyondarLocationManager class, in the com.beyondar.android.util.location package.

To use BeyondarLocationManager its needed to add ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions in the android manifest (before the <application> tag):

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Remember that the usage of the location sensors consume a lot of battery, so it is important to disable it when when is not needed, for that purpose BeyondarLocationManager.enable() and BeyondarLocationManager.disable(); are defined. For instance it is a good practice to disable the GPS sensors when the activity is not longer needed:

@Override
void onResume(){
    // Enable GPS
    BeyondarLocationManager.enable();
}

@Override 
void onPause(){
    // Disable GPS
    BeyondarLocationManager.disable();
}

In order to allow BeyondarLocationManager to manage the location sensors it is required to parse the location system service:

void onCreate(Bundle savedInstanceState){
    // ...
    BeyondarLocationManager.setLocationManager((LocationManager) this.getSystemService(Context.LOCATION_SERVICE));
    // ...
}

When the BeyondarLocationManager is ready you can register your own LocationListener, add a World or a GeoObject to be notified when the location changes. Don't forget to remove the listeners when they are not longer needed.

// With this method we request to have updated myWorld with the user location.
BeyondarLocationManager.addWorldLocationUpdate(myWorld);

// Add a custom LocationListener that will be notified always with the best location available.
BeyondarLocationManager.addLocationListener(myLocationListener);

// Specify a GeoObject to get updated with the user location.
BeyondarLocationManager.addGeoObjectLocationUpdate(myGeoObject);

Example code

You can find the source code of a complete example here.

Clone this wiki locally