|
@@ -1,18 +1,34 @@
|
|
|
package ru.loparev.rmp
|
|
|
|
|
|
+import android.Manifest
|
|
|
+import android.annotation.SuppressLint
|
|
|
+import android.content.pm.PackageManager
|
|
|
+import android.graphics.BitmapFactory
|
|
|
+import android.location.Location
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
import android.os.Bundle
|
|
|
+import androidx.core.app.ActivityCompat
|
|
|
+import com.google.android.gms.location.FusedLocationProviderClient
|
|
|
+import com.google.android.gms.location.LocationServices
|
|
|
|
|
|
import com.google.android.gms.maps.CameraUpdateFactory
|
|
|
import com.google.android.gms.maps.GoogleMap
|
|
|
import com.google.android.gms.maps.OnMapReadyCallback
|
|
|
import com.google.android.gms.maps.SupportMapFragment
|
|
|
+import com.google.android.gms.maps.model.BitmapDescriptorFactory
|
|
|
import com.google.android.gms.maps.model.LatLng
|
|
|
+import com.google.android.gms.maps.model.Marker
|
|
|
import com.google.android.gms.maps.model.MarkerOptions
|
|
|
|
|
|
-class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
|
|
|
+class MapsActivity : AppCompatActivity(), OnMapReadyCallback,GoogleMap.OnMarkerClickListener {
|
|
|
|
|
|
private lateinit var mMap: GoogleMap
|
|
|
+ private lateinit var lastLocation: Location
|
|
|
+ private lateinit var fusedLocationClient: FusedLocationProviderClient
|
|
|
+
|
|
|
+ companion object {
|
|
|
+ private const val LOCATION_PERMISSION_REQUEST_CODE = 1
|
|
|
+ }
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
@@ -22,22 +38,78 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback {
|
|
|
.findFragmentById(R.id.map) as SupportMapFragment
|
|
|
mapFragment.getMapAsync(this)
|
|
|
|
|
|
+ fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Manipulates the map once available.
|
|
|
- * This callback is triggered when the map is ready to be used.
|
|
|
- * This is where we can add markers or lines, add listeners or move the camera. In this case,
|
|
|
- * we just add a marker near Sydney, Australia.
|
|
|
- * If Google Play services is not installed on the device, the user will be prompted to install
|
|
|
- * it inside the SupportMapFragment. This method will only be triggered once the user has
|
|
|
- * installed Google Play services and returned to the app.
|
|
|
- */
|
|
|
+
|
|
|
+
|
|
|
override fun onMapReady(googleMap: GoogleMap) {
|
|
|
+
|
|
|
mMap = googleMap
|
|
|
+ mMap.uiSettings.isZoomControlsEnabled = true
|
|
|
+ mMap.setOnMarkerClickListener { true }
|
|
|
+
|
|
|
+ setUpMap()
|
|
|
+
|
|
|
+ // 1
|
|
|
+ if (ActivityCompat.checkSelfPermission(
|
|
|
+ this,
|
|
|
+ Manifest.permission.ACCESS_FINE_LOCATION
|
|
|
+ ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
|
|
|
+ this,
|
|
|
+ Manifest.permission.ACCESS_COARSE_LOCATION
|
|
|
+ ) != PackageManager.PERMISSION_GRANTED
|
|
|
+ ) {
|
|
|
+
|
|
|
+ return
|
|
|
+ }
|
|
|
+ mMap.isMyLocationEnabled=true
|
|
|
+
|
|
|
+ fusedLocationClient.lastLocation.addOnSuccessListener(this) { location ->
|
|
|
+ // Got last known location. In some rare situations this can be null.
|
|
|
+ // 3
|
|
|
+ if (location != null) {
|
|
|
+ lastLocation = location
|
|
|
+ val currentLatLng = LatLng(location.latitude, location.longitude)
|
|
|
+ val car1LatLng = LatLng(56.0, 95.0)
|
|
|
+ placeMarkerOnMap(currentLatLng)
|
|
|
+ placeMarkerOnMap(car1LatLng)
|
|
|
+ mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 12f))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2
|
|
|
+
|
|
|
// Add a marker in Sydney and move the camera
|
|
|
// val sydney = LatLng(-34.0, 151.0)
|
|
|
// mMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
|
|
|
// mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney))
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private fun setUpMap() {
|
|
|
+ if (ActivityCompat.checkSelfPermission(this,
|
|
|
+ android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ ActivityCompat.requestPermissions(this,
|
|
|
+ arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_REQUEST_CODE)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ override fun onMarkerClick(p0: Marker?) = false
|
|
|
+
|
|
|
+
|
|
|
+ private fun placeMarkerOnMap(location: LatLng) {
|
|
|
+ // 1
|
|
|
+ val markerOptions = MarkerOptions().position(location)
|
|
|
+ markerOptions.icon(
|
|
|
+ BitmapDescriptorFactory.fromBitmap(
|
|
|
+ BitmapFactory.decodeResource(resources, R.drawable.car)))
|
|
|
+ // 2
|
|
|
+ mMap.addMarker(markerOptions)
|
|
|
+ }
|
|
|
}
|