Add Database helper, can save marker in stop table and read it
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
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
|
||||
@@ -10,6 +11,7 @@ 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
|
||||
@@ -17,7 +19,8 @@ import android.widget.TextView
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.google.android.gms.location.FusedLocationProviderClient
|
||||
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
|
||||
@@ -37,14 +40,10 @@ 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 fusedLocationClient: FusedLocationProviderClient
|
||||
|
||||
private lateinit var currentLocation: Location
|
||||
lateinit var locationManager: LocationManager
|
||||
private lateinit var locationManager: LocationManager
|
||||
private lateinit var mapController: IMapController
|
||||
|
||||
private lateinit var locationByGps: Location
|
||||
private lateinit var locationByNetwork: Location
|
||||
private lateinit var positionMarker: Marker
|
||||
|
||||
private lateinit var locationCallback: LocationCallback
|
||||
@@ -52,10 +51,13 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
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))
|
||||
@@ -71,12 +73,59 @@ class MainActivity : ComponentActivity() {
|
||||
addMarkButton.setOnClickListener{
|
||||
val marker = Marker(map)
|
||||
marker.position = GeoPoint(latitude, longitude)
|
||||
marker.title = "MARK BUTTON !!"
|
||||
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 =
|
||||
@@ -84,6 +133,7 @@ class MainActivity : ComponentActivity() {
|
||||
val mapIntent = Intent(Intent.ACTION_VIEW, mapIntentUri)
|
||||
mapIntent.setPackage("com.google.android.apps.maps")
|
||||
startActivity(mapIntent)
|
||||
|
||||
}
|
||||
|
||||
val rotationGestureOverlay = RotationGestureOverlay(map)
|
||||
@@ -111,7 +161,7 @@ class MainActivity : ComponentActivity() {
|
||||
location.provider = LocationManager.NETWORK_PROVIDER
|
||||
}
|
||||
}
|
||||
|
||||
currentLocation = location
|
||||
latitude = location.latitude
|
||||
longitude = location.longitude
|
||||
mapController.animateTo(GeoPoint(latitude, longitude))
|
||||
@@ -191,22 +241,4 @@ class MainActivity : ComponentActivity() {
|
||||
REQUEST_PERMISSIONS_REQUEST_CODE)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*private fun requestPermissionsIfNecessary(String[] permissions) {
|
||||
val permissionsToRequest = ArrayList<String>();
|
||||
permissions.forEach { permission ->
|
||||
if (ContextCompat.checkSelfPermission(this, permission)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
// Permission is not granted
|
||||
permissionsToRequest.add(permission);
|
||||
}
|
||||
}
|
||||
if (permissionsToRequest.size() > 0) {
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
permissionsToRequest.toArray(new String[0]),
|
||||
REQUEST_PERMISSIONS_REQUEST_CODE);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
Reference in New Issue
Block a user