Guest User

Untitled

a guest
Apr 12th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.73 KB | None | 0 0
  1. package com.schafer.tracker
  2.  
  3. import com.beust.klaxon.Klaxon
  4. import java.net.URL
  5. import kotlin.math.abs
  6.  
  7. class airplaneTracker() {
  8.     init {
  9.         val apiResponse = URL("https://opensky-network.org/api/states/all").readText()
  10.         val data = Klaxon()
  11.             .parse<data>(
  12.                 apiResponse
  13.             )
  14.         val jsonData = listOf(data!!.states)
  15.         var lowDist = 0.0
  16.         for (x in jsonData) {
  17.             if (x[2] == "United States") {
  18.                 val lat = x[5].toString().toDouble()
  19.                 val long = x[6].toString().toDouble()
  20.                 val R = 6371 // Radius of the earth
  21.  
  22.                 val latDistance = Math.toRadians(lat -  41.661129)
  23.                 val lonDistance = Math.toRadians(long -  -91.530167)
  24.                 val a =
  25.                     Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + (Math.cos(Math.toRadians(41.661129)) * Math.cos(
  26.                         Math.toRadians(long)
  27.                     )
  28.                             * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2))
  29.                 val c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
  30.                 var distance = R.toDouble() * c * 1000.0 // convert to meters
  31.  
  32.                 val height = abs(204 - x[7].toString().toInt()).toDouble()
  33.  
  34.                 distance = Math.pow(distance, 2.0) + Math.pow(height, 2.0)
  35.  
  36.                 distance = Math.sqrt(distance)
  37.  
  38.                 if (distance <= lowDist || lowDist == 0.0) {
  39.                     lowDist = distance
  40.                 }
  41.             }
  42.         }
  43.         println(lowDist)
  44.     }
  45.  
  46.  
  47.  
  48.     companion object {
  49.         @JvmStatic public fun main(args: Array<String>) {
  50.             airplaneTracker()
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment