Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package view.view
- import android.Manifest
- import android.annotation.SuppressLint
- import android.content.Intent
- import android.content.pm.PackageManager
- import android.location.Location
- import android.location.LocationManager
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import android.provider.Settings
- import android.view.Menu
- import android.widget.Toast
- import androidx.core.app.ActivityCompat
- import com.firstapp.maptest.R
- import com.firstapp.maptest.databinding.ActivityMainBinding
- import com.google.android.gms.location.FusedLocationProviderClient
- import com.google.android.gms.location.LocationCallback
- import com.google.android.gms.location.LocationResult
- import com.google.android.gms.location.LocationServices
- import com.mapbox.geojson.FeatureCollection
- import com.mapbox.geojson.Point
- import com.mapbox.geojson.Polygon
- import com.mapbox.maps.CameraBoundsOptions
- import com.mapbox.maps.CoordinateBounds
- import com.mapbox.maps.MapView
- import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource
- import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
- import com.mapbox.maps.extension.style.sources.getSource
- import com.mapbox.maps.extension.style.style
- import com.mapbox.maps.plugin.annotation.annotations
- import com.mapbox.maps.plugin.annotation.generated.CircleAnnotationOptions
- import com.mapbox.maps.plugin.annotation.generated.createCircleAnnotationManager
- import com.mapbox.maps.plugin.attribution.attribution
- import com.mapbox.maps.plugin.compass.compass
- import com.mapbox.maps.plugin.gestures.gestures
- import com.mapbox.maps.plugin.logo.logo
- import com.mapbox.maps.plugin.scalebar.scalebar
- import kotlin.math.cos
- class MainActivity : AppCompatActivity() {
- private lateinit var mapboxMap: MapView
- private lateinit var binding: ActivityMainBinding
- private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
- // private val annotationApi = mapboxMap.annotations
- // private val circleAnnotationManager = annotationApi.createCircleAnnotationManager()
- //private var latt: Double = 0.0
- // private var longg: Double = 0.0
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- // Add the resulting circle to the map.
- //circleAnnotationManager.create(circleAnnotationOptions)
- fusedLocationProviderClient= LocationServices.getFusedLocationProviderClient(this)
- getCurrentLocation()
- binding = ActivityMainBinding.inflate(layoutInflater)
- mapboxMap = binding.mapView
- mapboxMap.scalebar.updateSettings { enabled = false }
- mapboxMap.logo.updateSettings { enabled = false }
- mapboxMap.compass.updateSettings { enabled = false }
- mapboxMap.attribution.updateSettings { enabled = false }
- mapboxMap.gestures.rotateEnabled = false
- mapboxMap.gestures.pitchEnabled = false
- setContentView(binding.root)
- binding.mapView.getMapboxMap().loadStyle(
- styleExtension = style(getString(R.string.mapbox_styleURL)) {
- +geoJsonSource(BOUNDS_ID) {
- featureCollection(FeatureCollection.fromFeatures(listOf()))
- }
- }
- ){ setupBounds(USER_BOUNDS) }
- }
- //getting location
- private fun getCurrentLocation() {
- if(checkPermissions()){
- if(isLocationEnabled()){
- if (ActivityCompat.checkSelfPermission(
- this,
- Manifest.permission.ACCESS_FINE_LOCATION
- ) != PackageManager.PERMISSION_GRANTED
- ) {
- requestPermissions()
- return
- }
- fusedLocationProviderClient.lastLocation.addOnCompleteListener(this){
- task -> val location: Location?=task.result
- if(location == null){
- Toast.makeText(this, "Unknown exception", Toast.LENGTH_SHORT).show()
- }
- else{
- Toast.makeText(this, "Great Success", Toast.LENGTH_SHORT).show()
- }
- }
- }
- else{
- Toast.makeText(this, "Turn on your location", Toast.LENGTH_SHORT).show()
- val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
- startActivity(intent)
- }
- }
- else{
- requestPermissions()
- }
- }
- private fun isLocationEnabled(): Boolean {
- val locationManager:LocationManager = getSystemService(LOCATION_SERVICE) as LocationManager
- return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
- }
- private fun requestPermissions() {
- ActivityCompat.requestPermissions(
- this, arrayOf( Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_ACCESS_LOCATION
- )
- }
- private fun checkPermissions():Boolean{
- //read more on this what ia acitivty compat
- return ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED
- }
- @SuppressLint("SuspiciousIndentation")
- override fun onRequestPermissionsResult(
- requestCode: Int,
- permissions: Array<out String>,
- grantResults: IntArray
- ) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults)
- if(requestCode == PERMISSION_REQUEST_ACCESS_LOCATION){
- if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
- Toast.makeText(applicationContext,"Granted Location", Toast.LENGTH_LONG).show()
- getCurrentLocation()
- }
- else{
- Toast.makeText(applicationContext,"Denied Location", Toast.LENGTH_LONG).show()
- }
- }
- //-------------------------------------------------------------
- //bounds
- override fun onCreateOptionsMenu(menu: Menu): Boolean {
- menuInflater.inflate(R.menu.menu_bounds, menu)
- return true
- }
- private fun setupBounds(bounds: CameraBoundsOptions) {
- mapboxMap.getMapboxMap().setBounds(bounds)
- showBoundsArea(bounds)
- }
- private fun showBoundsArea(boundsOptions: CameraBoundsOptions) {
- val source = mapboxMap.getMapboxMap().getStyle()!!.getSource(BOUNDS_ID) as GeoJsonSource
- val bounds = boundsOptions.bounds
- val list = mutableListOf<List<Point>>()
- bounds?.let {
- if (!it.infiniteBounds) {
- val northEast = it.northeast
- val southWest = it.southwest
- val northWest = Point.fromLngLat(southWest.longitude(), northEast.latitude())
- val southEast = Point.fromLngLat(northEast.longitude(), southWest.latitude())
- list.add(
- mutableListOf(northEast, southEast, southWest, northWest, northEast)
- )
- }
- }
- if (list.isNotEmpty()) {
- source.geometry(
- Polygon.fromLngLats(
- list
- )
- )
- }
- }
- //---------------------------
- //circle
- companion object {
- var centerLat:Double = 40.923341438092784
- var centerLng:Double = -73.96957672209044
- // Approximate distance in degrees for 1 mile
- val latDistance = 1.0 / 69.0
- val lngDistance = 1.0 / (69.0 * cos(centerLat * Math.PI / 180))
- val southWestLat = centerLat - latDistance
- val southWestLng = centerLng - lngDistance
- val northEastLat = centerLat + latDistance
- val northEastLng = centerLng + lngDistance
- // private var current = lastLocation
- private const val PERMISSION_REQUEST_ACCESS_LOCATION=100
- private const val BOUNDS_ID = "BOUNDS_ID"
- private val USER_BOUNDS: CameraBoundsOptions = CameraBoundsOptions.Builder()
- .bounds(
- CoordinateBounds(
- Point.fromLngLat(southWestLng, southWestLat),
- Point.fromLngLat(northEastLng, northEastLat),
- false
- )
- )
- .minZoom(45.0)
- .build()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement