我已經能夠使用存盤在 Firebase 實時資料庫中的坐標在我的 Google 地圖上添加多個標記,但當前的問題是我無法正確檢索標記的名稱。最后一個標記的名稱被分配給從資料庫中檢索到的所有標記。
我想要的是每個標記都有一個單獨的名稱和(描述)分配給它。還分享了
輸出和 firebase 資料庫的螢屏截圖。
public class HistoricalMapsActivity extends FragmentActivity implements OnMapReadyCallback {
private static final int REQUEST_LOCATION_PERMISSION = 1009;
private GoogleMap mMap;
private ActivityMapsBinding binding;
//update--Taking multiple markers from Realtime Database
private MarkerOptions options = new MarkerOptions();
private ArrayList<LatLng> latlngs = new ArrayList<>();
private ArrayList<String> Names = new ArrayList<>();
String Lat ,Lon,Names1, Names2;
double latitude , longitude;
//update--Taking multiple markers from Realtime Database
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
//update--Taking multiple markers from Realtime Database
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());
//Getting Value of Name of the Place
Names1 = String.valueOf(chidSnap.child("Name").getValue());
latitude= Double.parseDouble(Lat);
longitude= Double.parseDouble(Lon);
Names2 = Names1;
latlngs.add(new LatLng(latitude, longitude));
Names.add(Names2);
}
if(mMap != null) {
for (LatLng point : latlngs) {
options.position(point);
options.title(Names2);
options.snippet("someDesc");
mMap.addMarker(options);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//update--Taking multiple markers from Realtime Database
mapFragment.getMapAsync(this);
}
private void addMapMarkers(){
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//update--Taking multiple markers from Realtime Database
for (LatLng point : latlngs) {
options.position(point);
options.title(Names1);
options.snippet("someDesc");
googleMap.addMarker(options);
}
//update--Taking multiple markers from Realtime Database
//Adding custom maps style over here
//******** THIS PART OF CODE EXCLUSIVELY DESIGNED TO FETCH THE CUSTOM MAPS.JSON TEMPLATE**********
enableMyLocation();
try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.mapstyle));
if (!success) {
Log.e("MapsActivity", "Style parsing failed.");
}
} catch (Resources. NotFoundException e) {
Log.e("MapsActivity", "Can't find style. Error: ", e);
}
//******** MAP STYLING CODE ENDS OVER HERE **********
/* // Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
LatLng islamabad = new LatLng(33.68, 73.04);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Islamabad"));
//moving the camera position to Islamabad.
mMap.moveCamera(CameraUpdateFactory.newLatLng(islamabad));*/
}
//Getting the Users current Location
private void enableMyLocation() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION_PERMISSION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
// Check if location permissions are granted and if so enable the
// location data layer.
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_LOCATION_PERMISSION:
if (grantResults.length > 0
&& grantResults[0]
== PackageManager.PERMISSION_GRANTED) {
enableMyLocation();
break;
}
}
}
}
uj5u.com熱心網友回復:
最后一個標記的名稱被分配給從資料庫中檢索到的所有標記。
發生這種情況是因為您使用以下方法為所有標記設定相同的名稱:
options.title(Names2);
由于您是在回圈中執行此操作,因此到它結束時,Names2將保存最后一個元素的值,因此會出現這種行為。要解決這個問題,您應該根據串列中每個的位置設定標題。這可以像這樣簡單地完成:
int position = 0; //?? Initialised the counter.
for (LatLng point : latlngs) {
options.position(point);
options.title(Names.get(position)); //?? Set the title according to the position.
options.snippet("someDesc");
mMap.addMarker(options);
position ; //?? Increment the position
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/425710.html
標籤:爪哇 安卓 火力基地 谷歌地图 firebase-实时数据库
