When a suggestion is selected in the MerchantsFragment's SerachView, zoom and move the map camera to center the corresponding marker.
This commit is contained in:
parent
978e8d6ee2
commit
c65cf1b69e
3 changed files with 30 additions and 10 deletions
|
@ -25,6 +25,7 @@ import com.google.android.gms.maps.CameraUpdateFactory
|
||||||
import com.google.android.gms.maps.GoogleMap
|
import com.google.android.gms.maps.GoogleMap
|
||||||
import com.google.android.gms.maps.OnMapReadyCallback
|
import com.google.android.gms.maps.OnMapReadyCallback
|
||||||
import com.google.android.gms.maps.SupportMapFragment
|
import com.google.android.gms.maps.SupportMapFragment
|
||||||
|
import com.google.android.gms.maps.model.LatLng
|
||||||
import com.google.android.gms.maps.model.LatLngBounds
|
import com.google.android.gms.maps.model.LatLngBounds
|
||||||
import com.google.android.gms.maps.model.MapStyleOptions
|
import com.google.android.gms.maps.model.MapStyleOptions
|
||||||
import com.google.android.gms.maps.model.Marker
|
import com.google.android.gms.maps.model.Marker
|
||||||
|
@ -62,6 +63,8 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, SearchView.OnSuggestio
|
||||||
|
|
||||||
// SearchView suggestions
|
// SearchView suggestions
|
||||||
private const val SUGGEST_COLUMN_ID = "_id"
|
private const val SUGGEST_COLUMN_ID = "_id"
|
||||||
|
private const val SUGGEST_COLUMN_LAT = "suggest_lat"
|
||||||
|
private const val SUGGEST_COLUMN_LON = "suggest_lon"
|
||||||
private const val SUGGEST_COLUMN_NAME = "suggest_name"
|
private const val SUGGEST_COLUMN_NAME = "suggest_name"
|
||||||
private const val SUGGEST_COLUMN_ADDRESS = "suggest_address"
|
private const val SUGGEST_COLUMN_ADDRESS = "suggest_address"
|
||||||
private const val SUGGEST_COLUMN_IS_MERCHANT = "suggest_is_merchant"
|
private const val SUGGEST_COLUMN_IS_MERCHANT = "suggest_is_merchant"
|
||||||
|
@ -199,6 +202,8 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, SearchView.OnSuggestio
|
||||||
for (merchant in t1) {
|
for (merchant in t1) {
|
||||||
val mapObject = MapObject(
|
val mapObject = MapObject(
|
||||||
merchant._id,
|
merchant._id,
|
||||||
|
merchant.lat,
|
||||||
|
merchant.lon,
|
||||||
merchant.name,
|
merchant.name,
|
||||||
merchant.address,
|
merchant.address,
|
||||||
1
|
1
|
||||||
|
@ -209,6 +214,8 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, SearchView.OnSuggestio
|
||||||
for (teller in t2) {
|
for (teller in t2) {
|
||||||
val mapObject = MapObject(
|
val mapObject = MapObject(
|
||||||
teller._id,
|
teller._id,
|
||||||
|
teller.lat,
|
||||||
|
teller.lon,
|
||||||
teller.gt_name,
|
teller.gt_name,
|
||||||
teller.address,
|
teller.address,
|
||||||
0
|
0
|
||||||
|
@ -223,13 +230,14 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, SearchView.OnSuggestio
|
||||||
Log.d(TAG, "list with ${mapObjects.size} elements")
|
Log.d(TAG, "list with ${mapObjects.size} elements")
|
||||||
val cursor = MatrixCursor(
|
val cursor = MatrixCursor(
|
||||||
arrayOf(
|
arrayOf(
|
||||||
SUGGEST_COLUMN_ID, SUGGEST_COLUMN_NAME,
|
SUGGEST_COLUMN_ID, SUGGEST_COLUMN_LAT, SUGGEST_COLUMN_LON, SUGGEST_COLUMN_NAME,
|
||||||
SUGGEST_COLUMN_ADDRESS, SUGGEST_COLUMN_IS_MERCHANT, SUGGEST_COLUMN_IMAGE_RESOURCE
|
SUGGEST_COLUMN_ADDRESS, SUGGEST_COLUMN_IS_MERCHANT, SUGGEST_COLUMN_IMAGE_RESOURCE
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
for (mapObject in mapObjects) {
|
for (mapObject in mapObjects) {
|
||||||
cursor.addRow(arrayOf(BigInteger(mapObject._id, 16).toLong(), mapObject.name, mapObject.address,
|
cursor.addRow(arrayOf(BigInteger(mapObject._id, 16).toLong(), mapObject.lat, mapObject.lon,
|
||||||
mapObject.isMerchant, if (mapObject.isMerchant == 1) R.drawable.ic_merchant_pin else R.drawable.ic_teller_pin))
|
mapObject.name, mapObject.address, mapObject.isMerchant,
|
||||||
|
if (mapObject.isMerchant == 1) R.drawable.ic_merchant_pin else R.drawable.ic_teller_pin))
|
||||||
}
|
}
|
||||||
mSearchView?.suggestionsAdapter?.changeCursor(cursor)
|
mSearchView?.suggestionsAdapter?.changeCursor(cursor)
|
||||||
}
|
}
|
||||||
|
@ -245,17 +253,26 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, SearchView.OnSuggestio
|
||||||
override fun onSuggestionClick(position: Int): Boolean {
|
override fun onSuggestionClick(position: Int): Boolean {
|
||||||
val cursor = mSearchView?.suggestionsAdapter?.getItem(position) as Cursor?
|
val cursor = mSearchView?.suggestionsAdapter?.getItem(position) as Cursor?
|
||||||
val id = cursor?.getString(cursor.getColumnIndex(SUGGEST_COLUMN_ID))
|
val id = cursor?.getString(cursor.getColumnIndex(SUGGEST_COLUMN_ID))
|
||||||
|
val lat = cursor?.getString(cursor.getColumnIndex(SUGGEST_COLUMN_LAT))?.toDoubleOrNull()
|
||||||
|
val lon = cursor?.getString(cursor.getColumnIndex(SUGGEST_COLUMN_LON))?.toDoubleOrNull()
|
||||||
val name = cursor?.getString(cursor.getColumnIndex(SUGGEST_COLUMN_NAME)) ?: ""
|
val name = cursor?.getString(cursor.getColumnIndex(SUGGEST_COLUMN_NAME)) ?: ""
|
||||||
val isMerchant = cursor?.getInt(cursor.getColumnIndex(SUGGEST_COLUMN_IS_MERCHANT))
|
val isMerchant = cursor?.getInt(cursor.getColumnIndex(SUGGEST_COLUMN_IS_MERCHANT))
|
||||||
cursor?.close()
|
cursor?.close()
|
||||||
|
|
||||||
if (isMerchant == 1) {
|
if (lat != null && lon != null) {
|
||||||
// Search and show merchant marker
|
val builder = LatLngBounds.builder()
|
||||||
context?.toast("Merchant: $name")
|
builder.include(LatLng(lat, lon))
|
||||||
} else {
|
|
||||||
// Search and show teller marker
|
val bounds = builder.build()
|
||||||
context?.toast("Teller: $name")
|
|
||||||
|
try {
|
||||||
|
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100))
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.d(TAG, e.message)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mSearchView?.clearFocus()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package cy.agorise.bitsybitshareswallet.models
|
||||||
|
|
||||||
class MapObject(
|
class MapObject(
|
||||||
val _id: String,
|
val _id: String,
|
||||||
|
val lat: Double,
|
||||||
|
val lon: Double,
|
||||||
val name: String,
|
val name: String,
|
||||||
val address: String?,
|
val address: String?,
|
||||||
val isMerchant: Int
|
val isMerchant: Int
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout
|
<androidx.constraintlayout.widget.ConstraintLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
|
Loading…
Reference in a new issue