From 20f7fa9ea5b115ed9be79ce95c5a76111bb7806d Mon Sep 17 00:00:00 2001 From: Alvaro Date: Sat, 17 Feb 2024 12:03:12 +0100 Subject: [PATCH] Added all snipets: OpenStreetMap activity, activity_open_street_map xml, OSM button, markers and routes. Snippets for issue #5 and issue #6 --- .idea/deploymentTargetDropDown.xml | 17 +++++++- .../com/example/mad_2024_app/MainActivity.kt | 13 ++++++ .../com/example/mad_2024_app/OpenStreetMap.kt | 43 ++++++++++++++++++- app/src/main/res/layout/activity_main.xml | 11 +++++ .../res/layout/activity_open_street_map.xml | 17 ++++++++ 5 files changed, 97 insertions(+), 4 deletions(-) diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 0c0c338..e55c09f 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -2,8 +2,21 @@ - - + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/example/mad_2024_app/MainActivity.kt b/app/src/main/java/com/example/mad_2024_app/MainActivity.kt index a9b6167..6a94ae7 100644 --- a/app/src/main/java/com/example/mad_2024_app/MainActivity.kt +++ b/app/src/main/java/com/example/mad_2024_app/MainActivity.kt @@ -59,6 +59,19 @@ class MainActivity : ComponentActivity(), LocationListener { Toast.makeText(this, "Location not available yet.", Toast.LENGTH_SHORT).show() } } + fun onNextOSMButtonClick(view: View) { + if (::latestLocation.isInitialized) { + Toast.makeText(this, "Going to the second layer!", Toast.LENGTH_SHORT).show() + val intent = Intent(this, OpenStreetMap::class.java).apply { + putExtra("locationBundle", Bundle().apply { + putParcelable("location", latestLocation) + }) + } + startActivity(intent) + } else { + Toast.makeText(this, "Location not available yet.", Toast.LENGTH_SHORT).show() + } + } private fun checkPermissionsAndStartLocationUpdates() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && diff --git a/app/src/main/java/com/example/mad_2024_app/OpenStreetMap.kt b/app/src/main/java/com/example/mad_2024_app/OpenStreetMap.kt index 39f65e1..19548f7 100644 --- a/app/src/main/java/com/example/mad_2024_app/OpenStreetMap.kt +++ b/app/src/main/java/com/example/mad_2024_app/OpenStreetMap.kt @@ -1,14 +1,19 @@ package com.example.mad_2024_app +import android.content.Intent import android.location.Location import android.os.Bundle import android.util.Log +import android.view.View +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat import org.osmdroid.config.Configuration 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.Polyline class OpenStreetMap : AppCompatActivity() { private val TAG = "LogoGPSOpenStreetMapActivity" @@ -50,15 +55,25 @@ class OpenStreetMap : AppCompatActivity() { .load(applicationContext, getSharedPreferences("osm", MODE_PRIVATE)) map = findViewById(R.id.map) map.setTileSource(TileSourceFactory.MAPNIK) - map.controller.setZoom(15.0) + map.controller.setZoom(18.0) val startPoint = GeoPoint(location.latitude, location.longitude) //val startPoint = GeoPoint(40.416775, -3.703790) in case you want to test it mannualy map.controller.setCenter(startPoint) addMarker(startPoint, "My current location") addMarkers(map, gymkhanaCoords, gymkhanaNames) + addMarkersAndRoute(map, gymkhanaCoords, gymkhanaNames) }; } + fun onPrevButtonClick(view: View){ + // This is the handler + Toast.makeText(this, "Going to the main layer!", Toast.LENGTH_SHORT).show() + + // go to another activity + val intent = Intent(this, MainActivity::class.java) + startActivity(intent) + } + private fun addMarker(point: GeoPoint, title: String) { val marker = Marker(map) marker.position = point @@ -73,11 +88,35 @@ class OpenStreetMap : AppCompatActivity() { val marker = Marker(mapView) marker.position = location marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM) + marker.title = "Marker at ${locationsNames.get(locationsCoords.indexOf(location))} ${location.latitude}, ${location.longitude}" + marker.icon = ContextCompat.getDrawable(this, com.google.android.material.R.drawable.ic_m3_chip_checked_circle) mapView.overlays.add(marker) } mapView.invalidate() // Refresh the map to display the new markers } + fun addMarkersAndRoute(mapView: MapView, locationsCoords: List, locationsNames: List) { + if (locationsCoords.size != locationsNames.size) { + Log.e("addMarkersAndRoute", "Locations and names lists must have the same number of items.") + return + } + val route = Polyline() + route.setPoints(locationsCoords) + route.color = ContextCompat.getColor(this, R.color.teal_700) + mapView.overlays.add(route) + for (location in locationsCoords) { + val marker = Marker(mapView) + marker.position = location + marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM) + val locationIndex = locationsCoords.indexOf(location) + marker.title = "Marker at ${locationsNames[locationIndex]} ${location.latitude}, ${location.longitude}" + marker.icon = ContextCompat.getDrawable(this, org.osmdroid.library.R.drawable.ic_menu_compass) + mapView.overlays.add(marker) + } + mapView.invalidate() + } + + override fun onResume() { super.onResume() map.onResume() @@ -87,4 +126,4 @@ class OpenStreetMap : AppCompatActivity() { super.onPause() map.onPause() } -} +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9b6875b..9f98825 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -18,6 +18,17 @@ android:onClick="onNextButtonClick" android:text="@string/button_next" /> +