gpt4 book ai didi

java - 谷歌地图绘制 Firebase 数据库中的多个标记

转载 作者:行者123 更新时间:2023-12-02 09:04:00 26 4
gpt4 key购买 nike

在 map 上绘制标记时遇到麻烦,我能够从 firebase 获取用户的纬度和经度并将其存储到我的数组列表中,我的问题是我如何能够全局设置我的数组列表?这是我的代码。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private GoogleMap googleMap;
private MarkerOptions options = new MarkerOptions();
private ArrayList<LatLng> latlngs = new ArrayList<>();
private ArrayList<String> Names = new ArrayList<>();
String Lat ,Lon,Names1;
double latitude , longitude;







@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);





FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Positions");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {

Lat = String.valueOf(chidSnap.child("Latitude").getValue());
Lon = String.valueOf(chidSnap.child("Longitude").getValue());
Names1 = String.valueOf(chidSnap.getKey());

latitude= Double.parseDouble(Lat);
longitude= Double.parseDouble(Lon);


latlngs.add(new LatLng(latitude, longitude));
Names.add(Names1);




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


// System.out.println(Names);


}









/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {



mMap = googleMap;
for (LatLng point : latlngs) {
options.position(point);
options.title("Users");
options.snippet("someDesc");
googleMap.addMarker(options);
}


}
}

我不知道这件事出了什么问题,不知何故我的 map 没有显示任何标记,而我的列表“latlngs”显示“0.0,0.0”我不知道如何全局设置我的经纬度,因为它位于从 firebase 获取数据的代码中。

最佳答案

对 Firebase 的查询是一个异步过程。因此,您的 onDataChange 可能是在 onMapReady 之后执行的。请尝试以下操作:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {

....
}

if(mMap != null) {
for (LatLng point : latlngs) {
options.position(point);
options.title("Users");
options.snippet("someDesc");
mMap.addMarker(options);
}
}
}

或者onDataChange内部调用mapFragment.getMapAsync(MapsActivity.this);

关于java - 谷歌地图绘制 Firebase 数据库中的多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59965080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com