Android

Mapwize integrations in Android (Kotlin)

Integration Sample

Adding the dependency

  1. Add the Crowd Connected and Mapwize repositories to your Gradle build file
repositories {
    jcenter()
    maven { url "https://jitpack.io" }
    maven { url "https://maven.mapwize.io" }
    maven { url "http://maven.crowdconnected.net/" }
}
  1. Add the libraries to your dependencies
implementation "net.crowdconnected.androidcolocator:colocator:2.4.2"
implementation "com.github.Mapwize:mapwize-ui-android:2.0.5"
implementation "io.mapwize.indoormaps:MapwizeForMapbox:2.3.+"

Getting Permission

Location permission should be requested within your app onboarding process. To increase location opt-ins explain the user how location data will be used, such as sending them relevant push notifications. Read more on maximising location opt-ins

ActivityCompat.requestPermissions(thisActivity, new String[]{ Manifest.permission.ACCESS_FINE_LOCATION }, 0);

Enabling Location Gathering

If the user has granted Location Permission start the Colocator library

CoLocator.start(this.getApplication(), YOUR_APP_KEY);

Configure Mapwize

API Access

In your MainApplication file add

import io.mapwize.mapwizeformapbox.AccountManager

Inside onCreate() method add

AccountManager.start(this, MAPWIZE_API_KEY);

Setup Map

In your MainActivity file extend OnFragmentInteractionListener

class MainActivity : AppCompatActivity(), MapwizeFragment.OnFragmentInteractionListener

Add Mapwize items

private var mapwizeFragment: MapwizeFragment? = null
private var mapboxMap: MapboxMap? = null
private var mapwizePlugin: MapwizePlugin? = null

Declare your Organisation ID and Venue ID

var organisationID = "YOUR_ORGANISATION_ID"
var venueID = "YOUR_VENUE_ID"

Configure Mapwize fragment

val opts = MapOptions.Builder()
        .restrictContentToOrganization(organisationID)
        .centerOnVenue(venueID)
        .build()

var uiSettings = MapwizeFragmentUISettings.Builder()
        .build()

this.mapwizeFragment = MapwizeFragment.newInstance(opts, uiSettings)
val fm = supportFragmentManager
val ft = fm.beginTransaction()
ft.add(fragmentContainer.id, mapwizeFragment!!)
ft.commit()

In onFragmentReady(mapboxMap: MapboxMap?, mapwizePlugin: MapwizePlugin?) method of your MainActivity add

this.mapboxMap = mapboxMap
this.mapwizePlugin = mapwizePlugin
this.locationProvider = ColocatorIndoorLocationProvider(this)
this.mapwizePlugin?.setLocationProvider(this.locationProvider!!)
this.locationProvider!!.start()
this.mapboxMap?.locationComponent?.renderMode = RenderMode.NORMAL

Setup Location Provider

Create a class that extends IndoorLocationProvider

public class ColocatorIndoorLocationProvider extends IndoorLocationProvider

Add variables for retaining location data

private Double headingOffset;
private double lat, lng, error;
private boolean hadLocation = false;
private Float bearing;

Setup Colocator

Register for Colocator location updates

CoLocator.instance().registerLocationListener(new LocationCallback() {
    @Override
    public void onLocationReceived(LocationResponse clientLocationResponse) { }

    @Override
    public void onLocationsReceived(List<LocationResponse> list) {
        for (LocationResponse response : list) {
            hadLocation = true;
            if (response.getHeadingOffset() != 0) {
                headingOffset = response.getHeadingOffset();
            } else {
                headingOffset = null;
                bearing = null;
            }
            lat = response.getLatitude();
            lng = response.getLongitude();
            error = response.getError();
            Location location = new Location("");
            location.setLatitude(lat);
            location.setLongitude(lng);
            location.setAccuracy((float) error);
            if (bearing != null) {
                location.setBearing(bearing);
            }

            // Check the floor number or contact CrowdConnected if you need to use multiple floors
            dispatchIndoorLocationChange(new IndoorLocation(location, 0D));
        }
    }
});

Setup Sensor Events monitorization

Declare Sensor Manager and Sensor Callback

private SensorManager sensorManager;
private SensorCallback sensorCallback;

Register listener for sensor updates

this.sensorManager = (SensorManager) activity.getSystemService(SENSOR_SERVICE);
sensorCallback = new ColocatorIndoorLocationProvider.SensorCallback();
sensorManager.registerListener(sensorCallback, sensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR), SensorManager.SENSOR_DELAY_NORMAL);

Handle sensor events

Add SensorCallback class for listening to sensor events

private class SensorCallback implements SensorEventListener {
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        double x = sensorEvent.values[0];
        double y = sensorEvent.values[1];
        double z = sensorEvent.values[2];
        double cos = sensorEvent.values[3];
        double u = 1.0 - 2.0 * (y * y + z * z);
        double v = 2.0 * (cos * z + x * y);
        double azimuth_rad = Math.atan2(v, u);
        double azimuth_deg = azimuth_rad * 180.0 / Math.PI;
        if (hadLocation && headingOffset != null) {
            bearing = (float) ((azimuth_deg + headingOffset - 90.0) * -1.0);
            Location location = new Location("");
            location.setLatitude(lat);
            location.setLongitude(lng);
            location.setAccuracy((float) error);
            location.setBearing(bearing);
            dispatchIndoorLocationChange(new IndoorLocation(location, 0D));
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) { }
}

Setup IndoorLocationProvider for Mapwize

In MainActivity add a ColocatorIndoorLocationProvider

private var locationProvider: ColocatorIndoorLocationProvider? = null

In onFragmentReady(mapboxMap: MapboxMap?, mapwizePlugin: MapwizePlugin?) method of your MainActivity add

this.mapwizePlugin?.setLocationProvider(this.locationProvider!!)

Stop Location Updates

Unregister location listener when user exit the map screen

Call the following method once the Mapwize map is dismissed

CoLocator.instance().unregisterLocationListener()