Skip to content

Commit

Permalink
Added all snipets: OpenStreetMap activity, activity_open_street_map x…
Browse files Browse the repository at this point in the history
…ml, OSM button, markers and routes.

Snippets for issue #5 and issue #6
  • Loading branch information
AlvaroTG17 committed Feb 17, 2024
1 parent 9c72720 commit 20f7fa9
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 4 deletions.
17 changes: 15 additions & 2 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions app/src/main/java/com/example/mad_2024_app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
43 changes: 41 additions & 2 deletions app/src/main/java/com/example/mad_2024_app/OpenStreetMap.kt
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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<GeoPoint>, locationsNames: List<String>) {
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()
Expand All @@ -87,4 +126,4 @@ class OpenStreetMap : AppCompatActivity() {
super.onPause()
map.onPause()
}
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
android:onClick="onNextButtonClick"
android:text="@string/button_next" />

<Button
android:id="@+id/mainButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:backgroundTint="@color/purple_200"
android:backgroundTintMode="src_atop"
android:onClick="onNextOSMButtonClick"
android:text="@string/button_next" />

<TextView
android:id="@+id/headerTextView"
android:layout_width="match_parent"
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/res/layout/activity_open_street_map.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello! This is the OSM activity"
android:padding="24dp"
android:textSize="24sp" />

<org.osmdroid.views.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_previous"
android:layout_centerInParent="true"
android:layout_marginBottom="16dp"
android:onClick="onPrevButtonClick"
android:backgroundTint="@color/purple_200"
android:backgroundTintMode="src_atop"/>
</RelativeLayout>

0 comments on commit 20f7fa9

Please sign in to comment.