gpt4 book ai didi

android - 谷歌地图多个标记不显示,一个显示另一个隐藏反之亦然

转载 作者:行者123 更新时间:2023-11-30 00:41:20 25 4
gpt4 key购买 nike

您好,我想在从 firebase 检索纬度和经度后显示多个标记,所以我将这些值放在循环中,但它显示一个可见,另一个隐藏,反之亦然,但我想同时显示它们的标记。

下面是我的代码。

public class MapsActivity extends AppCompatActivity
implements LocationListener, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,OnMapReadyCallback {

GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
String lat,lon;
LatLng che2;

private GoogleMap mMap;
static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;

vehicle_lat_long msend = new vehicle_lat_long();
ArrayList<vehicle_lat_long> markersArray = new ArrayList<vehicle_lat_long>();


FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// Add a marker in Sydney and move the camera

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);

// Get Location Manager and check for GPS & Network location services
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);

if(!(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) ||
!(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))) {
// Build the alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Services Not Active");
builder.setMessage("Please enable Location Services and GPS");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
// Show location settings when the user acknowledges the alert dialog
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
Dialog alertDialog = builder.create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(2000);
mLocationRequest.setFastestInterval(2000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
this.RetriveData();

}

ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {


}

@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
//Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
};


@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}

@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}

@Override
public void onLocationChanged(Location location) {
if (location != null) {
String strLocation =
DateFormat.getTimeInstance().format(location.getTime()) + "\n" +
"Latitude=" + location.getLatitude() + "\n" +
"Longitude=" + location.getLongitude();



lat = String.valueOf(location.getLatitude());
lon = String.valueOf(location.getLongitude());

msend.setLatitude(lat);
msend.setLongtitude(lon);
msend.setCarname(Global.car_name);
myRef.child(Global.trip_name).child(Global.car_name).setValue(msend);
}
}


//Retrive Data
private void RetriveData()
{
myRef.child(Global.trip_name).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
Log.e("MyTag", dataSnapshot.getKey());

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey)
{


msend = dataSnapshot.getValue(vehicle_lat_long.class);
markersArray.add(msend);
mMap.clear();

for(int i = 0 ; i < markersArray.size() ; i++ )
{
double l1= Double.parseDouble(msend.getLatitude());
double l2= Double.parseDouble(msend.getLongtitude());
che2 = new LatLng(l1,l2);

mMap.addMarker(new MarkerOptions().position(che2).title(msend.getCarname()).icon(BitmapDescriptorFactory.fromResource(R.drawable.cursor))); mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(che2, 17.0f));

}


}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot)
{

}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey)
{

}

@Override
public void onCancelled(DatabaseError databaseError)
{

}

});

}

最佳答案

根据我的评论,循环被正确调用,但用于每个标记的细节不会改变,因为您一直从 msend 而不是 markersArray 调用它.尝试更换

double l1= Double.parseDouble(msend.getLatitude());
double l2= Double.parseDouble(msend.getLongtitude());

double l1= Double.parseDouble(markersArray.get(i).getLatitude());
double l2= Double.parseDouble(markersArray.get(i).getLongtitude());

关于android - 谷歌地图多个标记不显示,一个显示另一个隐藏反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42527388/

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