如何在Firebase上创建节点并对其进行更新

时间:2018-05-16 18:36:30

标签: java android firebase location

我必须将我的位置存储在Firebase中并保持更新。我目前可以存储它,但是当位置更改它创建一个新节点时,如何在应用程序开始时只创建一个节点并在每次位置更改时更新它?感谢。

代码:

DatabaseReference rootRef = database.getInstance().getReference();
String key = firebaseData.child("Posicion/").push().getKey();
LatLng latLng = new LatLng(latitude,longitude);
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/Posicion/" + key, latLng);
rootRef.updateChildren(childUpdates);

它创建了这个结构:https://i.stack.imgur.com/SlOQq.png

1 个答案:

答案 0 :(得分:0)

您正在更改插入和更新之间的节点键。

尝试以下列格式创建节点,例如:

-locations

- id(这个必须是您在应用启动时创建的唯一ID或记录的用户ID)

---- currentLatitude:&#34; xxxxxx&#34;

---- currentLongitude:&#34; yyyyyy&#34;

在这种情况下,实现将类似于:

<!--navigation bar-->
<nav class="navbar navbar-default transparent navbar-static-top" role="navigation" id="navbar-main">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="row">
<div class="col-lg-12"> <a class="navbar-brand" href="http://patskailua.com/index.php"><img src="http://patskailua.com/images/kailua-beach-properties-llc.png"  alt="pats kailua hawaiian Flower" usemap="#Map" class="img-responsive">
</a></div>
</div>
</div>
</div>
</div>
<div class="navbar-header">
<div class="text-center">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navCollapse">
<span class="sr-only">Toggle Navigation</span>
<span class="fa fa-chevron-down"></span> Menu
</button>    
<div class="collapse navbar-collapse" id="navCollapse">
<ul class="nav navbar-nav navbar-right">
<li class="active goleft1"><a href="http://patskailua.com/wp/our-vacation-properties.php">vacation rentals</a></li>
<li><a href="http://patskailua.com/wp/sightseeing.php">kailua activities</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">rentals by size<span class="caret black"></span></a>
<ul class="dropdown-menu">
<li><a href="http://patskailua.com/wp/three-bedrooms.php" >Three Bedrooms</a></li>
<li><a href="http://patskailua.com/wp/two-bedrooms.php">Two Bedrooms</a></li>
<li><a href="http://patskailua.com/wp/one-bedrooms.php">One Bedrooms</a></li>
<li><a href="http://patskailua.com/wp/studios.php">Studios</a></li>
</ul>
</li><li><a href="http://patskailua.com/wp/contact-us.php">contact us</a>            </li>
<li><a href=" http://patskailua.com/wp/about-us.php">about us</a></li>
<li class="dropdown">
<a href=" http://patskailua.com/wp/property-management.php" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">property management and sales<span class="caret black"></span></a>
<ul class="dropdown-menu">
<li><a href=" http://patskailua.com/wp/property-management.php">Property Management</a></li>
<li><a href=" http://patskailua.com/wp/buying-a-home-in-hawaii.php">Buying a Property</a></li>
<li><a href=" http://patskailua.com/wp/selling-a-home-in-hawaii.php">Selling a Property</a></li>

     <li class="goleft"><a href="http://patskailua.com/index.php">home</a></li>
</li>
</ul>
</div>

</nav>
<!--end nav bar-->