不使用Navigation SDK,在Google Map实现司机实时路径追踪的方法咨询
核心思路
不用Navigation SDK的情况下,核心逻辑是持续获取司机设备的实时定位数据,将新的经纬度点逐步追加到Polyline的点集合中,同时更新地图上的司机位置标记,以此实现路径的实时追踪。
具体实现步骤
1. 实时获取经纬度数据
依赖系统原生定位服务获取司机的实时位置,根据需求设置合适的更新频率与精度:
- Android(Kotlin)示例:
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context) val locationRequest = LocationRequest.create().apply { interval = 10000 // 每10秒更新一次,可按需调整 fastestInterval = 5000 // 最快5秒更新一次 priority = LocationRequest.PRIORITY_HIGH_ACCURACY // 高精度定位 } val locationCallback = object : LocationCallback() { override fun onLocationResult(locationResult: LocationResult) { locationResult.lastLocation?.let { location -> // 拿到最新经纬度:location.latitude、location.longitude updateDriverPath(location.latitude, location.longitude) } } } // 启动定位更新 fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
- iOS(Swift)示例:
import CoreLocation class DriverLocationManager: NSObject, CLLocationManagerDelegate { private let locationManager = CLLocationManager() override init() { super.init() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let latestLocation = locations.last else { return } // 拿到最新经纬度:latestLocation.coordinate.latitude、latestLocation.coordinate.longitude updateDriverPath(latitude: latestLocation.coordinate.latitude, longitude: latestLocation.coordinate.longitude) } }
2. 实时更新Polyline路径
初始化Google Map时创建空的Polyline,每次获取新定位点后,将点追加到路径集合并刷新Polyline:
- Android(Kotlin)示例:
private var driverPolyline: Polyline? = null private val pathPoints = mutableListOf<LatLng>() // 初始化Polyline private fun initPolyline(map: GoogleMap) { val polylineOptions = PolylineOptions() .color(Color.BLUE) .width(10f) driverPolyline = map.addPolyline(polylineOptions) } // 更新路径 private fun updateDriverPath(lat: Double, lng: Double) { val newPoint = LatLng(lat, lng) pathPoints.add(newPoint) // 刷新Polyline的点集合 driverPolyline?.points = pathPoints // 可选:自动将地图视角移动到司机当前位置 map.moveCamera(CameraUpdateFactory.newLatLngZoom(newPoint, 15f)) }
- iOS(Swift)示例:
private var driverPolyline: GMSPolyline? private var pathPoints = GMSMutablePath() // 初始化Polyline private func initPolyline(mapView: GMSMapView) { driverPolyline = GMSPolyline(path: pathPoints) driverPolyline?.strokeColor = .blue driverPolyline?.strokeWidth = 10.0 driverPolyline?.map = mapView } // 更新路径 private func updateDriverPath(latitude: CLLocationDegrees, longitude: CLLocationDegrees) { val newPoint = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) pathPoints.add(newPoint) // 刷新Polyline路径 driverPolyline?.path = pathPoints // 可选:自动将地图视角移动到司机当前位置 mapView.camera = GMSCameraPosition(target: newPoint, zoom: 15, bearing: 0, viewingAngle: 0) }
3. 优化建议
- 平衡精度与耗电:如果不需要极致精度,可降低定位优先级(如Android使用
PRIORITY_BALANCED_POWER_ACCURACY),延长更新间隔,减少设备耗电。 - 路径防抖:过滤掉距离过近的定位点(如两点间距小于5米则不添加),避免路径出现频繁抖动。
- 位置标记同步:除了Polyline,可添加一个Marker标记司机当前位置,每次更新定位时同步移动Marker,提升直观性。
- 离线缓存:若司机可能进入无网络区域,可先缓存定位点,待网络恢复后再做同步(如需上传至服务器)。
内容的提问来源于stack exchange,提问作者Rajni Bajaj
相关产品推荐
相关产品推荐

