【问题标题】:Google Map Multiple Marker not showing, One show another hide vise versaGoogle Map Multiple Marker 未显示,一个显示另一个隐藏,反之亦然
【发布时间】:2017-07-20 12:34:47
【问题描述】:

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

            }

        });

    }

【问题讨论】:

  • 您不应该从markersArray 而不是mSend 检索标记详细信息吗?它可能循环正常,但所有标记都在另一个之上。
  • 从 msend 检索到 markerarray 后,,,,, msend = dataSnapshot.getValue(vehicle_lat_long.class); markerArray.add(msend);

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


【解决方案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());

【讨论】:

  • 我们要使用这样的集群概念吗?
  • 此时真的取决于您。 :) 我刚刚指出只有一个标记显示的原因是因为它实际上只是在另一个之上。
  • 替换后也只显示一个标记,你能帮我解决集群概念吗?
  • 这很奇怪。您能否打印出markersArray 中的值并验证检索到的数据是否符合预期?对不起。我不熟悉集群概念
猜你喜欢
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 2012-02-25
  • 1970-01-01
相关资源
最近更新 更多