Android平台Google Map如何获取路线上多地点的中间位置?
Hey there! Welcome to Stack Overflow—super glad you're diving into this. Let's walk through how to grab midpoints or intermediate locations along a route between two (or more) spots in Google Maps for Android.
First, the Basics
To pull this off, you'll need two key pieces:
- The full set of coordinates (LatLng points) that make up your route (this comes from the Google Maps Directions API)
- A way to calculate either the exact midpoint between two points, or split the entire route into equal segments to get intermediate stops
Step 1: Fetch Route Polyline & Decode It
First, call the Directions API to get your route data. The response will include an encoded polyline string for each leg of the route. You need to decode this string into a list of LatLng points that represent every segment of the route.
Here's a Kotlin function to decode the polyline:
fun decodePolyline(encoded: String): List<LatLng> { val poly = ArrayList<LatLng>() var index = 0 val len = encoded.length var lat = 0 var lng = 0 while (index < len) { var b: Int var shift = 0 var result = 0 do { b = encoded[index++].code - 63 result = result or ((b and 0x1f) shl shift) shift += 5 } while (b >= 0x20) val dlat = if (result and 1 != 0) (result shr 1).inv() else result shr 1 lat += dlat shift = 0 result = 0 do { b = encoded[index++].code - 63 result = result or ((b and 0x1f) shl shift) shift += 5 } while (b >= 0x20) val dlng = if (result and 1 != 0) (result shr 1).inv() else result shr 1 lng += dlng val latLng = LatLng((lat.toDouble() / 1E5), (lng.toDouble() / 1E5)) poly.add(latLng) } return poly }
Step 2: Calculate Midpoints
Case 1: Exact Midpoint Between Two LatLng Points
If you just need the straight-line midpoint between two specific locations (not along the actual route path), use this simple formula:
fun getMidpoint(point1: LatLng, point2: LatLng): LatLng { val midLat = (point1.latitude + point2.latitude) / 2 val midLng = (point1.longitude + point2.longitude) / 2 return LatLng(midLat, midLng) }
Note: This gives a straight-line midpoint, not the midpoint along the driving/walking route.
Case 2: Intermediate Points Along the Full Route
If you want points spaced evenly along the actual route (e.g., split a 3-stop route into 4 equal segments), follow these steps:
- Combine all decoded
LatLngpoints from every leg/step of your route into one single list. - Calculate the total number of points, then pick indices at regular intervals to get your intermediate spots.
Here's a function to do that:
fun getIntermediatePoints(routePoints: List<LatLng>, numberOfPoints: Int): List<LatLng> { val intermediatePoints = mutableListOf<LatLng>() if (routePoints.size <= 1 || numberOfPoints <= 0) return intermediatePoints // Add the start point first intermediatePoints.add(routePoints.first()) val step = routePoints.size / (numberOfPoints + 1) for (i in 1..numberOfPoints) { val index = step * i if (index < routePoints.size) { intermediatePoints.add(routePoints[index]) } } // Add the end point intermediatePoints.add(routePoints.last()) return intermediatePoints }
Putting It All Together
- Make a request to the Directions API with your start, end, and any waypoints.
- Parse the response to extract the encoded polyline(s) for each leg.
- Decode each polyline into
LatLnglists and merge them into one full route list. - Use the functions above to get your midpoints/intermediate points.
- You can then add markers on the map for these points using
map.addMarker(MarkerOptions().position(midpoint)).
Quick Notes
- Make sure you've enabled the Directions API in your Google Cloud Console and added your API key to your Android project.
- For longer routes, the polyline will have more points, so the intermediate points will be more accurate to the actual route.
- If you need points based on distance (e.g., every 5km), you'll need to calculate the cumulative distance between consecutive points and pick spots when the distance threshold is hit.
内容的提问来源于stack exchange,提问作者Shashikant

