如何在Google地图Android应用中,持续检测当前位置附近的标记位置?

8
我正在尝试在Google地图的左上角显示一个TextView,告诉用户他们距离特定标记点位置有x距离,例如“您靠近A位置”,但仅在范围内时显示TextView。如果他们移出范围,TextView将消失。
我知道Google Places API会显示所有附近的位置,但我只想在用户距离它们x距离时显示地图上标记的附近位置。
我该如何实现这一点?我是否仍需要使用Google Places API?
我的类.java:
private GoogleMap mMap;

private LatLng currLocation;

private static final LatLng POINTA = new LatLng(32.820193, -117.232568);
private static final LatLng POINTB = new LatLng(32.829129, -117.232204);
private static final LatLng POINTC = new LatLng(32.821114, -117.231534);
private static final LatLng POINTD = new LatLng(32.825157, -117.232003);

// Array to store hotspot coordinates
private ArrayList<LatLng> markerCoords = new ArrayList<LatLng>();
private static final int POINTS = 4;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    setUpMapIfNeeded();
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Create MenuInflater object to insert ActionBar buttons
    MenuInflater inflater = getMenuInflater();

    // Display the ActionBar buttons from .xml
    inflater.inflate(R.menu.menu_map, menu);

    return true;
}

@Override
protected void onResume()
{
    super.onResume();
    setUpMapIfNeeded();

}

private void setUpMapIfNeeded()
{
    if (mMap == null)
    {
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

        // Check if we were successful in obtaining the map.
        if (mMap != null)
        {
            markerCoords.add(POINTA);
            markerCoords.add(POINTB);
            markerCoords.add(POINTC);
            markerCoords.add(POINTD);

            setUpMap();
        }
    }
}
private void setUpMap()
{
    // Set My Location blue dot
    mMap.setMyLocationEnabled(true);

    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location myLocation = locationManager.getLastKnownLocation(provider);

    if (myLocation != null)
    {
        double latitude = myLocation.getLatitude();
        double longitude = myLocation.getLongitude();
        currLocation = new LatLng(latitude, longitude);
    }

    for (int i = 0; i < POINTS; i++)
    {
        mMap.addMarker(new MarkerOptions()
                .position(markerCoords.get(i))
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.post_it_marker)));
    }
}

map.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        map:cameraTargetLat="32.881416"
        map:cameraTargetLng="-117.237428"
        map:cameraZoom="15"
        map:mapType="normal" />
</RelativeLayout>

有人能指点我方向吗?谢谢。

你好,你解决了这个问题吗?因为我也遇到了同样的挑战。 - Zack
1个回答

12

简短回答:location.distanceTo(anotherLocation)

在setupMap中,我建议添加额外的监听器以简单地获取所有位置更改。

mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);

然后在这个监听器中,您只需计算当前位置和目标之间的距离。以米为单位。

@Override
public void onMyLocationChange(Location location) {
    Location target = new Location("target");
    for(LatLng point : new LatLng[]{POINTA, POINTB, POINTC, POINTD}) {
        target.setLatitude(point.latitude);
        target.setLongitude(point.longitude);
        if(location.distanceTo(target) < METERS_100) {
            // bingo!
        }
    }
}

当然,你应该让你的活动实现GoogleMap.OnMyLocationChangeListener


我在使用this时遇到了一个错误,提示“无法将setOnMyLocationChangeListener应用于此”。 - Pangu
@Pangu,加油,使用Android Studio修复很容易。只需让您的活动实现GoogleMap.OnMyLocationChangeListener接口即可。我已更新我的答案。 - Stas Parshin
1
@pengrad...非常抱歉,我是安卓方面的新手 :( ....我会尝试并很快告诉你结果! - Pangu
@Pangu 好的,让我们关闭这篇帖子 :) - Stas Parshin
似乎有一个小问题,当我记录位置和目标之间的距离时,它返回x米,但将其转换为英里后,得到的距离比我在浏览器上使用谷歌地图显示的距离少了1.4英里...您知道问题可能是什么吗? - Pangu
1
这是一个直线距离,并且在谷歌地图上是路线距离。 - Stas Parshin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接