Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.82 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2019 Razeware LLC
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
  15. * distribute, sublicense, create a derivative work, and/or sell copies of the
  16. * Software in any work that is designed, intended, or marketed for pedagogical or
  17. * instructional purposes related to programming, coding, application development,
  18. * or information technology. Permission for such use, copying, modification,
  19. * merger, publication, distribution, sublicensing, creation of derivative works,
  20. * or sale is expressly withheld.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. * THE SOFTWARE.
  29. */
  30.  
  31. package com.raywenderlich.where2go
  32.  
  33. import android.annotation.SuppressLint
  34. import android.app.Activity
  35. import android.app.Application
  36. import android.content.Intent
  37. import android.location.Location
  38. import android.os.Bundle
  39. import android.os.Environment
  40. import android.preference.PreferenceManager
  41. import android.support.v7.app.AppCompatActivity
  42. import android.util.Log
  43. import android.widget.Toast
  44. import com.google.android.gms.common.api.ApiException
  45. import com.google.android.gms.common.api.ResolvableApiException
  46. import com.google.android.gms.location.*
  47. import com.mapbox.android.core.location.LocationEngine
  48. import com.mapbox.android.core.location.LocationEngineListener
  49. import com.mapbox.android.core.location.LocationEnginePriority
  50. import com.mapbox.android.core.location.LocationEngineProvider
  51. import com.mapbox.android.core.permissions.PermissionsListener
  52. import com.mapbox.android.core.permissions.PermissionsManager
  53. import com.mapbox.api.directions.v5.DirectionsCriteria
  54. import com.mapbox.api.directions.v5.models.DirectionsResponse
  55. import com.mapbox.api.directions.v5.models.DirectionsRoute
  56. import com.mapbox.api.matching.v5.MapboxMapMatching
  57. import com.mapbox.api.matching.v5.models.MapMatchingResponse
  58. import com.mapbox.geojson.Point
  59. import com.mapbox.mapboxsdk.Mapbox
  60. import com.mapbox.mapboxsdk.annotations.MarkerOptions
  61. import com.mapbox.mapboxsdk.camera.CameraUpdateFactory
  62. import com.mapbox.mapboxsdk.geometry.LatLng
  63. import com.mapbox.mapboxsdk.location.LocationComponent
  64. import com.mapbox.mapboxsdk.location.modes.CameraMode
  65. import com.mapbox.mapboxsdk.maps.MapboxMap
  66. import com.mapbox.mapboxsdk.maps.OnMapReadyCallback
  67. import com.mapbox.mapboxsdk.style.light.Position
  68. import com.mapbox.services.android.navigation.ui.v5.NavigationLauncher
  69. import com.mapbox.services.android.navigation.ui.v5.NavigationLauncherOptions
  70. import com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute
  71. import com.mapbox.services.android.navigation.v5.navigation.*
  72. import kotlinx.android.synthetic.main.activity_main.*
  73. import retrofit2.Call
  74. import retrofit2.Callback
  75. import retrofit2.Response
  76. import timber.log.Timber
  77. import java.io.File
  78. import java.io.IOException
  79. import java.io.InputStream
  80. import java.nio.charset.Charset
  81.  
  82. class MainActivity : AppCompatActivity(),
  83. PermissionsListener, LocationEngineListener, OnMapReadyCallback, MapboxMap.OnMapClickListener {
  84.  
  85. //1
  86. val REQUEST_CHECK_SETTINGS = 1
  87. var settingsClient: SettingsClient? = null
  88. private val TEST_GPX = "parcourstestguidos.gpx"
  89. private val TILE_PATH_NAME = "2018_11_18-22_33_58"
  90. var flag = true
  91. var origino: Point? = null
  92. var druga: Point? = null
  93. var numo = 0
  94. //2
  95. lateinit var map: MapboxMap
  96. lateinit var permissionManager: PermissionsManager
  97. var originLocation: Location? = null
  98.  
  99. var locationEngine: LocationEngine? = null
  100. var locationComponent: LocationComponent? = null
  101.  
  102. var navigationMapRoute: NavigationMapRoute? = null
  103. var currentRoute: DirectionsRoute? = null
  104. var currentRoute2: DirectionsRoute? = null
  105.  
  106. private val mapboxOfflineRouter: MapboxOfflineRouter
  107. get() {
  108. return MapboxOfflineRouter(obtainOfflineDirectory())
  109. }
  110.  
  111. private fun obtainOfflineDirectory(): String {
  112. val offline = Environment.getExternalStoragePublicDirectory("Offline")
  113. if (!offline.exists()) {
  114. Timber.d("Offline directory does not exist")
  115. offline.mkdirs()
  116. }
  117. return offline.absolutePath
  118. }
  119.  
  120. override fun onCreate(savedInstanceState: Bundle?) {
  121. super.onCreate(savedInstanceState)
  122. Mapbox.getInstance(this, "pk.eyJ1IjoibWFuZXYxNSIsImEiOiJjaWgwazg1a2Mwd2txdnFtM3p1dnNvbjJkIn0.fvNagTH12eCx2IwmoDdgRw")
  123. setContentView(R.layout.activity_main)
  124. mapbox.onCreate(savedInstanceState)
  125. mapbox.getMapAsync(this)
  126. settingsClient = LocationServices.getSettingsClient(this)
  127. btnNavigate.isEnabled = true
  128.  
  129. btnNavigate.setOnClickListener {
  130. // val intent = Intent(this, OfflineRegionDownloadActivity::class.java)
  131. // startActivity(intent)
  132. val intent = Intent(this, EndNavigationActivity::class.java)
  133. startActivity(intent)
  134. // val navigationLauncherOptions = NavigationLauncherOptions.builder() //1
  135. // .directionsRoute(currentRoute) //2
  136. // .shouldSimulateRoute(true) //3
  137. // .build()
  138. //
  139. // NavigationLauncher.startNavigation(this, navigationLauncherOptions) //4
  140. }
  141. }
  142.  
  143. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  144. super.onActivityResult(requestCode, resultCode, data)
  145.  
  146. if (requestCode == REQUEST_CHECK_SETTINGS) {
  147. if (resultCode == Activity.RESULT_OK) {
  148. enableLocation()
  149. } else
  150. if (resultCode == Activity.RESULT_CANCELED) {
  151. finish()
  152. }
  153. }
  154. }
  155.  
  156. @SuppressWarnings("MissingPermission")
  157. override fun onStart() {
  158. super.onStart()
  159. if (PermissionsManager.areLocationPermissionsGranted(this)) {
  160. locationEngine?.requestLocationUpdates()
  161. locationComponent?.onStart()
  162. }
  163.  
  164. mapbox.onStart()
  165. }
  166.  
  167. override fun onResume() {
  168. super.onResume()
  169. mapbox.onResume()
  170. }
  171.  
  172. override fun onPause() {
  173. super.onPause()
  174. mapbox.onPause()
  175. }
  176.  
  177. override fun onStop() {
  178. super.onStop()
  179. locationEngine?.removeLocationUpdates()
  180. locationComponent?.onStop()
  181. mapbox.onStop()
  182. }
  183.  
  184. override fun onDestroy() {
  185. super.onDestroy()
  186. locationEngine?.deactivate()
  187. mapbox.onDestroy()
  188. }
  189.  
  190. override fun onLowMemory() {
  191. super.onLowMemory()
  192. mapbox.onLowMemory()
  193. }
  194.  
  195. override fun onSaveInstanceState(outState: Bundle?) {
  196. super.onSaveInstanceState(outState)
  197. if (outState != null) {
  198. mapbox.onSaveInstanceState(outState)
  199. }
  200. }
  201.  
  202. override fun onExplanationNeeded(permissionsToExplain: MutableList<String>?) {
  203. Toast.makeText(this, "This app needs location permission to be able to show your location on the map", Toast.LENGTH_LONG).show()
  204. }
  205.  
  206. override fun onPermissionResult(granted: Boolean) {
  207. if (granted) {
  208. enableLocation()
  209. } else {
  210. Toast.makeText(this, "User location was not granted", Toast.LENGTH_LONG).show()
  211. finish()
  212. }
  213. }
  214.  
  215. override fun onLocationChanged(location: Location?) {
  216. location?.run {
  217. originLocation = this
  218. setCameraPosition(this)
  219. }
  220. }
  221.  
  222. override fun onConnected() {
  223. }
  224.  
  225. override fun onMapReady(mapboxMap: MapboxMap?) {
  226. //1
  227. map = mapboxMap ?: return
  228. //2
  229. val locationRequestBuilder = LocationSettingsRequest.Builder().addLocationRequest(LocationRequest()
  230. .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
  231. )
  232. //3
  233. val locationRequest = locationRequestBuilder?.build()
  234.  
  235. settingsClient?.checkLocationSettings(locationRequest)?.run {
  236. addOnSuccessListener {
  237. enableLocation()
  238. }
  239.  
  240. addOnFailureListener {
  241. val statusCode = (it as ApiException).statusCode
  242.  
  243. if (statusCode == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
  244. val resolvableException = it as? ResolvableApiException
  245. resolvableException?.startResolutionForResult(this@MainActivity, REQUEST_CHECK_SETTINGS)
  246. }
  247. }
  248. }
  249. }
  250.  
  251. //1
  252. fun enableLocation() {
  253. if (PermissionsManager.areLocationPermissionsGranted(this)) {
  254. initializeLocationComponent()
  255. initializeLocationEngine()
  256. map.addOnMapClickListener(this)
  257. initlizeRoute()
  258. } else {
  259. permissionManager = PermissionsManager(this)
  260. permissionManager.requestLocationPermissions(this)
  261. }
  262. }
  263.  
  264. fun initlizeRoute() {
  265. val mapMatchedPoints = mutableListOf<Point>()
  266. mapMatchedPoints.add(Point.fromLngLat(21.4384477, 41.9844241))
  267. mapMatchedPoints.add(Point.fromLngLat(21.438205, 41.9848805))
  268. mapMatchedPoints.add(Point.fromLngLat(21.4377607, 41.9857103))
  269. mapMatchedPoints.add(Point.fromLngLat(21.438912, 41.9864523))
  270. // mapMatchedPoints.add(Point.fromLngLat(21.4372307, 41.9871085))
  271. // mapMatchedPoints.add(Point.fromLngLat(21.436772, 41.9878366))
  272. // mapMatchedPoints.add(Point.fromLngLat(21.4368103, 41.9879339))
  273. // mapMatchedPoints.add(Point.fromLngLat(21.4368747, 41.987948))
  274. // mapMatchedPoints.add(Point.fromLngLat(21.4371215, 41.9879574))
  275. // mapMatchedPoints.add(Point.fromLngLat(21.4380211, 41.9886061))
  276. // mapMatchedPoints.add(Point.fromLngLat(21.4380211,41.9886061))
  277. mapMatchedPoints.add(Point.fromLngLat(21.4356674, 41.990109))
  278. mapMatchedPoints.add(Point.fromLngLat(21.4354717, 41.9905303))
  279. // mapMatchedPoints.add(Point.fromLngLat(21.4470612, 41.9848251))
  280. // mapMatchedPoints.add(Point.fromLngLat(21.4544094, 41.9806309))
  281.  
  282.  
  283. map.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(21.438912, 41.9864523), 50.0))
  284.  
  285. MapboxMapMatching.builder()
  286. .accessToken("pk.eyJ1IjoibWFuZXYxNSIsImEiOiJjaWgwazg1a2Mwd2txdnFtM3p1dnNvbjJkIn0.fvNagTH12eCx2IwmoDdgRw")
  287. .coordinates(mapMatchedPoints)
  288. // .waypoints(0, mapMatchedPoints.size - 1)
  289. .steps(true)
  290. .voiceInstructions(true)
  291. .voiceUnits(DirectionsCriteria.METRIC)
  292. .bannerInstructions(true)
  293. .profile(DirectionsCriteria.PROFILE_CYCLING)
  294. .build()
  295. .enqueueCall(object : Callback<MapMatchingResponse> {
  296.  
  297. override fun onResponse(call: Call<MapMatchingResponse>, response: Response<MapMatchingResponse>) {
  298. // if (response.isSuccessful) {
  299. if (navigationMapRoute != null) {
  300. navigationMapRoute?.updateRouteVisibilityTo(false)
  301. } else {
  302. navigationMapRoute = NavigationMapRoute(null, mapbox, map)
  303. }
  304. currentRoute = response.body()!!.matchings()!![0].toDirectionRoute()
  305. Engine.route = currentRoute
  306.  
  307. if (currentRoute != null) {
  308. navigationMapRoute?.addRoute(currentRoute)
  309. }
  310.  
  311. btnNavigate.isEnabled = true
  312.  
  313. // }
  314. }
  315.  
  316. override fun onFailure(call: Call<MapMatchingResponse>, throwable: Throwable) {
  317. }
  318. })
  319.  
  320.  
  321. }
  322.  
  323. //2
  324. @SuppressWarnings("MissingPermission")
  325. fun initializeLocationEngine() {
  326. locationEngine = LocationEngineProvider(this).obtainBestLocationEngineAvailable()
  327. locationEngine?.priority = LocationEnginePriority.HIGH_ACCURACY
  328. locationEngine?.activate()
  329. locationEngine?.addLocationEngineListener(this)
  330.  
  331. val lastLocation = locationEngine?.lastLocation
  332. if (lastLocation != null) {
  333. originLocation = lastLocation
  334. // setCameraPosition(lastLocation)
  335. } else {
  336. locationEngine?.addLocationEngineListener(this)
  337. }
  338. }
  339.  
  340. @SuppressWarnings("MissingPermission")
  341. fun initializeLocationComponent() {
  342. locationComponent = map.locationComponent
  343. locationComponent?.activateLocationComponent(this)
  344. locationComponent?.isLocationComponentEnabled = true
  345. locationComponent?.cameraMode = CameraMode.TRACKING
  346. }
  347.  
  348. //3
  349. fun setCameraPosition(location: Location) {
  350. map.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(location.latitude,
  351. location.longitude), 30.0))
  352. }
  353.  
  354. override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
  355. super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  356.  
  357. permissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults)
  358. }
  359.  
  360. override fun onMapClick(point: LatLng) {
  361. if (!map.markers.isEmpty()) {
  362. map.clear()
  363. }
  364.  
  365. map.addMarker(MarkerOptions().setTitle("I'm a marker :]").setSnippet("This is a snippet about this marker that will show up here").position(point))
  366.  
  367. checkLocation()
  368. originLocation?.run {
  369. val startPoint = Point.fromLngLat(longitude, latitude)
  370. val endPoint = Point.fromLngLat(point.longitude, point.latitude)
  371.  
  372. getRoute(startPoint, endPoint)
  373. }
  374. }
  375.  
  376. @SuppressLint("MissingPermission")
  377. private fun checkLocation() {
  378. if (originLocation == null) {
  379. map.locationComponent.lastKnownLocation?.run {
  380. originLocation = this
  381. }
  382. }
  383. }
  384.  
  385. private fun ace() {
  386. val parser = GpxParser()
  387. val `inn` = assets.open(TEST_GPX)
  388. val testRoute = parser.parseGpx(inn)
  389. var coordinates: List<Point>? = null
  390. if (testRoute != null) {
  391. for (item in testRoute) {
  392.  
  393. }
  394. }
  395.  
  396.  
  397. }
  398.  
  399. private fun getRoute(originPoint: Point, endPoint: Point) {
  400.  
  401. val parser = GpxParser()
  402. val `inn` = assets.open(TEST_GPX)
  403. val testRoute = parser.parseGpx(inn)
  404.  
  405.  
  406. val mapMatchedPoints = mutableListOf<Point>()
  407. var num = 0
  408. var num2 = 0
  409. val num1 = testRoute?.size
  410. var br = (num1!! - 3) / 100
  411. br += 1
  412. if (testRoute != null) {
  413. for (item in testRoute) {
  414. num++
  415. if (num2 == 0) {
  416. mapMatchedPoints.add(Point.fromLngLat(item.longitude, item.latitude))
  417. }
  418. if (num == br) {
  419. num = 0
  420. num2++
  421. if (num2 < 100)
  422. mapMatchedPoints.add(Point.fromLngLat(item.longitude, item.latitude))
  423. }
  424. }
  425. mapMatchedPoints.add(Point.fromLngLat(testRoute[testRoute.size - 1].longitude, testRoute[testRoute.size - 1].latitude))
  426. }
  427.  
  428. // for (i in 0 until mapMatchedPoints.size) {
  429. // if (i + 1 < mapMatchedPoints.size) {
  430. // NavigationRoute.builder(this) //1
  431. // .accessToken(Mapbox.getAccessToken()!!) //2
  432. // .origin(Point.fromLngLat(mapMatchedPoints[i].longitude(), mapMatchedPoints[i].latitude())) //3
  433. // .voiceUnits(DirectionsCriteria.METRIC)
  434. // .profile(DirectionsCriteria.PROFILE_CYCLING)
  435. // .destination(Point.fromLngLat(mapMatchedPoints[i + 1].longitude(), mapMatchedPoints[i + 1].latitude())) //4
  436. // .build() //5
  437. // .getRoute(object : Callback<DirectionsResponse> { //6
  438. // override fun onFailure(call: Call<DirectionsResponse>, t: Throwable) {
  439. // Log.d("MainActivity", t.localizedMessage)
  440. // }
  441. //
  442. // override fun onResponse(call: Call<DirectionsResponse>,
  443. // response: Response<DirectionsResponse>) {
  444. // if (navigationMapRoute != null) {
  445. // navigationMapRoute?.updateRouteVisibilityTo(false)
  446. // } else {
  447. // navigationMapRoute = NavigationMapRoute(null, mapbox, map)
  448. // }
  449. // currentRoute = response.body()!!.routes().first()
  450. // druga = endPoint
  451. // numo++
  452. // if (flag) {
  453. // flag = false
  454. // currentRoute2 = currentRoute
  455. // // currentRoute2?.legs()!!.removeAt(0)
  456. // } else {
  457. //
  458. // currentRoute2!!.routeOptions()!!.coordinates() += currentRoute!!.routeOptions()!!.coordinates()[1]
  459. //// currentRoute2?.duration()!!.plus(currentRoute?.duration()!!.toDouble())
  460. //// currentRoute2?.distance()!!.plus(currentRoute?.distance()!!.toDouble())
  461. // // currentRoute2?.legs()!!.addAll(currentRoute?.legs()!!.toMutableList())
  462. // // currentRoute2?.legs()!![0].steps()!!.addAll(currentRoute?.legs()!![0].steps()!!)
  463. // //
  464. // // currentRoute2?.legs()!!.addAll(currentRoute?.legs()!!.toMutableList())
  465. // // currentRoute2?.geometry()!!.contentEquals(currentRoute2?.geometry()!!.replace(currentRoute2?.geometry()!!, "~"+currentRoute?.geometry()!!))
  466. // currentRoute2?.legs()!!.addAll(currentRoute?.legs()!!.toMutableList())
  467. //// navigationMapRoute?.addRoute(currentRoute2)
  468. // }
  469. // if (currentRoute != null) {
  470. // if (numo == mapMatchedPoints.size-1) {
  471. // // currentRoute2?.legs()!!.addAll(currentRoute?.legs()!!.toMutableList())
  472. // navigationMapRoute?.addRoute(currentRoute2)
  473. // }
  474. // }
  475. // btnNavigate.isEnabled = true
  476. // }
  477. // })
  478. // }
  479. // }
  480.  
  481. MapboxMapMatching.builder()
  482. .accessToken("pk.eyJ1IjoibWFuZXYxNSIsImEiOiJjaWgwazg1a2Mwd2txdnFtM3p1dnNvbjJkIn0.fvNagTH12eCx2IwmoDdgRw")
  483. .coordinates(mapMatchedPoints)
  484. // .waypoints(0, mapMatchedPoints.size - 1)
  485. .steps(true)
  486. .voiceInstructions(true)
  487. .bannerInstructions(true)
  488. .voiceUnits(DirectionsCriteria.METRIC)
  489. .profile(DirectionsCriteria.PROFILE_CYCLING)
  490. .build()
  491. .enqueueCall(object : Callback<MapMatchingResponse> {
  492.  
  493. override fun onResponse(call: Call<MapMatchingResponse>, response: Response<MapMatchingResponse>) {
  494. // if (response.isSuccessful) {
  495. if (navigationMapRoute != null) {
  496. navigationMapRoute?.updateRouteVisibilityTo(false)
  497. } else {
  498. navigationMapRoute = NavigationMapRoute(null, mapbox, map)
  499. }
  500. currentRoute = response.body()!!.matchings()!![0].toDirectionRoute()
  501. Engine.route = currentRoute
  502.  
  503.  
  504. if (currentRoute != null) {
  505. navigationMapRoute?.addRoute(currentRoute)
  506. }
  507.  
  508. btnNavigate.isEnabled = true
  509.  
  510. // }
  511. }
  512.  
  513. override fun onFailure(call: Call<MapMatchingResponse>, throwable: Throwable) {
  514. }
  515. })
  516.  
  517.  
  518. // val parser = GpxParser()
  519. // val `inn` = assets.open(TEST_GPX)
  520. // val testRoute = parser.parseGpx(inn)
  521. // if (flag) {
  522. // origino = originPoint
  523. // } else {
  524. // origino = druga
  525. // }
  526. // NavigationRoute.builder(this) //1
  527. // .accessToken(Mapbox.getAccessToken()!!) //2
  528. // .origin(origino!!) //3
  529. // .voiceUnits(DirectionsCriteria.METRIC)
  530. // .profile(DirectionsCriteria.PROFILE_CYCLING)
  531. // .destination(endPoint) //4
  532. // .build() //5
  533. // .getRoute(object : Callback<DirectionsResponse> { //6
  534. // override fun onFailure(call: Call<DirectionsResponse>, t: Throwable) {
  535. // Log.d("MainActivity", t.localizedMessage)
  536. // }
  537. //
  538. // override fun onResponse(call: Call<DirectionsResponse>,
  539. // response: Response<DirectionsResponse>) {
  540. // if (navigationMapRoute != null) {
  541. // navigationMapRoute?.updateRouteVisibilityTo(false)
  542. // } else {
  543. // navigationMapRoute = NavigationMapRoute(null, mapbox, map)
  544. // }
  545. // currentRoute = response.body()!!.routes().first()
  546. // druga = endPoint
  547. // numo++
  548. // if (flag) {
  549. // flag = false
  550. // currentRoute2 = currentRoute
  551. // // currentRoute2?.legs()!!.removeAt(0)
  552. // } else {
  553. //
  554. // currentRoute2!!.routeOptions()!!.coordinates() += currentRoute!!.routeOptions()!!.coordinates()[1]
  555. //// currentRoute2?.duration()!!.plus(currentRoute?.duration()!!.toDouble())
  556. //// currentRoute2?.distance()!!.plus(currentRoute?.distance()!!.toDouble())
  557. // // currentRoute2?.legs()!!.addAll(currentRoute?.legs()!!.toMutableList())
  558. // // currentRoute2?.legs()!![0].steps()!!.addAll(currentRoute?.legs()!![0].steps()!!)
  559. // //
  560. // // currentRoute2?.legs()!!.addAll(currentRoute?.legs()!!.toMutableList())
  561. // currentRoute2?.geometry()!!.contentEquals(currentRoute2?.geometry()!!.replace(currentRoute2?.geometry()!!, currentRoute?.geometry()!!))
  562. // // currentRoute2?.legs()!!.addAll(currentRoute?.legs()!!.toMutableList())
  563. // }
  564. // if (currentRoute != null) {
  565. // if (numo == 20) {
  566. // // currentRoute2?.legs()!![0].steps()!![1] = currentRoute?.legs()!![0].steps()!![1]
  567. //// .addAll(currentRoute?.legs()!!.toMutableList())
  568. // // currentRoute2?.legs()!!.removeAt(0)
  569. //// currentRoute2?.legs()!!.removeAt(0)
  570. // // currentRoute2?.legs()!![0].steps()!!.removeAt(0)
  571. //// currentRoute2?.legs()!!.addAll(currentRoute?.legs()!!.toMutableList())
  572. // // currentRoute2?.legs()!![1].steps()!!.removeAt(0)
  573. //// currentRoute2?.legs()!!.removeAt(0)
  574. // currentRoute2?.legs()!!.addAll(currentRoute?.legs()!!.toMutableList())
  575. // navigationMapRoute?.addRoute(currentRoute2)
  576. // Engine.route = currentRoute2
  577. // }
  578. // }
  579. // btnNavigate.isEnabled = true
  580. // }
  581. // })
  582.  
  583. // val tileDir = File(obtainOfflineDirectory(), TILE_PATH_NAME)
  584. // if (!tileDir.exists()) {
  585. // tileDir.mkdirs()
  586. // }
  587. // val offlineRouter = MapboxOfflineRouter(tileDir.absolutePath)
  588. // offlineRouter.configure(retrieveOfflineVersionFromPreferences(), object : OnOfflineTilesConfiguredCallback {
  589. //
  590. // override fun onConfigured(numberOfTiles: Int) {
  591. // Timber.d("Offline tiles configured: $numberOfTiles")
  592. //// Fetch offline route
  593. //
  594. // val origin = Point.fromLngLat(-6.782582, 61.833349)
  595. // // val bearing = location.bearing.toDouble()
  596. // val onlineRouteBuilder = NavigationRoute.builder(applicationContext)
  597. // .origin(origin)
  598. // .destination(Point.fromLngLat(-6.729809, 61.826144))
  599. // .accessToken("pk.eyJ1IjoibWFuZXYxNSIsImEiOiJjaWgwazg1a2Mwd2txdnFtM3p1dnNvbjJkIn0.fvNagTH12eCx2IwmoDdgRw")
  600. // val offlineRoute = OfflineRoute.builder(onlineRouteBuilder).build()
  601. // mapboxOfflineRouter.findRoute(offlineRoute, object : OnOfflineRouteFoundCallback {
  602. // override fun onRouteFound(route: DirectionsRoute) {
  603. // if (navigationMapRoute != null) {
  604. // navigationMapRoute?.updateRouteVisibilityTo(false)
  605. // } else {
  606. // navigationMapRoute = NavigationMapRoute(null, mapbox, map)
  607. // }
  608. // currentRoute = route
  609. //
  610. //
  611. // if (currentRoute != null) {
  612. // navigationMapRoute?.addRoute(currentRoute)
  613. // }
  614. //
  615. // btnNavigate.isEnabled = true
  616. // }
  617. //
  618. // override fun onError(error: OfflineError) {
  619. //// Handle route error
  620. // Timber.d("Offline tiles configuration error: {${error.message}}")
  621. // }
  622. // })
  623. // }
  624. //
  625. // override fun onConfigurationError(error: OfflineError) {
  626. // Timber.d("Offline tiles configuration error: {${error.message}}")
  627. // }
  628. // })
  629.  
  630.  
  631. }
  632.  
  633.  
  634. // private fun findOfflineRoute(location: Location, destination: Point) {
  635. // offlineRouteFinder.findRoute(location, destination)
  636. // }
  637. private fun retrieveOfflineVersionFromPreferences(): String {
  638. // val context = getApplication<Application>()
  639. return PreferenceManager.getDefaultSharedPreferences(applicationContext)
  640. .getString(applicationContext.getString(R.string.offline_version_key), "")
  641. }
  642.  
  643. fun aaa() {
  644. val parser = GpxParser()
  645. val inputStream = buildTestGpxInputStream(TEST_GPX)
  646.  
  647. val parsedLocations = parser.parseGpx(inputStream)
  648. }
  649.  
  650. private fun buildTestGpxInputStream(gpxFileName: String): InputStream {
  651. val classLoader = javaClass.classLoader
  652. return classLoader.getResourceAsStream(gpxFileName)
  653. }
  654.  
  655. fun loadJsonFromAsset(filename: String): String? {
  656. try {
  657. val `is` = assets.open(filename)
  658. val size = `is`.available()
  659. val buffer = ByteArray(size)
  660. `is`.read(buffer)
  661. `is`.close()
  662. return String(buffer, Charset.forName("UTF-8"))
  663.  
  664. } catch (ex: IOException) {
  665. ex.printStackTrace()
  666. return null
  667. }
  668.  
  669. }
  670. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement