You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android端Google Map搜索位置后当前位置标识实现咨询

Android Google Map 添加当前位置标识与方向指引图标实现方案

一、搜索切换到目标位置后显示当前位置标识

如果需要在切换到目标位置后,用自定义标识/图片显示当前位置,可以按以下步骤实现:

1. 确保位置权限已获取

在AndroidManifest.xml中声明权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

动态申请权限(在Activity/Fragment中):

private fun checkLocationPermission() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), LOCATION_PERMISSION_REQUEST_CODE)
    } else {
        getCurrentLocation()
    }
}

2. 获取当前位置并添加自定义Marker

使用FusedLocationProviderClient获取最新位置,然后在地图上添加自定义Marker:

private lateinit var fusedLocationClient: FusedLocationProviderClient
private var currentLocationMarker: Marker? = null

private fun getCurrentLocation() {
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? ->
        location?.let {
            // 移除旧的当前位置Marker(如果存在)
            currentLocationMarker?.remove()
            // 创建自定义Marker选项
            val markerOptions = MarkerOptions()
                .position(LatLng(it.latitude, it.longitude))
                .title("当前位置")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_current_location)) // 替换为你的自定义图标
            // 添加Marker到地图
            currentLocationMarker = googleMap.addMarker(markerOptions)
            // 可选:将地图视角移动到目标位置,同时保持当前位置Marker可见
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(targetLatLng, 15f)) // targetLatLng是你搜索到的目标位置
        }
    }
}

二、从当前位置搜索其他位置时显示指向当前位置的方向图标

当在当前位置搜索其他目标位置时,需要在目标位置处添加指向当前位置的方向图标,核心是计算目标位置到当前位置的方位角,然后旋转图标:

1. 计算方位角

通过两个位置的经纬度计算方位角(即目标位置指向当前位置的角度):

private fun calculateBearing(currentLatLng: LatLng, targetLatLng: LatLng): Float {
    val currentLocation = Location("current")
    currentLocation.latitude = currentLatLng.latitude
    currentLocation.longitude = currentLatLng.longitude
    
    val targetLocation = Location("target")
    targetLocation.latitude = targetLatLng.latitude
    targetLocation.longitude = targetLatLng.longitude
    
    // 返回目标位置指向当前位置的方位角(0-360度)
    return targetLocation.bearingTo(currentLocation)
}

2. 添加带方向的指引Marker

在目标位置添加Marker,并设置图标旋转角度:

private var directionMarker: Marker? = null

private fun addDirectionMarker(currentLatLng: LatLng, targetLatLng: LatLng) {
    val bearing = calculateBearing(currentLatLng, targetLatLng)
    // 移除旧的方向Marker
    directionMarker?.remove()
    val markerOptions = MarkerOptions()
        .position(targetLatLng)
        .title("指向当前位置")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_direction_arrow)) // 替换为你的箭头图标
        .rotation(bearing) // 设置图标旋转角度,指向当前位置
    directionMarker = googleMap.addMarker(markerOptions)
}

3. 实时更新方向(可选)

如果当前位置实时变化,需要监听位置更新并刷新Marker方向:

private fun startLocationUpdates() {
    val locationRequest = LocationRequest.create().apply {
        interval = 5000
        fastestInterval = 2000
        priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    }
    fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
}

private val locationCallback = object : LocationCallback() {
    override fun onLocationResult(locationResult: LocationResult) {
        locationResult.lastLocation?.let { location ->
            val currentLatLng = LatLng(location.latitude, location.longitude)
            // 如果已添加方向Marker,更新其旋转角度
            directionMarker?.let {
                it.rotation = calculateBearing(currentLatLng, it.position)
            }
            // 同时更新当前位置Marker
            currentLocationMarker?.position = currentLatLng
        }
    }
}

注意事项

  • 确保自定义图标资源(ic_current_location、ic_direction_arrow)已添加到项目的drawable目录中。
  • 在地图初始化完成后(OnMapReadyCallback的onMapReady方法内)执行上述逻辑。
  • 处理位置权限被拒绝的情况,提示用户开启权限。

内容的提问来源于stack exchange,提问作者Vishal Rao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 12:55:29