Was ich versuche zu erreichen, ist, den letzten bekannten Standort des Benutzers anzuzeigen. Wenn das GPS deaktiviert ist, fordern Sie die Standorteinstellung an. Wenn der Benutzer OK drückt, aktiviert er das GPS und bewegt die Kartenkamera zum gefundenen Ort.Der Standort ist null, es sei denn, GoogleMap wird erneut initialisiert.
Es funktioniert wie erwartet, wenn das GPS bereits aktiviert ist; Wenn dies jedoch nicht der Fall ist, wird die Kartenkamera nicht bewegt, da die Position null ist.
Ich kann diese Arbeit machen, wenn ich initMap()
nach der else
Anweisung (wenn location == null
) innerhalb der displayLocation()
Funktion hinzufügen, aber etwas fühlt sich nicht richtig über diese Lösung.
Könnte jemand bitte erklären, was hier vor sich geht? Ich schätze es sehr!
By the way, ich bin mit MVP-Architektur, so dass, wenn die Aktivität beginnt, die erste Funktion, die aufgerufen wird, ist requestPermission()
class MainActivity : AppCompatActivity(), MainView, OnMapReadyCallback {
private val ZOOM_LEVEL = 12
private val ZOOM_DURATION = 2000
private val PERMISSION_REQUEST_CODE = 101
private val SETTINGS_REQUEST_CODE = 102
private val RC_SIGN_IN = 123
private lateinit var mMainPresent: MainPresenterImpl
private lateinit var mGoogleMap: GoogleMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mMainPresent = MainPresenterImpl(this)
mMainPresent.onCreate()
}
override fun onResume() {
super.onResume()
mMainPresent.onResume()
}
override fun onPause() {
super.onPause()
mMainPresent.onPause()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
PERMISSION_REQUEST_CODE -> {
if (grantResults.isEmpty() || grantResults[0] == PackageManager.PERMISSION_DENIED) {
finish()
} else {
initMap()
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
SETTINGS_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK) {
initMap()
}
}
}
}
override fun onMapReady(googleMap: GoogleMap) {
mGoogleMap = googleMap
displayLocation()
}
override fun displayLoginMethods() {
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(!BuildConfig.DEBUG) // disables it for debug
.setAvailableProviders(
Arrays.asList<AuthUI.IdpConfig>(AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build(),
AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build()))
.build(),
RC_SIGN_IN)
}
override fun requestPermission() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE)
}
} else {
requestLocationSettings()
}
}
override fun displayLocation() {
val mFusedLocationClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
try {
mGoogleMap.isMyLocationEnabled = true
mGoogleMap.uiSettings.isMyLocationButtonEnabled = true
mFusedLocationClient.lastLocation
.addOnSuccessListener(this) { location ->
// Got last known location. In some rare situations this can be null.
if (location != null) {
// Logic to handle location object
val latitude = location.latitude
val longitude = location.longitude
val coordinate = LatLng(latitude, longitude)
mGoogleMap.addMarker(MarkerOptions().position(coordinate))
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(coordinate))
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(ZOOM_LEVEL.toFloat()), ZOOM_DURATION, null)
} else {
Log.d("displayLocation", "location = null")
}
}
} catch (e: SecurityException) {
Log.e("Exception: %s", e.message)
}
}
/**
* Initiates Google Map. Once it's ready it calls the onMapReady method
*/
override fun initMap() {
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
private fun createLocationRequest(): LocationRequest {
val locationRequest = LocationRequest()
locationRequest.interval = 10000
locationRequest.fastestInterval = 5000
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
return locationRequest
}
private fun requestLocationSettings() {
val builder = LocationSettingsRequest.Builder().addLocationRequest(createLocationRequest())
val client = LocationServices.getSettingsClient(this)
val task = client.checkLocationSettings(builder.build())
task.addOnSuccessListener(this) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
initMap()
}
task.addOnFailureListener(this) { e ->
if (e is ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
e.startResolutionForResult([email protected], SETTINGS_REQUEST_CODE)
} catch (sendEx: IntentSender.SendIntentException) {
// Ignore the error.
}
}
}
}
}