Java 如何在android studio地图中显示带有距离和持续时间计算的标记

Java 如何在android studio地图中显示带有距离和持续时间计算的标记,java,json,android-studio,Java,Json,Android Studio,我正在学习这个教程 第1部分和 但是,对于我的应用程序,我需要显示从gps位置到所有这些点/站的距离的多个标记/站,并显示持续时间。我怎样才能让它工作在下面的链接作为参考和上面的链接 class MainActivity:AppCompatActivity(),FirebaseDriverListener{ 伴星{ 私有const val MY\u PERMISSIONS\u REQUEST\u ACCESS\u FINE\u LOCATION=6161 private const val O

我正在学习这个教程 第1部分和 但是,对于我的应用程序,我需要显示从gps位置到所有这些点/站的距离的多个标记/站,并显示持续时间。我怎样才能让它工作在下面的链接作为参考和上面的链接

class MainActivity:AppCompatActivity(),FirebaseDriverListener{
伴星{
私有const val MY\u PERMISSIONS\u REQUEST\u ACCESS\u FINE\u LOCATION=6161
private const val ONLINE_DRIVERS=“ONLINE_DRIVERS”
}
私有lateinit var googleMap:googleMap
私有lateinit变量locationProviderClient:FusedLocationProviderClient
私有lateinit var locationRequest:locationRequest
私有lateinit变量locationCallback:locationCallback
私有变量locationFlag=true
私有lateinit var valueEventListener:FirebaseEventListenerHelper
private val uiHelper=uiHelper()
private val googleMapHelper=googleMapHelper()
private val databaseReference=FirebaseDatabase.getInstance().reference.child(在线驱动程序)
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mapFragment:SupportMapFragment=supportFragmentManager.findFragmentById(R.id.supportMap)作为SupportMapFragment
mapFragment.getMapAsync{googleMap=it}
createLocationCallback()
locationProviderClient=LocationServices.getFusedLocationProviderClient(此)
locationRequest=uiHelper.getLocationRequest()
如果(!uiHelper.isPlayServicesAvailable(此)){
Toast.makeText(这是“未安装播放服务!”,Toast.LENGTH\u SHORT.show()
完成()
}else requestLocationUpdate()
valueEventListener=FirebaseEventListenerHelper(此)
databaseReference.addChildEventListener(valueEventListener)
}
@SuppressLint(“丢失许可”)
private fun requestLocationUpdate(){
如果(!uiHelper.isHaveLocationPermission(此)){
ActivityCompat.requestPermissions(这个、数组(Manifest.permission.ACCESS\u FINE\u LOCATION)、MY\u PERMISSIONS\u REQUEST\u ACCESS\u FINE\u LOCATION)
返回
}
if(uiHelper.isLocationProviderEnabled(此))
uiHelper.showPositiveDialogWithListener(此,resources.getString(R.string.need_位置),resources.getString(R.string.location_内容),对象:IPositiveNegativeListener{
覆盖正(){
起始触觉(意图(设置、动作、位置、来源、设置))
}
},“打开”,假)
locationProviderClient.RequestLocationUpdate(locationRequest、locationCallback、Looper.myLooper())
}
private fun createLocationCallback(){
locationCallback=对象:locationCallback(){
覆盖趣味onLocationResult(locationResult:locationResult?){
super.onLocationResult(位置结果)
if(locationResult!!.lastLocation==null)返回
val latLng=latLng(locationResult.lastLocation.latitude,locationResult.lastLocation.longitude)
Log.e(“位置”,latLng.latitude.toString()+”,“+latLng.longitude)
if(位置标志){
locationFlag=false
animateCamera(拉丁语)
}
}
}
}
私人娱乐动漫节(latLng:latLng){
val cameraUpdate=googleMapHelper.buildCameraUpdate(latLng)
googleMap.animateCamera(cameraUpdate,10,空)
}
重写onRequestPermissionsResult(请求代码:Int,权限:Array,GrantResult:IntArray){
super.onRequestPermissionsResult(请求代码、权限、GrantResult)
if(requestCode==我的权限请求访问位置){
val值=授予结果[0]
if(值==PackageManager.PERMISSION_DENIED){
Toast.makeText(此“位置权限被拒绝”,Toast.LENGTH\u SHORT.show())
完成()
}else if(value==PackageManager.PERMISSION_grated)requestLocationUpdate()
}
}
已添加覆盖(驱动程序:驱动程序){
val markerOptions=googleMapHelper.getDriverMarkerOptions(LatLng(driver.lat,driver.lng))
val marker=googleMap.addMarker(markerOptions)
marker.tag=driver.driverId
MarkerCollection.insertMarker(marker)
totalOnlineDrivers.text=resources.getString(R.string.total\u online\u drivers).plus(“”.plus(MarkerCollection.allMarkers().size)
}
已删除超越功能(驾驶员:驾驶员){
MarkerCollection.removeMarker(driver.driverId)
totalOnlineDrivers.text=resources.getString(R.string.total\u online\u drivers).plus(“”.plus(MarkerCollection.allMarkers().size)
}
已更新覆盖(驱动程序:驱动程序){
val marker=MarkerCollection.getMarker(driverId=driver.driverId)
MarkerAnimationHelper.animateMarkerToGB(marker!!,LatLng(driver.lat,driver.lng),LatLngInterpolator.sphere())
}
重写onDestroy(){
super.ondestory()
databaseReference.removeEventListener(valueEventListener)
locationProviderClient.RemovelocationUpdate(locationCallback)
MarkerCollection.clearMarkers()
}
}

class MainActivity : AppCompatActivity(), FirebaseDriverListener {
companion object {
    private const val MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 6161
    private const val ONLINE_DRIVERS = "online_drivers"
}
private lateinit var googleMap: GoogleMap
private lateinit var locationProviderClient: FusedLocationProviderClient
private lateinit var locationRequest: LocationRequest
private lateinit var locationCallback: LocationCallback
private var locationFlag = true
private lateinit var valueEventListener: FirebaseEventListenerHelper
private val uiHelper = UiHelper()
private val googleMapHelper = GoogleMapHelper()
private val databaseReference = FirebaseDatabase.getInstance().reference.child(ONLINE_DRIVERS)
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val mapFragment: SupportMapFragment = supportFragmentManager.findFragmentById(R.id.supportMap) as SupportMapFragment
    mapFragment.getMapAsync { googleMap = it }
    createLocationCallback()
    locationProviderClient = LocationServices.getFusedLocationProviderClient(this)
    locationRequest = uiHelper.getLocationRequest()
    if (!uiHelper.isPlayServicesAvailable(this)) {
        Toast.makeText(this, "Play Services did not installed!", Toast.LENGTH_SHORT).show()
        finish()
    } else requestLocationUpdate()
    valueEventListener = FirebaseEventListenerHelper(this)
    databaseReference.addChildEventListener(valueEventListener)
}
@SuppressLint("MissingPermission")
private fun requestLocationUpdate() {
    if (!uiHelper.isHaveLocationPermission(this)) {
        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION)
        return
    }
    if (uiHelper.isLocationProviderEnabled(this))
        uiHelper.showPositiveDialogWithListener(this, resources.getString(R.string.need_location), resources.getString(R.string.location_content), object : IPositiveNegativeListener {
            override fun onPositive() {
                startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
            }
        }, "Turn On", false)
    locationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper())
}
private fun createLocationCallback() {
    locationCallback = object : LocationCallback() {
        override fun onLocationResult(locationResult: LocationResult?) {
            super.onLocationResult(locationResult)
            if (locationResult!!.lastLocation == null) return
            val latLng = LatLng(locationResult.lastLocation.latitude, locationResult.lastLocation.longitude)
            Log.e("Location", latLng.latitude.toString() + " , " + latLng.longitude)
            if (locationFlag) {
                locationFlag = false
                animateCamera(latLng)
            }
        }
    }
}
private fun animateCamera(latLng: LatLng) {
    val cameraUpdate = googleMapHelper.buildCameraUpdate(latLng)
    googleMap.animateCamera(cameraUpdate, 10, null)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    if (requestCode == MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION) {
        val value = grantResults[0]
        if (value == PackageManager.PERMISSION_DENIED) {
            Toast.makeText(this, "Location Permission denied", Toast.LENGTH_SHORT).show()
            finish()
        } else if (value == PackageManager.PERMISSION_GRANTED) requestLocationUpdate()
    }
}
override fun onDriverAdded(driver: Driver) {
    val markerOptions = googleMapHelper.getDriverMarkerOptions(LatLng(driver.lat, driver.lng))
    val marker = googleMap.addMarker(markerOptions)
    marker.tag = driver.driverId
    MarkerCollection.insertMarker(marker)
    totalOnlineDrivers.text = resources.getString(R.string.total_online_drivers).plus(" ").plus(MarkerCollection.allMarkers().size)
}
override fun onDriverRemoved(driver: Driver) {
    MarkerCollection.removeMarker(driver.driverId)
    totalOnlineDrivers.text = resources.getString(R.string.total_online_drivers).plus(" ").plus(MarkerCollection.allMarkers().size)
}
override fun onDriverUpdated(driver: Driver) {
    val marker = MarkerCollection.getMarker(driverId = driver.driverId)
    MarkerAnimationHelper.animateMarkerToGB(marker!!, LatLng(driver.lat, driver.lng), LatLngInterpolator.Spherical())
}
override fun onDestroy() {
    super.onDestroy()
    databaseReference.removeEventListener(valueEventListener)
    locationProviderClient.removeLocationUpdates(locationCallback)
    MarkerCollection.clearMarkers()
}