Skip to content

Location

Muhammad Utsman edited this page Jul 25, 2021 · 1 revision

With location library you can access current location, observer current location and get place or address of current and specified location (search).

Download

implementation 'io.github.utsmannn:geolib-location:{last_version}'

Prerequisite class

You need FusedLocationProviderClient

val fusedLocation = LocationServices.getFusedLocationProviderClient(context)

Create PlaceLocation

val placesLocation = fusedLocation.createPlacesLocation(HERE_API_KEY)

Current location

Observer current location

This function build under CoroutineScope with return Flow

MainScope().launch {
    // start observer location
    placesLocation.getLocationFlow()
        .collect { location ->
            // location result
        }
}

Get current location

MainScope().launch {
    val location = placesLocation.getLocationFlow().first()
}

Get comparator current location (prev and current)

MainScope().launch {
placesLocation.getComparisonLocation()
    .collect { comparisonLocation ->
        val prevLocation = comparisonLocation.previousLocation
        val currentLocation = comparisonLocation.currentLocation
    }
}

Place Location

Structure class of PlaceData

Param type desc
hereId String Id place from Here API
title String Title of place
address String Address of place
district String District of place
city String City of place
location Location Location of place
distance Double Distance of current location and place
category String nullable Category of place

Get place from location

val result: Result<List<PlaceData>> = placesLocation.getPlacesLocation(location)

result.doOnSuccess { places ->
    // handle success
}

result.doOnFailure {
    // handler failure
}

Search nearby place

Search place is searching nearby place on location by query with data result List<PlaceData>

val result: Result<List<PlaceData>> = placesLocation.searchPlaces(location, query)

result.doOnSuccess { places ->
    // handle success
}

result.doOnFailure {
    // handler failure
}