如何使用Firebase Google Maps API Android中的数据更新标记位置 [英] How to update marker positions with data from Firebase Google Maps API Android

查看:112
本文介绍了如何使用Firebase Google Maps API Android中的数据更新标记位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一款应用,可以使用Firebase数据库实时跟踪用户。每个用户都使用标记显示在地图中。

当有新的位置更新时,标记必须更新。问题在于,每次检索到新的位置更新时,使用Firebase方法onDataChange()或类似工具时,我无法访问旧标记以将其删除并创建一个新标记或仅更新旧标记,因为标记不会 t存在。



我尝试了使用Gson在SharedPreferences中保存标记,但是当我将标记传递给json时,应用程序崩溃。



有人知道我该如何更新标记吗?



这是写在onDataChange()里的:

  for(User user:usersList){
Gson gson = new Gson();
$ b $ *
字符串previousJson = preferences.getString(user.getId()+ - marker,);
标记previousMarker = gson.fromJson(previousJson,Marker.class);
if(previousMarker!= null)markerAnterior.remove();
* /

final LatLng latlng = new LatLng(user.getLastLocation()。getLatitude(),user.getLastLocation()。getLongitude());

MarkerOptions markerOptions = new MarkerOptions()
.position(latlng)
.title(user.getId());

Marker marker = mMap.addMarker(markerOptions);

// String newJson = gson.toJson(marker); // CRASH
// editor.putString(user.getId()+ - marker,newJson);
// editor.commit();


谢谢。


<创建一个HashMap实例变量,该变量将用户ID映射到标记:

 <$ c 解析方案$ c>私人地图< String,Marker> mMarkerMap = new HashMap<>(); 

然后,在HashMap中填充新的标记,以便稍后检索它们。



如果标记已存在,只需更新位置:

  for(User user:usersList ){

final LatLng latlng = new LatLng(user.getLastLocation()。getLatitude(),user.getLastLocation()。getLongitude());

标记previousMarker = mMarkerMap.get(user.getId());
if(previousMarker!= null){
//上一个标记存在,更新位置:
previousMarker.setPosition(latlng);
} else {
//无以前的标记,创建一个新的标记:
MarkerOptions markerOptions = new MarkerOptions()
.position(latlng)
.title(user .getId());

Marker marker = mMap.addMarker(markerOptions);

//将这个新标记放在HashMap中:
mMarkerMap.put(user.getId(),marker);
}
}


I'm creating an app that makes a real time tracking of users using Firebase Database. Every user is displayed in the map using markers.

When there is a new location update, the marker has to be updated. The problem is that with Firebase method onDataChange() or similars every time a new location update is retrieved, I can't access to the old marker to remove it and create a new one or just update the old marker, because the marker doesn't exist.

I tried it saving markers in SharedPreferences using Gson, but when I pass the marker to json, the app crashes.

Does anyone know how can I update the markers?

This is written inside the onDataChange():

           for (User user: usersList) {
                Gson gson = new Gson();

                /*
                String previousJson = preferences.getString(user.getId()+"-marker", "");
                Marker previousMarker = gson.fromJson(previousJson, Marker.class);
                if (previousMarker!=null) markerAnterior.remove();
                */

                final LatLng latlng = new LatLng(user.getLastLocation().getLatitude(), user.getLastLocation().getLongitude());

                MarkerOptions markerOptions = new MarkerOptions()
                        .position(latlng)
                        .title(user.getId());

                Marker marker= mMap.addMarker(markerOptions);

             //   String newJson = gson.toJson(marker); //CRASH
            //    editor.putString(user.getId()+"-marker", newJson);
            //    editor.commit();

            }

Thank you.

解决方案

Create a HashMap instance variable that will map user IDs to Markers:

private Map<String, Marker> mMarkerMap = new HashMap<>();

Then, populate new Markers in the HashMap so you can retrieve them later.

If the Marker already exists, just update the location:

for (User user : usersList) {

    final LatLng latlng = new LatLng(user.getLastLocation().getLatitude(), user.getLastLocation().getLongitude());

    Marker previousMarker = mMarkerMap.get(user.getId());
    if (previousMarker != null) {
        //previous marker exists, update position:
        previousMarker.setPosition(latlng);
    } else {
        //No previous marker, create a new one:
        MarkerOptions markerOptions = new MarkerOptions()
                .position(latlng)
                .title(user.getId());

        Marker marker = mMap.addMarker(markerOptions);

        //put this new marker in the HashMap:
        mMarkerMap.put(user.getId(), marker);
    }
}

这篇关于如何使用Firebase Google Maps API Android中的数据更新标记位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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