当用户移动时,将地图上的所有标记定期移动到其更新/新的当前位置 [英] Move all the markers on the map to their updated/new current location periodically as users moves

查看:198
本文介绍了当用户移动时,将地图上的所有标记定期移动到其更新/新的当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款android应用,其中一些用户从他们的设备打开相同的活动。在此活动中有一张地图,当用户从他们的设备打开此活动时,他们的位置坐标将从Firebase中获取,并且基于这些坐标的标记将显示在地图上。



以下是我的代码:

  acceptingUserReference.child(requestID).child(key).addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
$ b $ if(dataSnapshot.getValue()!= null){
final Map< String,String> newAcceptedUser = (Map< String,String>)dataSnapshot.getValue();

nameOfP.add(newAcceptedUser.get(pName));
cLatP.add(newAcceptedUser.get(currentLat ).trim());
cLngP.add(newAcceptedUser.get( currentLng)修剪());

addMarkers();

//检查地图已加载
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback(){
@Override
public void onMapLoaded(){
mMap .getUiSettings()。setZoomControlsEnabled(true);
mMap.getUiSettings()。setMapToolbarEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
mMap.setMaxZoomPreference(19.0f); mMap.setMyLocationEnabled(true);

}
});
@Override
public void onCancelled(DatabaseError databaseError){
$ b}
});

以下是 addMarkers()方法: p>

  public void addMarkers(){
mMap.clear();
venueMarker = mMap.addMarker(new MarkerOptions()。position(new LatLng(Double.parseDouble(venueLat),Double.parseDouble(venueLng)));
markersList.add(venueMarker);
(for i = 0; i< nameOfP.size(); i ++){
p = mMap.addMarker(new MarkerOptions()。position(new LatLng(Double.valueOf(cLatP.get(i)) ,Double.valueOf(cLngP.get(i))))。title(nameOfP.get(i).trim())。icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
markersList.add( pMarker);
}
}

以下是 onLocationChanged ()

  @Override 
public void onLocationChanged(Location location){
mCurrentLocation = location;
currentLtAU = mCurrentLocation.getLatitude();
currentLnAU = mCurrentLocation.getLongitude();
}

用户不断向特定位置移动。



我想要的是在用户移动时将各个标记移动到新位置,以便每个人都可以看到每个人和每个人目前的位置。请帮我弄明白。

每当 onLocationChanged 调用意味着用户移动了一段距离。



更新 Firebase 中用户的更新位置 onLocationChanged 所以,其他用户可以找到您更新的位置。

注意:use geofire 更新用户位置并查看此 answer

  @Override 
public void onLocationChanged(Location location){
mCurrentLocation = location;
currentLtAU = mCurrentLocation.getLatitude();
currentLnAU = mCurrentLocation.getLongitude();

2 call 当您成功将用户位置更新为 Firebase


时,您可以接受使用者参考.child(requestID)

I'm developing an android app where some users opens the same activity from their devices. There is a map in this activity and as users opens this activity from their devices, their location coordinates is fetched from Firebase and a marker based on these coordinates is shown on the map.

Here's my code:

    acceptingUserReference.child(requestID).child(key).addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {

                                    if (dataSnapshot.getValue() != null) {
                                            final Map<String, String> newAcceptedUser = (Map<String, String>) dataSnapshot.getValue();

                                            nameOfP.add(newAcceptedUser.get("pName"));                      
                                            cLatP.add(newAcceptedUser.get("currentLat").trim());
                                            cLngP.add(newAcceptedUser.get("currentLng").trim());

                                            addMarkers();

                                            //Check map is loaded
                                            mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                                                @Override
                                                public void onMapLoaded() {
                                                     mMap.getUiSettings().setZoomControlsEnabled(true);
                                                    mMap.getUiSettings().setMapToolbarEnabled(true);
                                                    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                                                    mMap.setMaxZoomPreference(19.0f);  mMap.setMyLocationEnabled(true);

                                                }
                                            });
                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });

Here's addMarkers() method:

public void addMarkers() {
        mMap.clear();
        venueMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(venueLat), Double.parseDouble(venueLng)));
        markersList.add(venueMarker);
        for (int i = 0; i < nameOfP.size(); i++) {
            p = mMap.addMarker(new MarkerOptions().position(new LatLng(Double.valueOf(cLatP.get(i)), Double.valueOf(cLngP.get(i)))).title(nameOfP.get(i).trim()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
            markersList.add(pMarker);
        }
}

Here's onLocationChanged():

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    currentLtAU = mCurrentLocation.getLatitude();
    currentLnAU = mCurrentLocation.getLongitude();
}

The users keeps moving towards a specific location.

What I want is to move the respective markers to the new location as the users moves so that everybody can see where each and everyone currently is. Please help me figure it out.

解决方案

1. Whenever onLocationChanged called means user moved to some distance.

Update user's updated location on Firebase in onLocationChanged so, other user can find your updated location.

Note: use geofire to update user location and see this answer.

@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
currentLtAU = mCurrentLocation.getLatitude();
currentLnAU = mCurrentLocation.getLongitude();
}

2. call acceptingUserReference.child(requestID) code when you successfully update user location to Firebase

这篇关于当用户移动时,将地图上的所有标记定期移动到其更新/新的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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