My code compiles but the app will not start now. I've tried many variations to simply get a user's current location and display it using MapsSDK but nothing works.
Fragment Activity:
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:
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'
}
layout:
<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>
I keep reverting my code to just display maps and that works but I'd like to have the app start on the user's current location.
Could you please share the crash log? That will be helpful to narrow the root issue. Also, if you haven't already, don't forget to request location permissions so you can use location services.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.