谷歌地图多个标记没有显示,一个显示另一个隐藏反之亦然

时间:2017-03-01 09:02:36

标签: android google-maps firebase google-maps-api-3 google-maps-android-api-2

嗨我想在从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)
            {

            }

        });

    }

1 个答案:

答案 0 :(得分:1)

根据我的评论,循环被正确调用,但用于每个标记的详细信息不会更改,因为您始终从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());