244 lines
10 KiB
Kotlin
244 lines
10 KiB
Kotlin
package com.example.busroute
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.content.ContentValues
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.content.pm.PackageManager
|
|
import android.location.Location
|
|
import android.location.LocationManager
|
|
import android.net.Uri
|
|
import android.os.Bundle
|
|
import android.os.Looper
|
|
import android.preference.PreferenceManager
|
|
import android.provider.BaseColumns
|
|
import android.util.Log
|
|
import android.view.WindowManager
|
|
import android.widget.Button
|
|
import android.widget.TextView
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.core.app.ActivityCompat
|
|
import androidx.core.content.ContextCompat
|
|
import com.example.busroute.Database.DbHelper
|
|
import com.example.busroute.Database.StopContract
|
|
import com.google.android.gms.location.LocationCallback
|
|
import com.google.android.gms.location.LocationRequest
|
|
import com.google.android.gms.location.LocationResult
|
|
import com.google.android.gms.location.LocationServices
|
|
import com.google.android.gms.location.Priority
|
|
import org.osmdroid.api.IMapController
|
|
import org.osmdroid.config.Configuration.getInstance
|
|
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
|
|
import org.osmdroid.util.GeoPoint
|
|
import org.osmdroid.views.MapView
|
|
import org.osmdroid.views.overlay.Marker
|
|
import org.osmdroid.views.overlay.compass.CompassOverlay
|
|
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider
|
|
import org.osmdroid.views.overlay.gestures.RotationGestureOverlay
|
|
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
private val REQUEST_PERMISSIONS_REQUEST_CODE = 1
|
|
private lateinit var map : MapView
|
|
|
|
private lateinit var currentLocation: Location
|
|
private lateinit var locationManager: LocationManager
|
|
private lateinit var mapController: IMapController
|
|
private lateinit var positionMarker: Marker
|
|
|
|
private lateinit var locationCallback: LocationCallback
|
|
private lateinit var locationRequest: LocationRequest
|
|
|
|
private var latitude = 0.0
|
|
private var longitude = 0.0
|
|
private var busStopList: ArrayList<BusStop> = ArrayList()
|
|
|
|
@SuppressLint("MissingPermission")
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
|
val dbHelper = DbHelper(this)
|
|
|
|
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this))
|
|
setContentView(R.layout.main)
|
|
|
|
map = findViewById<MapView>(R.id.map)
|
|
map.setTileSource(TileSourceFactory.MAPNIK)
|
|
positionMarker = Marker(map)
|
|
mapController = map.controller
|
|
mapController.setZoom(18.0)
|
|
|
|
val addMarkButton = findViewById<Button>(R.id.addMark)
|
|
addMarkButton.setOnClickListener{
|
|
val marker = Marker(map)
|
|
marker.position = GeoPoint(latitude, longitude)
|
|
marker.title = "Bus Stop " + busStopList.size+1
|
|
marker.textLabelFontSize = 10
|
|
marker.icon = ContextCompat.getDrawable(this, org.osmdroid.library.R.drawable.moreinfo_arrow)
|
|
map.overlays.add(marker)
|
|
map.invalidate()
|
|
busStopList.add(BusStop(marker))
|
|
Log.i("marker", busStopList.toString())
|
|
}
|
|
val removeMarkButton = findViewById<Button>(R.id.removeMark)
|
|
removeMarkButton.setOnClickListener{
|
|
if(busStopList.size < 1){
|
|
return@setOnClickListener
|
|
}
|
|
val marker = busStopList.last()
|
|
marker.marker.remove(map)
|
|
busStopList.remove(marker)
|
|
}
|
|
val saveMarkButton = findViewById<Button>(R.id.saveMark)
|
|
saveMarkButton.setOnClickListener{
|
|
val db = dbHelper.writableDatabase
|
|
busStopList.forEach {
|
|
val values = ContentValues().apply {
|
|
put(StopContract.StopEntry.LATITUDE, it.marker.position.latitude)
|
|
put(StopContract.StopEntry.LONGITUDE, it.marker.position.longitude)
|
|
put(StopContract.StopEntry.ORDER, busStopList.indexOf(it))
|
|
}
|
|
val newRowId = db?.insert(StopContract.StopEntry.TABLE_NAME, null, values)
|
|
}
|
|
val dbread = dbHelper.readableDatabase
|
|
val projection = arrayOf(BaseColumns._ID, StopContract.StopEntry.LATITUDE, StopContract.StopEntry.LONGITUDE, StopContract.StopEntry.ORDER)
|
|
val selection = ""
|
|
val selectionArgs = arrayOf("")
|
|
val sortOrder = "${StopContract.StopEntry.ORDER} ASC"
|
|
val cursor = dbread.query(
|
|
StopContract.StopEntry.TABLE_NAME, // The table to query
|
|
projection, // The array of columns to return (pass null to get all)
|
|
null, // The columns for the WHERE clause
|
|
null, // The values for the WHERE clause
|
|
null, // don't group the rows
|
|
null, // don't filter by row groups
|
|
sortOrder // The sort order
|
|
)
|
|
val itemIds = mutableListOf<Long>()
|
|
with(cursor) {
|
|
while (moveToNext()) {
|
|
val item = getString(getColumnIndexOrThrow(StopContract.StopEntry.LATITUDE))+getString(getColumnIndexOrThrow(StopContract.StopEntry.LONGITUDE))
|
|
//itemIds.add(itemId)
|
|
}
|
|
}
|
|
cursor.close()
|
|
}
|
|
|
|
|
|
val mapsButton = findViewById<Button>(R.id.maps)
|
|
mapsButton.setOnClickListener{
|
|
val mapIntentUri =
|
|
Uri.parse("google.navigation:q=Moutier,+France&waypoints=Cevins%7CAigueblanche")
|
|
val mapIntent = Intent(Intent.ACTION_VIEW, mapIntentUri)
|
|
mapIntent.setPackage("com.google.android.apps.maps")
|
|
startActivity(mapIntent)
|
|
|
|
}
|
|
|
|
val rotationGestureOverlay = RotationGestureOverlay(map)
|
|
rotationGestureOverlay.isEnabled
|
|
map.setMultiTouchControls(true)
|
|
map.overlays.add(rotationGestureOverlay)
|
|
val compassOverlay = CompassOverlay(this, InternalCompassOrientationProvider(this), map)
|
|
compassOverlay.enableCompass()
|
|
map.overlays.add(compassOverlay)
|
|
|
|
isLocationPermissionGranted()
|
|
val locationClient = LocationServices.getFusedLocationProviderClient(this)
|
|
|
|
locationRequest = LocationRequest.Builder(Priority.PRIORITY_BALANCED_POWER_ACCURACY,60)
|
|
.setWaitForAccurateLocation(true).setMaxUpdateAgeMillis(30).build()
|
|
locationCallback = object : LocationCallback() {
|
|
@SuppressLint("SetTextI18n")
|
|
override fun onLocationResult(locationResult: LocationResult) {
|
|
locationResult ?: return
|
|
for (location in locationResult.locations){
|
|
if(location.accuracy < 60){
|
|
if(location.provider == LocationManager.NETWORK_PROVIDER){
|
|
location.provider = LocationManager.GPS_PROVIDER
|
|
}else{
|
|
location.provider = LocationManager.NETWORK_PROVIDER
|
|
}
|
|
}
|
|
currentLocation = location
|
|
latitude = location.latitude
|
|
longitude = location.longitude
|
|
mapController.animateTo(GeoPoint(latitude, longitude))
|
|
|
|
positionMarker.position = GeoPoint(latitude, longitude)
|
|
positionMarker.title = "You"
|
|
map.overlays.add(positionMarker)
|
|
map.invalidate()
|
|
|
|
findViewById<TextView>(R.id.accuracy).text = "${location.accuracy} %"
|
|
findViewById<TextView>(R.id.position).text = "$latitude $longitude"
|
|
findViewById<TextView>(R.id.speed).text = "${location.speed} m/s"
|
|
findViewById<TextView>(R.id.speed_accuracy).text = "${location.speedAccuracyMetersPerSecond} %"
|
|
|
|
Log.i("Position", "$latitude $longitude")
|
|
Log.i("Accuracy Chosen", "${location.accuracy}")
|
|
}
|
|
}
|
|
}
|
|
locationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
|
|
|
|
}
|
|
private fun isLocationPermissionGranted(): Boolean {
|
|
return if (ActivityCompat.checkSelfPermission(
|
|
this,
|
|
android.Manifest.permission.ACCESS_COARSE_LOCATION
|
|
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
|
|
this,
|
|
android.Manifest.permission.ACCESS_FINE_LOCATION
|
|
) != PackageManager.PERMISSION_GRANTED
|
|
) {
|
|
ActivityCompat.requestPermissions(
|
|
this,
|
|
arrayOf(
|
|
android.Manifest.permission.ACCESS_FINE_LOCATION,
|
|
android.Manifest.permission.ACCESS_COARSE_LOCATION
|
|
),
|
|
1
|
|
)
|
|
false
|
|
} else {
|
|
true
|
|
}
|
|
}
|
|
|
|
|
|
override fun onResume() {
|
|
super.onResume()
|
|
//this will refresh the osmdroid configuration on resuming.
|
|
//if you make changes to the configuration, use
|
|
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
//Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
|
|
map.onResume() //needed for compass, my location overlays, v6.0.0 and up
|
|
}
|
|
|
|
override fun onPause() {
|
|
super.onPause()
|
|
//this will refresh the osmdroid configuration on resuming.
|
|
//if you make changes to the configuration, use
|
|
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
//Configuration.getInstance().save(this, prefs);
|
|
map.onPause() //needed for compass, my location overlays, v6.0.0 and up
|
|
}
|
|
|
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
val permissionsToRequest = ArrayList<String>()
|
|
var i = 0
|
|
while (i < grantResults.size) {
|
|
permissionsToRequest.add(permissions[i])
|
|
i++
|
|
}
|
|
if (permissionsToRequest.size > 0) {
|
|
ActivityCompat.requestPermissions(
|
|
this,
|
|
permissionsToRequest.toTypedArray(),
|
|
REQUEST_PERMISSIONS_REQUEST_CODE)
|
|
}
|
|
}
|
|
} |