Android - How to show nearby user markers?

北城以北 提交于 2019-12-23 02:50:54

问题


I have an android app where users can register and when they are logged in their location is visible by markers on the map as shown in this Image. But I just want to show only nearby marker within 100 meters.

Please help me up with exact coding section and all the object initialization.

Here is my code for fetching all user markers(lat,lng) from database:

private void getNearbyMarkers(){

    geoFireRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot locationSnapshot : dataSnapshot.getChildren()){

                LocationData locations = locationSnapshot.getValue(LocationData.class);

                final Double tempLat = Double.parseDouble(locations.getLatitude());
                final Double tempLng = Double.parseDouble(locations.getLongitude());
                final String uid = locations.getUid();
                databaseReference3.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String name = dataSnapshot.child(uid).child("name").getValue().toString();
                        String business = dataSnapshot.child(uid).child("business").getValue().toString();

                        LatLng allLatLang = new LatLng(tempLat,tempLng);
                        MarkerOptions markerOptions = new MarkerOptions();
                        markerOptions.position(allLatLang);
                        markerOptions.title(name);
                        markerOptions.snippet(business);
                        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));

                        locationMarker = mMap.addMarker(markerOptions);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

Thank you very much for your time and assistance in this matter.


回答1:


You can try SphericalUtil.computeDistanceBetween, for that you need to add dependency,

dependencies {
    compile 'com.google.maps.android:android-maps-utils:0.5+'
}

And you can try like this in your code,

markerOptions.visible(false);// We dont need to show, if its less than 100 meter we can show, otherwise we will just create and we will make it visble or not later

locationMarker = mMap.addMarker(markerOptions);

LatLng yourLatLang = new LatLng([your lat],[your lang]);
if (SphericalUtil.computeDistanceBetween(yourLatLang, locationMarker.getPosition()) < 100) {
    locationMarker.setVisible(true);
}



回答2:


Short answer location.distanceTo(anotherLocation)

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

Don't forget your activity to implements GoogleMap.OnMyLocationChangeListener

Hope this solves your problem. For further knowledge about how to work with Google Maps API look into this link



来源:https://stackoverflow.com/questions/45747796/android-how-to-show-nearby-user-markers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!