繁体   English   中英

地图活动每隔几秒钟就会放大

[英]Map Activity zooms in every some seconds

我的应用程序中有一个地图活动,当我打开地图活动时,我试图使其自动放大到我的位置,但是每隔几秒钟就会放大一次。

这是我的代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private static final String FIREBASE_URL="https://****.firebaseio.com/";
private Firebase firebaseRef;
private LocationManager locationManager;
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps2);
    Firebase.setAndroidContext(this);
    firebaseRef = new Firebase(FIREBASE_URL);

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    setUpMapIfNeeded();
    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {
            final String title = marker.getTitle().toString();

            firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot user : dataSnapshot.child("users").getChildren()) {
                        String event_title = user.child("event/event_title").getValue().toString();
                        if (title.equals(event_title)) {

                            String event_date = user.child("event/event_date").getValue().toString();
                            String event_content = user.child("event/event_content").getValue().toString();
                            String age_limit = user.child("event/age_limit").getValue().toString();
                            String event_hour = user.child("event/event_hour").getValue().toString();
                            String location_left = user.child("location_left").getValue().toString();
                            String location_right = user.child("location_right").getValue().toString();
                            final SharedPreferences sp = getSharedPreferences("key", 0);
                            final SharedPreferences.Editor sedt = sp.edit();

                            sedt.putString("event_title", event_title);
                            sedt.putString("event_date", event_date);
                            sedt.putString("event_content", event_content);
                            sedt.putString("age_limit", age_limit);
                            sedt.putString("event_hour", event_hour);
                            sedt.putString("location_left", location_left);
                            sedt.putString("location_right", location_right);
                            sedt.commit();

                        }

                    }
                    Intent intent = new Intent(MapsActivity.this, EventInfo.class);
                    startActivity(intent);
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });



    return true;
        }
    });

}

private void setUpMapIfNeeded() {
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mMap.setMyLocationEnabled(true);
        } else {
            Toast.makeText(MapsActivity.this, "You have to accept!", Toast.LENGTH_LONG).show();
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                mMap.setMyLocationEnabled(true);
            }
        }

        if (mMap != null) {


            mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {

                @Override
                public void onMyLocationChange(Location arg0) {

                    CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(arg0.getLatitude(), arg0.getLongitude()));
                    CameraUpdate zoom=CameraUpdateFactory.zoomTo(12);

                    mMap.moveCamera(center);
                    mMap.animateCamera(zoom);
                }
            });

        }
    }
}


@Override
public void onMapReady(final GoogleMap googleMap) {

    mMap=googleMap;
    firebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot child : dataSnapshot.child("users").getChildren()) {


                String rightLocation = child.child("location_right").getValue().toString();
                String leftLocation = child.child("location_left").getValue().toString();

                double location_left = Double.parseDouble(leftLocation);
                double location_right = Double.parseDouble(rightLocation);
                String event_title = child.child("event/event_title").getValue().toString();
                LatLng cod = new LatLng(location_left, location_right);
                googleMap.addMarker(new MarkerOptions().position(cod).title(event_title));


            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });


}

}

我可以猜到这是我的setUpMapIfNeeded()方法中的问题,但我找不到问题。

我究竟做错了什么?

问题是由于以下代码

                CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(arg0.getLatitude(), arg0.getLongitude()));
                CameraUpdate zoom=CameraUpdateFactory.zoomTo(12);
                mMap.moveCamera(center);
                mMap.animateCamera(zoom);

由于onMyLocationChange侦听器在位置发生任何更改时都将被调用。

因此,如果您需要将地图中心更改为最新位置,则必须编写代码以检查先前位置之间的实际位移。

如果实际发生位移,那么只有您可以更改地图的中心和缩放比例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM