Add Database helper, can save marker in stop table and read it
This commit is contained in:
2
.idea/deploymentTargetSelector.xml
generated
2
.idea/deploymentTargetSelector.xml
generated
@@ -4,7 +4,7 @@
|
|||||||
<selectionStates>
|
<selectionStates>
|
||||||
<SelectionState runConfigName="app">
|
<SelectionState runConfigName="app">
|
||||||
<option name="selectionMode" value="DROPDOWN" />
|
<option name="selectionMode" value="DROPDOWN" />
|
||||||
<DropdownSelection timestamp="2024-06-28T17:12:31.815486600Z">
|
<DropdownSelection timestamp="2024-07-01T11:57:37.460926400Z">
|
||||||
<Target type="DEFAULT_BOOT">
|
<Target type="DEFAULT_BOOT">
|
||||||
<handle>
|
<handle>
|
||||||
<DeviceId pluginId="PhysicalDevice" identifier="serial=y5v4t4fiif555tnb" />
|
<DeviceId pluginId="PhysicalDevice" identifier="serial=y5v4t4fiif555tnb" />
|
||||||
|
|||||||
9
app/src/main/java/com/example/busroute/BusStop.kt
Normal file
9
app/src/main/java/com/example/busroute/BusStop.kt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package com.example.busroute
|
||||||
|
|
||||||
|
import org.osmdroid.views.overlay.Marker
|
||||||
|
|
||||||
|
data class BusStop(val marker: Marker){
|
||||||
|
override fun toString(): String {
|
||||||
|
return "${this.marker.position.latitude} | ${this.marker.position.longitude}\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
27
app/src/main/java/com/example/busroute/Database/DbHelper.kt
Normal file
27
app/src/main/java/com/example/busroute/Database/DbHelper.kt
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package com.example.busroute.Database
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.database.sqlite.SQLiteDatabase
|
||||||
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
|
|
||||||
|
class DbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
|
||||||
|
override fun onCreate(db: SQLiteDatabase) {
|
||||||
|
db.execSQL(PathContract.SQL_CREATE_ENTRIES)
|
||||||
|
db.execSQL(StopContract.SQL_CREATE_ENTRIES)
|
||||||
|
}
|
||||||
|
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||||
|
// This database is only a cache for online data, so its upgrade policy is
|
||||||
|
// to simply to discard the data and start over
|
||||||
|
db.execSQL(PathContract.SQL_DELETE_ENTRIES)
|
||||||
|
db.execSQL(StopContract.SQL_DELETE_ENTRIES)
|
||||||
|
onCreate(db)
|
||||||
|
}
|
||||||
|
override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||||
|
onUpgrade(db, oldVersion, newVersion)
|
||||||
|
}
|
||||||
|
companion object {
|
||||||
|
// If you change the database schema, you must increment the database version.
|
||||||
|
const val DATABASE_VERSION = 1
|
||||||
|
const val DATABASE_NAME = "BusRoute.db"
|
||||||
|
}
|
||||||
|
}
|
||||||
18
app/src/main/java/com/example/busroute/Database/Path.kt
Normal file
18
app/src/main/java/com/example/busroute/Database/Path.kt
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example.busroute.Database
|
||||||
|
|
||||||
|
import android.provider.BaseColumns
|
||||||
|
|
||||||
|
object PathContract {
|
||||||
|
// Table contents are grouped together in an anonymous object.
|
||||||
|
object PathEntry : BaseColumns {
|
||||||
|
const val TABLE_NAME = "path"
|
||||||
|
const val PATH_NAME = "name"
|
||||||
|
}
|
||||||
|
|
||||||
|
const val SQL_CREATE_ENTRIES =
|
||||||
|
"CREATE TABLE ${PathEntry.TABLE_NAME} (" +
|
||||||
|
"${BaseColumns._ID} INTEGER PRIMARY KEY," +
|
||||||
|
"${PathEntry.PATH_NAME} TEXT)"
|
||||||
|
|
||||||
|
const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${PathEntry.TABLE_NAME}"
|
||||||
|
}
|
||||||
22
app/src/main/java/com/example/busroute/Database/Stop.kt
Normal file
22
app/src/main/java/com/example/busroute/Database/Stop.kt
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.example.busroute.Database
|
||||||
|
|
||||||
|
import android.provider.BaseColumns
|
||||||
|
|
||||||
|
object StopContract {
|
||||||
|
// Table contents are grouped together in an anonymous object.
|
||||||
|
object StopEntry : BaseColumns {
|
||||||
|
const val TABLE_NAME = "stop"
|
||||||
|
const val LATITUDE = "latitude"
|
||||||
|
const val LONGITUDE = "longitude"
|
||||||
|
const val ORDER = "'order'"
|
||||||
|
}
|
||||||
|
|
||||||
|
const val SQL_CREATE_ENTRIES =
|
||||||
|
"CREATE TABLE ${StopEntry.TABLE_NAME} (" +
|
||||||
|
"${BaseColumns._ID} INTEGER PRIMARY KEY," +
|
||||||
|
"${StopEntry.LATITUDE} DOUBLE, "+
|
||||||
|
"${StopEntry.LONGITUDE} DOUBLE, "+
|
||||||
|
"${StopEntry.ORDER} INTEGER)"
|
||||||
|
|
||||||
|
const val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${StopEntry.TABLE_NAME}"
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.example.busroute
|
package com.example.busroute
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
@@ -10,6 +11,7 @@ import android.net.Uri
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import android.preference.PreferenceManager
|
import android.preference.PreferenceManager
|
||||||
|
import android.provider.BaseColumns
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
@@ -17,7 +19,8 @@ import android.widget.TextView
|
|||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.content.ContextCompat
|
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.LocationCallback
|
||||||
import com.google.android.gms.location.LocationRequest
|
import com.google.android.gms.location.LocationRequest
|
||||||
import com.google.android.gms.location.LocationResult
|
import com.google.android.gms.location.LocationResult
|
||||||
@@ -37,14 +40,10 @@ import org.osmdroid.views.overlay.gestures.RotationGestureOverlay
|
|||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
private val REQUEST_PERMISSIONS_REQUEST_CODE = 1
|
private val REQUEST_PERMISSIONS_REQUEST_CODE = 1
|
||||||
private lateinit var map : MapView
|
private lateinit var map : MapView
|
||||||
private lateinit var fusedLocationClient: FusedLocationProviderClient
|
|
||||||
|
|
||||||
private lateinit var currentLocation: Location
|
private lateinit var currentLocation: Location
|
||||||
lateinit var locationManager: LocationManager
|
private lateinit var locationManager: LocationManager
|
||||||
private lateinit var mapController: IMapController
|
private lateinit var mapController: IMapController
|
||||||
|
|
||||||
private lateinit var locationByGps: Location
|
|
||||||
private lateinit var locationByNetwork: Location
|
|
||||||
private lateinit var positionMarker: Marker
|
private lateinit var positionMarker: Marker
|
||||||
|
|
||||||
private lateinit var locationCallback: LocationCallback
|
private lateinit var locationCallback: LocationCallback
|
||||||
@@ -52,10 +51,13 @@ class MainActivity : ComponentActivity() {
|
|||||||
|
|
||||||
private var latitude = 0.0
|
private var latitude = 0.0
|
||||||
private var longitude = 0.0
|
private var longitude = 0.0
|
||||||
|
private var busStopList: ArrayList<BusStop> = ArrayList()
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||||
|
val dbHelper = DbHelper(this)
|
||||||
|
|
||||||
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this))
|
getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this))
|
||||||
@@ -71,12 +73,59 @@ class MainActivity : ComponentActivity() {
|
|||||||
addMarkButton.setOnClickListener{
|
addMarkButton.setOnClickListener{
|
||||||
val marker = Marker(map)
|
val marker = Marker(map)
|
||||||
marker.position = GeoPoint(latitude, longitude)
|
marker.position = GeoPoint(latitude, longitude)
|
||||||
marker.title = "MARK BUTTON !!"
|
marker.title = "Bus Stop " + busStopList.size+1
|
||||||
marker.textLabelFontSize = 10
|
marker.textLabelFontSize = 10
|
||||||
marker.icon = ContextCompat.getDrawable(this, org.osmdroid.library.R.drawable.moreinfo_arrow)
|
marker.icon = ContextCompat.getDrawable(this, org.osmdroid.library.R.drawable.moreinfo_arrow)
|
||||||
map.overlays.add(marker)
|
map.overlays.add(marker)
|
||||||
map.invalidate()
|
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)
|
val mapsButton = findViewById<Button>(R.id.maps)
|
||||||
mapsButton.setOnClickListener{
|
mapsButton.setOnClickListener{
|
||||||
val mapIntentUri =
|
val mapIntentUri =
|
||||||
@@ -84,6 +133,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
val mapIntent = Intent(Intent.ACTION_VIEW, mapIntentUri)
|
val mapIntent = Intent(Intent.ACTION_VIEW, mapIntentUri)
|
||||||
mapIntent.setPackage("com.google.android.apps.maps")
|
mapIntent.setPackage("com.google.android.apps.maps")
|
||||||
startActivity(mapIntent)
|
startActivity(mapIntent)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val rotationGestureOverlay = RotationGestureOverlay(map)
|
val rotationGestureOverlay = RotationGestureOverlay(map)
|
||||||
@@ -111,7 +161,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
location.provider = LocationManager.NETWORK_PROVIDER
|
location.provider = LocationManager.NETWORK_PROVIDER
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
currentLocation = location
|
||||||
latitude = location.latitude
|
latitude = location.latitude
|
||||||
longitude = location.longitude
|
longitude = location.longitude
|
||||||
mapController.animateTo(GeoPoint(latitude, longitude))
|
mapController.animateTo(GeoPoint(latitude, longitude))
|
||||||
@@ -191,22 +241,4 @@ class MainActivity : ComponentActivity() {
|
|||||||
REQUEST_PERMISSIONS_REQUEST_CODE)
|
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);
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
@@ -21,6 +21,24 @@
|
|||||||
android:textColor="@color/black"
|
android:textColor="@color/black"
|
||||||
android:clickable="true"
|
android:clickable="true"
|
||||||
android:text="Add marker" />
|
android:text="Add marker" />
|
||||||
|
<Button
|
||||||
|
android:id="@+id/removeMark"
|
||||||
|
android:layout_width="150dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:backgroundTint="@color/white"
|
||||||
|
android:focusable="true"
|
||||||
|
android:textColor="@color/black"
|
||||||
|
android:clickable="true"
|
||||||
|
android:text="Remove last marker" />
|
||||||
|
<Button
|
||||||
|
android:id="@+id/saveMark"
|
||||||
|
android:layout_width="150dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:backgroundTint="@color/white"
|
||||||
|
android:focusable="true"
|
||||||
|
android:textColor="@color/black"
|
||||||
|
android:clickable="true"
|
||||||
|
android:text="Save Markers" />
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/maps"
|
android:id="@+id/maps"
|
||||||
android:layout_width="150dp"
|
android:layout_width="150dp"
|
||||||
|
|||||||
Reference in New Issue
Block a user