java - 如何从 Google Maps android 中的当前位置不断检测附近的标记位置?

标签 java android google-maps google-maps-markers android-maps-v2

我试图在 Google map 的左上角显示一个 TextView 以通知用户他们是否与特定标记点位置相距 x 距离,即“您靠近位置 A”,但是仅在范围内显示 TextView。如果它们移出范围,TextView 将消失。

我知道 Google Places API 会向您显示附近的所有位置,但我只想显示我在 map 上的标记的附近位置(如果用户距离他们很近 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>

有人能指出我正确的方向吗?谢谢

最佳答案

简答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!
        }
    }
}

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

关于java - 如何从 Google Maps android 中的当前位置不断检测附近的标记位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32284708/

相关文章:

android - 在Android中的recylerview上实现 map 时,Google Map返回空对象引用

javascript - Angular 插值滞后于渲染某些值以及如何从 typescript 对象数组中删除一项

java - 将 Java 项目添加到我的 Scala 项目

java - 如何杀死 Java 进程并执行关闭钩子(Hook)

android:将 View 添加到 inflatedView 中

android - 根据 UTC 格式转换日期和时间字符串

iOS , ld : framework not found GoogleMaps for architecture arm64

java - Eclipse Juno 没有调试工具栏?

java - JNA - AMD Overdrive5 API 返回空指针错误

Android:CardView 即将推出操作栏