Google maps 如何在科特林的谷歌地图中记录步行情况
我正试图在kotlin使用google maps API跟踪一次步行 现在我只得到最后一个已知的当前位置(实际上不是真实的当前位置) 这是我的地图活动代码:Google maps 如何在科特林的谷歌地图中记录步行情况,google-maps,kotlin,Google Maps,Kotlin,我正试图在kotlin使用google maps API跟踪一次步行 现在我只得到最后一个已知的当前位置(实际上不是真实的当前位置) 这是我的地图活动代码: import android.content.pm.PackageManager import android.location.Location import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.core.ap
import android.content.pm.PackageManager
import android.location.Location
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.example.macc_project.R
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.LatLng
import com.google.android.gms.maps.model.Marker
class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMarkerClickListener {
override fun onMarkerClick(p0: Marker?): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
companion object {
private const val LOCATION_PERMISSION_REQUEST_CODE = 1
}
private lateinit var map: GoogleMap
private lateinit var lastLocation: Location
private lateinit var fusedLocationClient: FusedLocationProviderClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps_google)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.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) {
map = googleMap
map.uiSettings.isZoomControlsEnabled = true
map.setOnMarkerClickListener(this)
setUpMap()
}
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
}
// 1
map.isMyLocationEnabled = true
// 2
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)
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 12f))
}
}
}
}
我该如何记录我的整个行走过程?例如,在我的行走之后画一条蓝线,从初始状态到最终状态即使用户离开应用程序或关闭屏幕,您是否希望它继续跟踪移动?@Tenfour04是我想要这个behaviour@Tenfour04我可以利用Google Api客户端和LocationListener作为链接吗?大约8年前,我制作了一个追踪位置的应用程序,所以我的知识已经过时了。我只是想提出总体战略。您需要一个前台服务来监控位置,这样当活动不在屏幕上时,您就不会错过位置点。我已经很长时间没有使用服务了,所以我不知道让服务与活动通信的推荐方法。但我认为在这种情况下,由于活动完成时不需要运行服务,所以可以使用AndroidViewModel实现启动服务,并在
onCleared()
中结束服务。该服务可以将位置点添加到活动观察到的MutableLiveData
。