根据实时Firebase纬度和经度值更改标记位置 [英] Change markers position base on Realtime Firebase lat and lang values
问题描述
我试图用实时数据库的值更改Google地图中标记的位置
I seek to change the position of markers in google maps with the value of the real-time database
我尝试使用mMap.clear,但这不是正确的解决方案,因为如果我有100个标记,并且更改了1个标记的位置,则100个是清除的,并且重新定位以进行1个标记的更改
I try to mMap.clear to but it's not the right solution because if I have 100 markers and change position of 1 marker the 100 is clear and reposition for make 1 marker change
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mMap.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Double lang = ds.child("lang").getValue(Double.class);
Double lat = ds.child("lat").getValue(Double.class);
save = new model(lat, lang);
ll = new LatLng(save.getLat(), save.getLang());
mCurrLocationMarker = mMap.addMarker(options
.position(ll));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我尝试更改标记的位置,但是如果我不编写mMap.clear,则实际结果会添加一个新标记,然后清除所有标记,如果我编写mMap.clear,则会添加它们以更改一个标记.
I try to change position for the marker but actual results add a new one if I not write mMap.clear and clear all markers and added them for change one marker if I write mMap.clear
推荐答案
您可以将标记对象存储为全局变量,而只需在位置更改时更改所选标记的位置即可.
You can store marker object as a global variable and just change selected markers position on location change.
就像
if (mCurrLocationMarker == null)
mCurrLocationMarker = mGoogleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_pin_red)).position(location));
else
mCurrLocationMarker.setPosition(location);
当您拥有一个以上的标记时,可以使用ArrayList或LinkedHashMap尝试相同的逻辑.
As you have more then one markers, you can try the same logic by using ArrayList or LinkedHashMap.
请检查以下文件: https://filebin.net/m2vgvcms6g73kef2/TestFragment. java?t = e9g0tdki
希望它会有所帮助..:)
Hope it helps.. :)
这篇关于根据实时Firebase纬度和经度值更改标记位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!