如何不断地检测从当前位置在谷歌地图的Andr​​oid附近标记位置? [英] How to constantly detect nearby marker locations from current location in Google Maps android?

查看:175
本文介绍了如何不断地检测从当前位置在谷歌地图的Andr​​oid附近标记位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在那个通知用户,如果他们是从特定的标记点位置,即点¯x距离你是接近谷歌地图的左上角显示的TextView 到位置A,但只有在范围内显示的TextView 。如果他们迁出的范围内,的TextView 将消失。

I am trying to display a TextView at 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 TextView if within range. If they move out of range, the TextView will disappear.

据我所知,谷歌Places API的显示所有附近的位置,但我只想表明我在地图上,如果用户是X接近他们的距离的标记附近的位置。

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.

我怎样才能做到这一点?我仍然需要使用谷歌Places API的?

How can I achieve this? Would I still need to use 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

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

推荐答案

简短的回答 location.distanceTo(anotherLocation)

在setupMap我建议添加额外的监听器,以获得在一个简单的方式,所有的位置变化。

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

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

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

这篇关于如何不断地检测从当前位置在谷歌地图的Andr​​oid附近标记位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆