[英]App crashes when getting current user's location (Kotlin)
我的代碼可以編譯,但應用程序現在不會啟動。 我嘗試了很多變體來簡單地獲取用戶的當前位置並使用 MapsSDK 顯示它,但沒有任何效果。
片段活動:
class BuyFragment : Fragment(), OnMapReadyCallback {
private var _binding: FragmentBuyBinding? = null
private val binding get() = _binding!!
private lateinit var map: GoogleMap
private lateinit var fusedLocationClient: FusedLocationProviderClient
private val locationPermissionCode = 1
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view = inflater.inflate(R.layout.fragment_buy, container, false)
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireContext())
return view
}
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
requireActivity(),
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
locationPermissionCode
)
} else {
map.isMyLocationEnabled = true
getCurrentLocation()
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == locationPermissionCode) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
map.isMyLocationEnabled = true
getCurrentLocation()
}
}
}
private fun getCurrentLocation() {
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
if (location != null) {
val currentLocation = LatLng(location.latitude, location.longitude)
map.addMarker(MarkerOptions().position(currentLocation).title("Current Location"))
map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 15.0f))
}
}
}
}
依賴項:
dependencies {
implementation 'androidx.preference:preference:1.0.0-rc02'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'com.google.android.gms:play-services-maps:18.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.2'
implementation 'com.google.android.gms:play-services-location:11.0.2'
implementation 'androidx.preference:preference-ktx:1.2.0'
implementation 'com.google.android.gms:play-services-location:21.0.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
布局:
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".ui.buy.BuyFragment">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</androidx.constraintlayout.widget.ConstraintLayout>
我一直將我的代碼恢復為僅顯示地圖並且可以正常工作,但我想讓該應用程序在用戶的當前位置啟動。
你能分享一下崩潰日志嗎? 這將有助於縮小根本問題的范圍。 此外,如果您還沒有,請不要忘記請求位置權限,以便您可以使用位置服務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.