我正在檢索數據,如下所示。對於每個「塊」數據,我需要在我設置的Google Map上添加一個標記。那麼是否可以將'lat','lng'等發送到onMapReady方法?將參數發送到onMapReady Google Maps API Android
//the List to store the retrieved data
static List<infoToStore> info = new ArrayList<>();
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot advertiser : dataSnapshot.getChildren()) {
final String catString = advertiser.child("cat").getValue(String.class);
final String URLString = advertiser.child("downloadURL").getValue(String.class);
final String keyString = advertiser.child("key").getValue(String.class);
final String lat = advertiser.child("latitude").getValue(String.class);
final String lng = advertiser.child("longitude").getValue(String.class);
final String descString = advertiser.child("serviceDesc").getValue(String.class);
final String nameString = advertiser.child("serviceName").getValue(String.class);
//populating the List
info.add(new infoToStore(catString, URLString, keyString, lat, lng, descString, nameString));
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-20.285889,57.584607) , 10.0f));
for (infoToStore details: info){
marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(getLat(), getLng())
.title(getName())
.snippet(getDesc())));
}
}
類infoToStore
public class infoToStore {
String cat;
String url;
String key;
String lat;
String lng;
String desc;
String name;
public infoToStore(String cat, String url, String key, String lat, String lng, String desc, String name) {
this.cat = cat;
this.url = url;
this.key = key;
this.lat = lat;
this.lng = lng;
this.desc = desc;
this.name = name;
}
public String getCat() {
return this.cat;
}
public String getUrl() {
return this.url;
}
public String getKey() {
return this.key;
}
public String getLat() {
return this.lat;
}
public String getLng() {
return this.lng;
}
public String getDesc() {
return this.desc;
}
public String getName() {
return this.name;
}
}
如果'onDataChange'在地圖加載後調用,你可以嘗試檢查if(mMap!= null),然後直接從'onDataChange'添加標記 –