Construct uri and launch google maps navigation

This commit is contained in:
clement
2024-07-10 16:18:38 +02:00
parent 49cdd06d5c
commit b97d209917
2 changed files with 33 additions and 0 deletions

View File

@@ -1,7 +1,9 @@
package com.example.busroute
import android.content.Intent
import android.location.Address
import android.location.Geocoder
import android.net.Uri
import android.os.Bundle
import android.provider.BaseColumns
import android.util.Log
@@ -9,10 +11,12 @@ import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.Spinner
import android.widget.TableLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.busroute.DataClass.BusStop
import com.example.busroute.DataClass.PathList
import com.example.busroute.Database.DbHelper
import com.example.busroute.Database.PathContract
@@ -27,6 +31,7 @@ class PathToMaps: AppCompatActivity() {
val dbHelper = DbHelper(this)
val db = dbHelper.readableDatabase
val geocoder = Geocoder(this, Locale.getDefault())
val StopPoint = ArrayList<String>()
setContentView(R.layout.select_path)
@@ -64,6 +69,7 @@ class PathToMaps: AppCompatActivity() {
id: Long
) {
stopTable.removeAllViews()
StopPoint.clear()
val selectedPath = parentView!!.selectedItem
if(selectedPath != null){
val cursor = db.rawQuery("SELECT * " +
@@ -80,6 +86,7 @@ class PathToMaps: AppCompatActivity() {
val geoResults: MutableList<Address>? = geocoder.getFromLocation(latitude, longitude, 1)
text.text = "$latitude | $longitude\r${geoResults?.get(0)?.getAddressLine(0)}\n"
stopTable.addView(text)
StopPoint.add("$latitude|$longitude")
}
}
}
@@ -89,5 +96,26 @@ class PathToMaps: AppCompatActivity() {
// your code here
}
}
val openMapsButton = findViewById<Button>(R.id.open_maps)
openMapsButton.setOnClickListener {
val lastPoint = StopPoint.last().split("|")
var uri = "google.navigation:q=${lastPoint[0]},${lastPoint[1]}&waypoints="
StopPoint.forEach {
if(StopPoint.indexOf(it) < StopPoint.size -1){
val position = it.split("|")
uri += "${position[0]},${position[1]}"
if(StopPoint.indexOf(it) < StopPoint.size -2){
uri += "%7C"
}
}
}
val mapIntentUri = Uri.parse(uri)
val mapIntent = Intent(Intent.ACTION_VIEW, mapIntentUri)
mapIntent.setPackage("com.google.android.apps.maps")
startActivity(mapIntent)
}
}
}

View File

@@ -5,6 +5,11 @@
<Spinner
android:id="@+id/combo_path">
</Spinner>
<Button
android:id="@+id/open_maps"
android:text="Lancer le trajet dans Maps">
</Button>
<TableLayout
android:id="@+id/stop_table"
android:layout_width="match_parent"