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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32284708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:57:16  来源:igfitidea点击:

How to constantly detect nearby marker locations from current location in Google Maps android?

javaandroidgoogle-mapsgoogle-maps-markersandroid-maps-v2

提问by Pangu

I am trying to display a TextViewat the top left corner of Google Maps that inform users if they are x distances from particular marker point locations, i.e. "You are close to location A", but only display the TextViewif within range. If they move out of range, the TextViewwill disappear.

我试图TextView在谷歌地图的左上角显示一个,通知用户他们是否距离特定标记点位置 x 距离,即“您靠近位置 A”,但只显示TextView如果在范围内。如果它们超出范围,则TextView将消失。

I understand that the Google Places API shows you all the nearby locations, but I only want to show nearby locations of the markers I have on the map if users are x distances close to them.

我知道 Google Places API 会向您显示所有附近的位置,但我只想显示我在地图上的标记的附近位置,如果用户与他们相距 x 距离。

How can I achieve this? Would I still need to use Google Places API?

我怎样才能做到这一点?我还需要使用 Google Places API 吗?

my class .java:

我的类.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>

Can someone point me in the right direction? Thanks

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

回答by Stas Parshin

Short answer location.distanceTo(anotherLocation)

简答 location.distanceTo(anotherLocation)

In setupMap i recommend to add additional listener, to get all location changes in a simple way.

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

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

Then in this listener you just take distance between current location and your targets. Distance in meters.

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

@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!
        }
    }
}

Of course, you should make your activity implements GoogleMap.OnMyLocationChangeListener

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