Skip to content

Instantly share code, notes, and snippets.

View mizutori's full-sized avatar

Takamitsu Mizutori mizutori

View GitHub Profile
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
if (provider == LocationManager.GPS_PROVIDER) {
if (status == LocationProvider.OUT_OF_SERVICE) {
notifyLocationProviderStatusUpdated(false)
} else {
notifyLocationProviderStatusUpdated(true)
}
}
}
val batteryInfoReceiver = object : BroadcastReceiver() {
override fun onReceive(ctxt: Context, intent: Intent) {
val batteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
val batteryLevelScaled = batteryLevel / scale.toFloat()
batteryLevelArray.add(Integer.valueOf(batteryLevel))
batteryLevelScaledArray.add(java.lang.Float.valueOf(batteryLevelScaled))
batteryScale = scale
/* Kalman Filter */
var Qvalue: Float = 3.0f
val locationTimeInMillis = location.elapsedRealtimeNanos / 1000000
val elapsedTimeInMillis = locationTimeInMillis - runStartTimeInMillis
if (currentSpeed == 0.0f) {
Qvalue = 3.0f //3 meters per second
} else {
Qvalue = currentSpeed // meters per second
@SuppressLint("NewApi")
private fun getLocationAge(newLocation: Location): Long {
val locationAge: Long
if (android.os.Build.VERSION.SDK_INT >= 17) {
val currentTimeInMilli = SystemClock.elapsedRealtimeNanos() / 1000000
val locationTimeInMilli = newLocation.elapsedRealtimeNanos / 1000000
locationAge = currentTimeInMilli - locationTimeInMilli
} else {
locationAge = System.currentTimeMillis() - newLocation.time
}
private fun filterAndAddLocation(location: Location): Boolean {
val age = getLocationAge(location)
if (age > 5 * 1000) { //more than 5 seconds
Log.d(LOG_TAG, "Location is old")
oldLocationList.add(location)
return false
}
override fun onLocationChanged(newLocation: Location?) {
newLocation?.let{
Log.d(LOG_TAG, "(" + it.latitude + "," + it.longitude + ")")
gpsCount++
if (isLogging) {
//locationList.add(newLocation);
filterAndAddLocation(it)
}
private fun zoomMapTo(location: Location) {
val latLng = LatLng(location.latitude, location.longitude)
try {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.5f))
this.didInitialZoom = true
return
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun zoomMapTo(location: Location) {
val latLng = LatLng(location.latitude, location.longitude)
if (this.didInitialZoom == false) {
try {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.5f))
this.didInitialZoom = true
return
} catch (e: Exception) {
e.printStackTrace()
private fun addPolyline() {
locationService?.locationList?.let{locationList ->
runningPathPolyline?.let{
val toLocation = locationList[locationList.size - 1]
val to = LatLng(
toLocation.latitude,
toLocation.longitude
)
val points = it.points
private fun drawUserPositionMarker(location: Location) {
val latLng = LatLng(location.latitude, location.longitude)
if (this.userPositionMarkerBitmapDescriptor == null) {
userPositionMarkerBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.user_position_point)
}
userPositionMarker?.let{
it.setPosition(latLng)
} ?: run{