Android - 如何显示附近的用户标记? [英] Android - How to show nearby user markers?

查看:122
本文介绍了Android - 如何显示附近的用户标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个android应用程序,用户可以注册,当他们登录到他们的位置时,可以通过地图上的标记看到,如图所示。但我只想在100米内显示附近的标记。

请帮我准确的编码部分和所有的对象初始化。





这里是我提取所有用户的代码标记(lat,lng)来自数据库:

  private void getNearbyMarkers(){

geoFireRef.addValueEventListener (DataSnapshot locationSnapshot:dataSnapshot.getChildren()){
$ b public Value onDataChange(DataSnapshot dataSnapshot){

(new ValueEventListener(){
@Override
$ b 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 ValueEve ntListener(){
@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 =新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){


});



$ @覆盖
public void onCancelled(DatabaseError databaseError){

}
} );


$ / code>

非常感谢您在这件事上的时间和协助。

解决方案

您可以尝试 SphericalUtil.computeDistanceBetween 添加依赖项,

 依赖项{
compile'c​​om.google.maps.android:android-maps-utils: 0.5+'
}

你可以在代码中这样试试,

  markerOptions.visible(false); //我们不需要显示,如果它小于100米我们可以显示,否则我们会创建并且我们将使其可见或不迟于

locationMarker = mMap.addMarker(markerOptions);

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


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.

解决方案

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);
}

这篇关于Android - 如何显示附近的用户标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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