-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
e.pavlushov
committed
Nov 28, 2024
1 parent
bbda17f
commit 61fa233
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/src/main/java/ru/dgis/sdk/demo/LocaleSwitchActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package ru.dgis.sdk.demo | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import android.widget.AdapterView | ||
import android.widget.ArrayAdapter | ||
import android.widget.LinearLayout | ||
import android.widget.Spinner | ||
import androidx.appcompat.app.AppCompatActivity | ||
import ru.dgis.sdk.Context | ||
import ru.dgis.sdk.coordinates.GeoPoint | ||
import ru.dgis.sdk.map.BearingSource | ||
import ru.dgis.sdk.map.CameraPosition | ||
import ru.dgis.sdk.map.DgisSource | ||
import ru.dgis.sdk.map.Map | ||
import ru.dgis.sdk.map.MapOptions | ||
import ru.dgis.sdk.map.MapView | ||
import ru.dgis.sdk.map.MyLocationControllerSettings | ||
import ru.dgis.sdk.map.MyLocationMapObjectSource | ||
import ru.dgis.sdk.map.Zoom | ||
import ru.dgis.sdk.platform.Locale | ||
import ru.dgis.sdk.platform.getLocaleManager | ||
|
||
class LocaleSwitchActivity : AppCompatActivity() { | ||
private val sdkContext: Context by lazy { application.sdkContext } | ||
private lateinit var mapView: MapView | ||
private lateinit var mapContainer: LinearLayout | ||
private var map: Map? = null | ||
private lateinit var mapSource: MyLocationMapObjectSource | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_locale_switch) | ||
|
||
mapContainer = findViewById(R.id.map_container) | ||
initSpinner(findViewById(R.id.spinnerLocale), R.array.locales_list, this::onLocaleItemSelected) | ||
recreateMap() | ||
} | ||
|
||
private fun onLocaleItemSelected(item: String) { | ||
if (item == "System") { | ||
getLocaleManager(sdkContext).overrideLocales(emptyList()) | ||
} else { | ||
Locale.makeLocale(item)?.let { | ||
getLocaleManager(sdkContext).overrideLocales(listOf(it)) | ||
} | ||
} | ||
recreateMap() | ||
} | ||
|
||
private fun recreateMap() { | ||
map?.let { lifecycle.removeObserver(mapView) } | ||
mapContainer.removeAllViews() | ||
|
||
val mapOptions = MapOptions().apply { | ||
position = CameraPosition( | ||
GeoPoint(40.37741938, 49.87862621), | ||
Zoom(9.0f) | ||
) | ||
sources = listOf(DgisSource.createDgisSource(sdkContext)) | ||
} | ||
|
||
mapView = MapView(this, mapOptions).also { | ||
it.getMapAsync { map -> | ||
this.map = map | ||
mapSource = MyLocationMapObjectSource( | ||
sdkContext, | ||
MyLocationControllerSettings(BearingSource.MAGNETIC) | ||
) | ||
map.addSource(mapSource) | ||
} | ||
mapContainer.addView(it) | ||
lifecycle.addObserver(it) | ||
} | ||
} | ||
|
||
private fun initSpinner(spinner: Spinner, itemsResource: Int, onItemSelected: (String) -> Unit) { | ||
ArrayAdapter.createFromResource(this, itemsResource, android.R.layout.simple_spinner_item).also { adapter -> | ||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) | ||
spinner.adapter = adapter | ||
} | ||
|
||
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { | ||
override fun onNothingSelected(parent: AdapterView<*>?) {} | ||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long) { | ||
onItemSelected(parent?.getItemAtPosition(pos)?.toString() ?: "") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<LinearLayout | ||
android:id="@+id/map_container" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
> | ||
</LinearLayout> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:background="@android:color/transparent" | ||
android:fitsSystemWindows="true" | ||
tools:context=".LocaleSwitchActivity"> | ||
|
||
<androidx.constraintlayout.widget.Guideline | ||
android:id="@+id/guidelineFollowModeCenter" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
app:layout_constraintGuide_percent="0.45" /> | ||
|
||
<androidx.constraintlayout.widget.Guideline | ||
android:id="@+id/guidelineFollowModeSpinners" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:orientation="vertical" | ||
app:layout_constraintGuide_percent="0.66" /> | ||
|
||
<TextView | ||
android:id="@+id/textViewLocale" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:layout_marginBottom="16dp" | ||
android:text="@string/locale" | ||
android:textColor="@color/text_foreground_day" | ||
app:layout_constraintBaseline_toBaselineOf="@id/spinnerLocale" | ||
app:layout_constraintStart_toEndOf="@id/guidelineFollowModeCenter" | ||
app:layout_constraintEnd_toStartOf="@id/guidelineFollowModeSpinners"/> | ||
|
||
<Spinner | ||
android:id="@+id/spinnerLocale" | ||
android:layout_width="0dp" | ||
android:layout_height="wrap_content" | ||
android:textAlignment="center" | ||
android:layout_marginBottom="26dp" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="@id/guidelineFollowModeSpinners" | ||
/> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<TextView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:textColor="@color/text_foreground_night" | ||
android:background="@drawable/spinner_item_background"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters