Advertisement
Guest User

Code

a guest
Mar 23rd, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 8.40 KB | None | 0 0
  1. package view.view
  2.  
  3. import android.Manifest
  4. import android.annotation.SuppressLint
  5. import android.content.Intent
  6. import android.content.pm.PackageManager
  7. import android.location.Location
  8. import android.location.LocationManager
  9. import androidx.appcompat.app.AppCompatActivity
  10. import android.os.Bundle
  11. import android.provider.Settings
  12. import android.view.Menu
  13. import android.widget.Toast
  14. import androidx.core.app.ActivityCompat
  15. import com.firstapp.maptest.R
  16. import com.firstapp.maptest.databinding.ActivityMainBinding
  17. import com.google.android.gms.location.FusedLocationProviderClient
  18. import com.google.android.gms.location.LocationCallback
  19. import com.google.android.gms.location.LocationResult
  20. import com.google.android.gms.location.LocationServices
  21. import com.mapbox.geojson.FeatureCollection
  22. import com.mapbox.geojson.Point
  23. import com.mapbox.geojson.Polygon
  24. import com.mapbox.maps.CameraBoundsOptions
  25.  
  26. import com.mapbox.maps.CoordinateBounds
  27. import com.mapbox.maps.MapView
  28.  
  29. import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource
  30. import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
  31. import com.mapbox.maps.extension.style.sources.getSource
  32. import com.mapbox.maps.extension.style.style
  33. import com.mapbox.maps.plugin.annotation.annotations
  34. import com.mapbox.maps.plugin.annotation.generated.CircleAnnotationOptions
  35. import com.mapbox.maps.plugin.annotation.generated.createCircleAnnotationManager
  36. import com.mapbox.maps.plugin.attribution.attribution
  37. import com.mapbox.maps.plugin.compass.compass
  38. import com.mapbox.maps.plugin.gestures.gestures
  39. import com.mapbox.maps.plugin.logo.logo
  40. import com.mapbox.maps.plugin.scalebar.scalebar
  41. import kotlin.math.cos
  42.  
  43.  
  44. class MainActivity : AppCompatActivity() {
  45.     private lateinit var mapboxMap: MapView
  46.     private lateinit var binding: ActivityMainBinding
  47.     private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
  48.  
  49.   // private val annotationApi = mapboxMap.annotations
  50.    // private val circleAnnotationManager = annotationApi.createCircleAnnotationManager()
  51.  
  52.  
  53.     //private var latt: Double = 0.0
  54.   //  private var longg: Double = 0.0
  55.     override fun onCreate(savedInstanceState: Bundle?) {
  56.         super.onCreate(savedInstanceState)
  57.  
  58. // Add the resulting circle to the map.
  59.         //circleAnnotationManager.create(circleAnnotationOptions)
  60.         fusedLocationProviderClient= LocationServices.getFusedLocationProviderClient(this)
  61.         getCurrentLocation()
  62.  
  63.         binding = ActivityMainBinding.inflate(layoutInflater)
  64.         mapboxMap = binding.mapView
  65.         mapboxMap.scalebar.updateSettings { enabled = false }
  66.         mapboxMap.logo.updateSettings { enabled = false }
  67.         mapboxMap.compass.updateSettings { enabled = false }
  68.         mapboxMap.attribution.updateSettings { enabled = false }
  69.         mapboxMap.gestures.rotateEnabled = false
  70.         mapboxMap.gestures.pitchEnabled = false
  71.         setContentView(binding.root)
  72.  
  73.         binding.mapView.getMapboxMap().loadStyle(
  74.             styleExtension = style(getString(R.string.mapbox_styleURL)) {
  75.                 +geoJsonSource(BOUNDS_ID) {
  76.                     featureCollection(FeatureCollection.fromFeatures(listOf()))
  77.                 }
  78.             }
  79.         ){ setupBounds(USER_BOUNDS) }
  80.  
  81.  
  82.  
  83.  
  84.     }
  85.  
  86.  
  87. //getting location
  88.     private fun getCurrentLocation() {
  89.         if(checkPermissions()){
  90.             if(isLocationEnabled()){
  91.                 if (ActivityCompat.checkSelfPermission(
  92.                         this,
  93.                         Manifest.permission.ACCESS_FINE_LOCATION
  94.                     ) != PackageManager.PERMISSION_GRANTED
  95.                 ) {
  96.                     requestPermissions()
  97.                     return
  98.                 }
  99.                 fusedLocationProviderClient.lastLocation.addOnCompleteListener(this){
  100.                     task -> val location: Location?=task.result
  101.                     if(location == null){
  102.                         Toast.makeText(this, "Unknown exception", Toast.LENGTH_SHORT).show()
  103.                     }
  104.                     else{
  105.                         Toast.makeText(this, "Great Success", Toast.LENGTH_SHORT).show()
  106.  
  107.  
  108.  
  109.  
  110.  
  111.                     }
  112.                 }
  113.             }
  114.             else{
  115.                 Toast.makeText(this, "Turn on your location", Toast.LENGTH_SHORT).show()
  116.                 val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
  117.                 startActivity(intent)
  118.             }
  119.         }
  120.         else{
  121.             requestPermissions()
  122.         }
  123.  
  124.  
  125.     }
  126.  
  127.     private fun isLocationEnabled(): Boolean {
  128.             val locationManager:LocationManager = getSystemService(LOCATION_SERVICE) as LocationManager
  129.             return  locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
  130.     }
  131.  
  132.     private fun requestPermissions() {
  133.        ActivityCompat.requestPermissions(
  134.            this, arrayOf( Manifest.permission.ACCESS_FINE_LOCATION),  PERMISSION_REQUEST_ACCESS_LOCATION
  135.        )
  136.  
  137.     }
  138.  
  139.     private fun checkPermissions():Boolean{
  140.         //read more on this what ia acitivty compat
  141.         return  ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED
  142.     }
  143.  
  144.     @SuppressLint("SuspiciousIndentation")
  145.     override fun onRequestPermissionsResult(
  146.         requestCode: Int,
  147.         permissions: Array<out String>,
  148.         grantResults: IntArray
  149.     ) {
  150.         super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  151.  
  152.         if(requestCode == PERMISSION_REQUEST_ACCESS_LOCATION){
  153.                 if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
  154.                     Toast.makeText(applicationContext,"Granted Location", Toast.LENGTH_LONG).show()
  155.                     getCurrentLocation()
  156.             }
  157.             else{
  158.             Toast.makeText(applicationContext,"Denied Location", Toast.LENGTH_LONG).show()
  159.         }
  160.     }
  161.     //-------------------------------------------------------------
  162.  
  163.  
  164.     //bounds
  165.     override fun onCreateOptionsMenu(menu: Menu): Boolean {
  166.         menuInflater.inflate(R.menu.menu_bounds, menu)
  167.         return true
  168.     }
  169.  
  170.     private fun setupBounds(bounds: CameraBoundsOptions) {
  171.         mapboxMap.getMapboxMap().setBounds(bounds)
  172.         showBoundsArea(bounds)
  173.     }
  174.  
  175.     private fun showBoundsArea(boundsOptions: CameraBoundsOptions) {
  176.         val source = mapboxMap.getMapboxMap().getStyle()!!.getSource(BOUNDS_ID) as GeoJsonSource
  177.         val bounds = boundsOptions.bounds
  178.         val list = mutableListOf<List<Point>>()
  179.         bounds?.let {
  180.             if (!it.infiniteBounds) {
  181.                 val northEast = it.northeast
  182.                 val southWest = it.southwest
  183.                 val northWest = Point.fromLngLat(southWest.longitude(), northEast.latitude())
  184.                 val southEast = Point.fromLngLat(northEast.longitude(), southWest.latitude())
  185.                 list.add(
  186.                     mutableListOf(northEast, southEast, southWest, northWest, northEast)
  187.                 )
  188.             }
  189.         }
  190.         if (list.isNotEmpty()) {
  191.             source.geometry(
  192.                 Polygon.fromLngLats(
  193.                     list
  194.                 )
  195.             )
  196.         }
  197.     }
  198. //---------------------------
  199.  
  200.  
  201.   //circle
  202.  
  203.  
  204.  
  205.     companion object {
  206.         var centerLat:Double = 40.923341438092784
  207.         var centerLng:Double = -73.96957672209044
  208.  
  209.         // Approximate distance in degrees for 1 mile
  210.         val latDistance = 1.0 / 69.0
  211.         val lngDistance = 1.0 / (69.0 * cos(centerLat * Math.PI / 180))
  212.  
  213.         val southWestLat = centerLat - latDistance
  214.         val southWestLng = centerLng - lngDistance
  215.  
  216.         val northEastLat = centerLat + latDistance
  217.         val northEastLng = centerLng + lngDistance
  218.        // private var current = lastLocation
  219.         private const val PERMISSION_REQUEST_ACCESS_LOCATION=100
  220.         private const val BOUNDS_ID = "BOUNDS_ID"
  221.         private val USER_BOUNDS: CameraBoundsOptions = CameraBoundsOptions.Builder()
  222.             .bounds(
  223.                 CoordinateBounds(
  224.                     Point.fromLngLat(southWestLng, southWestLat),
  225.                     Point.fromLngLat(northEastLng, northEastLat),
  226.                     false
  227.                 )
  228.  
  229.             )
  230.             .minZoom(45.0)
  231.  
  232.             .build()
  233.  
  234.  
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement