地图活动每隔几秒放大一次

时间:2016-03-25 11:44:52

标签: java android google-maps

我的应用程序中有一个地图活动,而我正在尝试在打开地图活动时进行此操作,它会自动放大到我的位置,而是每隔几秒放大一次。

这是我的代码:

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()方法存在问题,但我找不到问题。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

问题是由于代码

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

当onMyLocationChange监听器在位置发生任何变化时被调用。

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

如果确实发生了位移,那么只有你可以改变地图的中心和缩放。