Android Studio中如何在地图两点间定位距离50米的目标点位
Hey fellow dev! So you need to pinpoint a spot exactly 50 meters along the line between two marked points (A and B) on a map in Android Studio? No problem—let's break this down into straightforward steps that work with Google Maps (the go-to map SDK for Android):
First, you need the geographic coordinates (latitude/longitude) of your two points. There are two common ways to get these:
User-selected points: If users tap the map to mark A and B, use the map's click listener to capture
LatLngobjects:// Assuming you have a GoogleMap instance named 'map' map.setOnMapClickListener { latLng -> // Use your own logic to assign this to pointA or pointB (e.g., a toggle button) if (isSelectingPointA) { pointA = latLng map.addMarker(MarkerOptions().position(pointA).title("Point A").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))) } else { pointB = latLng map.addMarker(MarkerOptions().position(pointB).title("Point B").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))) } }Pre-defined points: If A and B are fixed, just hardcode their
LatLngvalues:val pointA = LatLng(34.0522, -118.2437) // Example: Los Angeles val pointB = LatLng(34.0622, -118.2537) // Example: Nearby point
To find the 50m point, you first need the bearing (direction angle) from A to B. The easiest way to do this is using Google's Maps Utils library—it handles all the spherical math for you.
First, add the dependency to your app-level build.gradle (Module level):
dependencies { implementation 'com.google.maps.android:android-maps-utils:2.2.5' }
Then compute the bearing:
val bearingFromAToB = SphericalUtil.computeHeading(pointA, pointB)
This gives you the angle (in degrees) from true north pointing towards point B.
Now use the bearing to calculate the point that's exactly 50 meters from A towards B. Again, the Maps Utils library has a handy method for this:
val targetDistance = 50.0 // Meters val targetPoint = SphericalUtil.computeOffset(pointA, targetDistance, bearingFromAToB)
If you need the 50m point from B towards A instead, just swap pointA and pointB in the computeHeading call, or use pointB as the starting point in computeOffset.
Finally, add a marker to the map to highlight your target point:
map.addMarker(MarkerOptions() .position(targetPoint) .title("Target (50m from A)") .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))) // Optional: Move the camera to focus on all three points val bounds = LatLngBounds.builder() .include(pointA) .include(pointB) .include(targetPoint) .build() map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100)) // 100px padding
Here's a quick diagram to visualize how this works:
The green marker sits exactly 50 meters along the straight line connecting A and B.
Quick Notes
- Make sure you've set up the Google Maps SDK correctly in your project (enabled the API key in Google Cloud Console, added the SDK dependency).
- If you don't want to use the Maps Utils library, you can implement the spherical trigonometry manually—but trust me, the library saves you from a ton of math bugs!
内容的提问来源于stack exchange,提问作者Hamidreza

