Android - 使用push()插入多个标记 [英] Android - Inserting multiple markers using push()

查看:145
本文介绍了Android - 使用push()插入多个标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在我的地图上插入多个标记。但是当我插入到我的Firebase数据库时,它只是插入的最后一个标记。我想我必须使用一些列表或散列表。



POJO类:

  public class FirebaseMarker {

字符串地址;
字符串时间;
双纬度;
双经度;
String routeId;
$ b public FirebaseMarker(){
}

公共FirebaseMarker(字符串地址,字符串时间,双纬度,双倍经度,字符串routerId){
this .address = address;
this.time = time;
this.latitude =纬度;
this.longitude = longitude;
this.routeId = routerId;
}

public String getRouterId(){
return routeId;


public void setRouterId(String routerId){
this.routeId = routerId;
}

public String getAddress(){
return address;

$ b $ public void setAddress(String address){
this.address = address;
}

public String getTime(){
return time;
}

public void setTime(String time){
this.time = time;
}

public double getLatitude(){
return latitude;
}

public void setLatitude(double latitude){
this.latitude = latitude;
}

public double getLongitude(){
return longitude;
}

public void setLongitude(double longitude){
this.longitude = longitude;




$ b $标记创建:

 列表<标记>标记;信息亦读率信息范讯范信息

Marker marker = mMap.addMarker(new MarkerOptions()
.position(latLng)
.title(address)
.snppet(time)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

markers.add(marker);

我如何插入:

<$ p $ FirebaseMarker marker = new FirebaseMarker(address,time,latitude,longitude,routeId);

markerId = userRef.push()。getKey();
userRef.child(sharedPreferences.getString(school,null))。child(markers)。child(markerId).setValue(markers);

这些参数都来自userinput和地理编码器

解决方案

将标记保存到数据库中是正确的,但是您需要首先将每个标记存储在内存中,以便将其保存到数据库中。



为此,您可以使用 HashMap 来简化保存:

  HashMap< String,对象> markers = new HashMap<>(); 

public void createMarker(String routeId,LatLng latLng,String address,String time){
//为这个标记创建一个唯一的ID(但是实际上并没有存储它)
String markerId = schoolReference.child(markers)。push()。getKey();

//将标记添加到HashMap
FirebaseMarker marker = new FirebaseMarker(address,time,latLng.getLatitude(),latLng.getLongitude(),routeId);
markers.put(markerId,marker);

//在地图中添加实际地图标记
mMap.addMarker(new MarkerOptions()
.position(latLng)
.title(address)
.snppet(time)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
);



$ b $ p
$ b

在这个例子中,当你创建标记(在 createMarker()方法),在向地图添加标记之前执行3个附加操作: >使用 push()(这不会联系数据库)为这个标记生成一个唯一的ID

  • 创建一个 FirebaseMarker 实例,可以在以后使用
  • 将这个唯一的ID和 FirebaseMarker HashMap



  • 然后,当您想将这些保存到数据库时,只需通过 HashMap 推送到数据库$ rel =nofollow noreferrer>使用 updateChildren() 执行多位置更新:

     public void saveToDatabase(){
    //做你的事情来保存路线

    //保存市场rs在数据库中的一次写入操作
    schoolReference.child(markers)。updateChildren(markers);
    }


    I am able to insert multiple markers on my map. But when I am inserting to my Firebase database, it is only the last marker placed that get inserted. I am thinking I have to use somekind of list or hashmap.

    POJO class:

    public class FirebaseMarker {
    
      String address;
      String time;
      double latitude;
      double longitude;
      String routeId;
    
      public FirebaseMarker() {
      }
    
      public FirebaseMarker(String address, String time, double latitude, double longitude, String routerId) {
          this.address = address;
          this.time = time;
          this.latitude = latitude;
          this.longitude = longitude;
          this.routeId = routerId;
      }
    
      public String getRouterId() {
        return routeId;
      }
    
      public void setRouterId(String routerId) {
        this.routeId = routerId;
      }
    
      public String getAddress() {
        return address;
      }
    
      public void setAddress(String address) {
        this.address = address;
      }
    
      public String getTime() {
        return time;
      }
    
      public void setTime(String time) {
        this.time = time;
      }
    
      public double getLatitude() {
        return latitude;
      }
    
      public void setLatitude(double latitude) {
        this.latitude = latitude;
      }
    
      public double getLongitude() {
        return longitude;
      }
    
      public void setLongitude(double longitude) {
        this.longitude = longitude;
      }
    }
    

    Marker creation:

    List<Marker> markers;
    markers = new ArrayList<>();
    
    Marker marker =  mMap.addMarker(new MarkerOptions()
                     .position(latLng)
                     .title(address)
                     .snppet(time)
                     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    
     markers.add(marker);
    

    How I am inserting:

    //FirebaseMarker marker = new FirebaseMarker(address, time, latitude, longitude, routeId); 
    
     markerId = userRef.push().getKey();
    userRef.child(sharedPreferences.getString("school",null)).child("markers").child(markerId).setValue(markers);
    

    The parameters are both from userinput and from geo-coder

    解决方案

    It looks like you have the general premise of saving the marker to the database correct, but you'll need to store each marker in memory first to be able to save them to the database later.

    For this, you can use a HashMap to simplify saving:

    HashMap<String, Object> markers = new HashMap<>();
    
    public void createMarker(String routeId, LatLng latLng, String address, String time) {    
        // Create a unique ID for this marker (but don't actually store it yet)
        String markerId = schoolReference.child("markers").push().getKey();
    
        // Add the marker to the HashMap
        FirebaseMarker marker = new FirebaseMarker(address, time, latLng.getLatitude(), latLng.getLongitude(), routeId);
        markers.put(markerId, marker);
    
        // Add an actual map marker to the map
        mMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title(address)
            .snppet(time)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
        );
    }
    

    In this example, when you create the marker (in the createMarker() method), you perform 3 additional actions before actually adding a marker to the map:

    • Generate a unique ID for this marker, using push() (this does not contact the database)
    • Create a FirebaseMarker instance that can be used later
    • Add this unique ID and FirebaseMarker pair to a HashMap

    Then, when you want to save these to the database, you simply push the entire HashMap onto the database by performing a multi-location update using updateChildren():

    public void saveToDatabase() {
        // Do your thing to save the route
    
        // Save the markers to the database in one write operation
        schoolReference.child("markers").updateChildren(markers);
    }
    

    这篇关于Android - 使用push()插入多个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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