var distanta = findViewById<EditText>(android.R.id.input)
km = seekBar.progress
km = km / 60
km2 = distanta / km
textView2.setText(km2).toString()
Project Link:https://drive.google.com/file/d/1s6Pi1kmC1oRmMqf0yJjtV1fAxiDuOr03/view?usp=sharing
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline operator fun BigDecimal.div(other: BigDecimal): BigDecimal defined in kotlin
public inline operator fun BigInteger.div(other: BigInteger): BigInteger defined in kotlin
Try this that may help you
var distanta = findViewById<EditText>(android.R.id.input)
km = seekBar.progress
km = km / 60
km2 = (distanta.text?.toString()?.toInt() ?: 0) / km
textView2.setText(km2.toString())
Related
Sup, guys! I have a task to calculate coordinates of continuation line between points A & B. I have a code each calculate distance between these points, and I have tangent of angle for calculating next points D and F. Thanks for any help
`val pointA = LatLng(a.latitude, a.longitude)
val pointB = LatLng(b.latitude, b.longitude)
val pointC = LatLng(a.latitude, b.longitude)//for calculating
val polylineOptions = PolylineOptions()
.add(pointA)
.add(pointB)
.add(pointC)
.add(pointA)
val distanceAB = getKmFromLatLong(pointA.latitude, pointA.longitude, pointB.latitude, pointB.longitude)
val distanceBC = getKmFromLatLong(pointB.latitude, pointB.longitude, pointC.latitude, pointC.longitude)
val distanceCA = getKmFromLatLong(pointC.latitude, pointC.longitude, pointA.latitude, pointA.longitude)
val tang = distanceBC/distanceCA
println("__ distanceAB $distanceAB")
println("__ distanceBC $distanceBC")
println("__ distanceCA $distanceCA")
println("__ tang $tang")`
Thanks, guys! Point-slope formula - rules
private fun generateNeedingPoints(a: LatLng, b: LatLng): List<LatLng> {
setScreenBounds()
val c: LatLng
val d: LatLng
val longitudeC = if (a.longitude < b.longitude) northEast.longitude else southWest.longitude
val longitudeD = if (a.longitude < b.longitude) southWest.longitude else northEast.longitude
c = calculateEdgeButton(a, b, longitudeC)
d = calculateEdgeButton(b, a, longitudeD)
return mutableListOf(d, a, b, c)
}
`
private fun calculateEdgeButton(a: LatLng, b: LatLng, edgeLng: Double, distortionCoefficient:Double = 1.0): LatLng{
val longChange = b.longitude - a.longitude
val latChange = b.latitude - a.latitude
val slope = latChange / longChange
val longChangePointC = edgeLng - b.longitude
val latChangePointC = longChangePointC * slope
val latitudeC = b.latitude + latChangePointC
return LatLng(latitudeC * distortionCoefficient, edgeLng)
}
`
The old Sensor.TYPE_ORIENTATION sensor returned a pitch between -180° and 180°. This was a nice API which included filtering and worked great. Sadly Sensor.TYPE_ORIENTATION was deprecated and is not available on modern phones.
The blessed replacement for Sensor.TYPE_ORIENTATION is a complicated combination of Context.SENSOR_SERVICE and TYPE_MAGNETIC_FIELD and the SensorManager.getRotationMatrix() and SensorManager.getOrientation() functions. You're on your own when it comes to filtering. (As an aside I used iirj - the trivial low pass filters I found on Stackoverflow did not work as well as whatever Sensor.TYPE_ORIENTATION did)
The documentation for getOrientation claims that it returns a pitch between -π to π. This can't be true since the implementation is values[1] = (float) Math.asin(-R[7]); (asin returns values between -π/2 and π/2)
Is there any way to get the full 360° of pitch and roll from the rotation matrix?
This is a known issue that Google won't fix. I created my own getOrientation function based on Gregory G. Slabaugh's paper
// Based on pseudo code from http://www.close-range.com/docs/Computing_Euler_angles_from_a_rotation_matrix.pdf
object EulerAngleHelper {
private const val R11 = 0
private const val R12 = 1
private const val R13 = 2
private const val R21 = 3
private const val R22 = 4
private const val R23 = 5
private const val R31 = 6
private const val R32 = 7
private const val R33 = 8
private const val AZIMUTH = 0
private const val PITCH = 1
private const val ROLL = 2
private const val PHI_Z = AZIMUTH
private const val PSI_X = PITCH
private const val THETA_Y = ROLL
fun getOrientation(r: DoubleArray, values: DoubleArray): DoubleArray {
when {
r[R31] < -0.98 -> {
values[PHI_Z] = 0.0 // Anything; can set to 0
values[THETA_Y] = Math.PI / 2
values[PSI_X] = values[PHI_Z] + atan2(r[R12], r[R13])
}
r[R31] > 0.98 -> {
values[PHI_Z] = 0.0 // Anything; can set to 0
values[THETA_Y] = -Math.PI / 2
values[PSI_X] = values[PHI_Z] + atan2(-r[R12], -r[R13])
}
else -> {
values[THETA_Y] = -asin(r[R31])
val cosTheta = cos(values[THETA_Y])
values[PSI_X] = atan2(r[R32] / cosTheta, r[R33] / cosTheta)
values[PHI_Z] = atan2(r[R21] / cosTheta, r[R11] / cosTheta)
}
}
return values
}
}
I've only tested the pitch and roll.
When i start my app, which should get the speed of the device, i get a NaN( i think its for " not a number"?) value for 1- 3 seconds in the textfield.The numbers are safed in a repository, but if is Nan, the app crashes.
How can i replace this NaN with 0(Zero)?... for ex.: If( NaNvalue = true){ (value = 0)}...something like that. I didnt find anything written in android or kotlin, just python(which i am not familiar with).
Any help would be great!!! Thank you...
my code:
fun getSpeed(location: Location): Float{
if (mRunning ) {
// val getkmh = (location.speed * 3.6)
// mSpeedKmH = getkmh.toFloat()
mElapsedSeconds = ((SystemClock.elapsedRealtime()
-mChrono!!.getBase()) / 1000)
mSpeedMeter = mDistanceTotal/mElapsedSeconds.toFloat()
mSpeed = ( mSpeedMeter *3.6).toFloat()// *3.6 > KmH
}
return mSpeedKmH
}
fun updateSpeed() {
val speedMeter = String.format(Locale.getDefault(), "%.1f",mSpeed)
tvSpeed.text = speedMeter
}
You can do it like the other answer shows or using takeIf and the elvis operator:
val speedMeter = String.format(Locale.getDefault(), "%.1f", mSpeed.takeIf { !it.isNaN() } ?: 0.0)
val value: Double = yourDoubleValue.let { if (it.isNaN()) 0.0 else it}
The same can be applied to Float values
Thanks to Fernando and Tenfour04!!! Both versions are working!!
val speedGesammt = String.format(Locale.getDefault(), "%.1f",
mSpeedAverageKmH.takeIf { !it.isNaN() } ?: 0.0)
val speedGesammt = String.format(Locale.getDefault(), "%.1f",
mSpeedAverageKmH.let { if (it.isNaN()) 0.0 else it})
I am building an Android application to measure 1km from a start position while driving.
i tried this : https://github.com/quentin7b/android-location-tracker/blob/master/README.md
val settings = TrackerSettings()
.setUseNetwork(true)
.setUseGPS(true)
.setUsePassive(false)
.setTimeBetweenUpdates(1000)
.setMetersBetweenUpdates(10F)
And i change postions to get distance between to position using:
val distance = currentPosition!!.distanceTo(location)
totalDistance += distance
But the result is not precise, anyone how can give a better solution
private fun distance(
startLatitude: Double,
startLongitude: Double,
endLatitude: Double,
endLongitude: Double,
unit: String
): String {
val theta = startLongitude - endLongitude
var dist: Double = sin(deg2rad(startLatitude)) * sin(deg2rad(endLatitude)) +
cos(deg2rad(startLatitude)) * cos(deg2rad(endLatitude)) * cos(deg2rad(theta))
dist = acos(dist)
dist = rad2deg(dist)
dist *= 60 * 1.1515 //miles
if (unit == "k") {
dist *= 1.609344 // km
}
return dist.toString()
}
private fun deg2rad(deg: Double): Double {
return deg * 3.1415926535897932 / 180.0
}
private fun rad2deg(rad: Double): Double {
return rad * 180.0 / 3.1415926535897932
}
You can use android.location.Location.distanceBetween() method which does this quite well. For more info
I'm using Google Places api in my app which calculate the distance between 2 address,
the problem is that I get less distance than in google maps.
I cannot get the same accuracy as Google maps,
Most of the time distance is shorter than the result from Google Maps
private fun SetupPlacesAutocompleteFun() {
val _autocompletFragment1 = supportFragmentManager
.findFragmentById(R.id.fragmentPlaces1) as AutocompleteSupportFragment
_autocompletFragment1.setPlaceFields(_placesFields)
_autocompletFragment1.setOnPlaceSelectedListener(object:PlaceSelectionListener{
override fun onPlaceSelected(p1: Place) {
_adrees1 = p1.latLng!!
_adressString1 = p1.address!!
}
override fun onError(p1: Status) {
Toast.makeText(this#GooglePlaces_Activity,"status "+p1.statusMessage,Toast.LENGTH_LONG).show()
}
})
val _autocompletFragment2 = supportFragmentManager
.findFragmentById(R.id.fragmentPlaces2) as AutocompleteSupportFragment
_autocompletFragment2.setPlaceFields(_placesFields)
_autocompletFragment2.setOnPlaceSelectedListener(object :PlaceSelectionListener{
override fun onPlaceSelected(p2: Place) {
_adress2 =p2.latLng!!
_adressString2 = p2.address!!
}
override fun onError(p2: Status) {
Toast.makeText(this#GooglePlaces_Activity,"status "+p2.statusMessage,Toast.LENGTH_LONG).show()
}
})
}
private fun InitPlacesFun() {
Places.initialize(this,getString(R.string.Places_api))
_placcesClint = Places.createClient(this)
}
fun TestButtonFun () {
button.setOnClickListener() {
var _loc:Location= Location("start")
_loc.latitude = _adrees1.latitude
_loc.longitude = _adrees1.longitude
var _loc2:Location= Location("start")
_loc2.latitude = _adress2.latitude
_loc2.longitude = _adress2.longitude
dist = (round(((_loc.distanceTo(_loc2).toDouble()/1000))*100) /100).toDouble()
}
I have also tried this:
var _Lat1 = _adrees1.latitude
var _Lat2 = _adress2.latitude
var _Long1 = _adrees1.longitude
var _Long2 = _adress2.longitude
var _LatRes = _Lat1 - _Lat2
var _longRes = _Long1 - _Long2
var R = 6371000f; // Radius of the earth in m
var dLat = _LatRes * Math.PI / 180f;
var dLon = _longRes * Math.PI / 180f;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(_Lat1 * Math.PI / 180f) * Math.cos(_Lat2 * Math.PI / 180f) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2f * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
dist = R* c
You get less distance because you're calculating the radial distance and not the actual route distance.
Try calculating the distance using google directions API.
You can see the difference between radial distance (orange) and actual distance (blue) in the image