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

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):

Step 1: Grab the Coordinates of Points A and B

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 LatLng objects:

    // 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 LatLng values:

    val pointA = LatLng(34.0522, -118.2437) // Example: Los Angeles
    val pointB = LatLng(34.0622, -118.2537) // Example: Nearby point
    
Step 2: Calculate the Direction from A to B

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.

Step 3: Compute the 50-Meter Target Point

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.

Step 4: Show the Target Point on the Map

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
Visual Reference

Here's a quick diagram to visualize how this works:
Map diagram showing Point A (red), Point B (blue), and the 50m target point (green)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:11:46