android - Google Maps.on camera Change Listener 永远不会被调用

标签 android google-maps-android-api-2

我有一个实现 GoogleMap.onCameraChangedListener 的 Activity(扩展 map fragment ),并且我已经覆盖了 onCameraChange 方法。

问题是,每当我在 map 上移动时,即相机位置发生变化时,onCameraChange 方法永远不会被调用。

这可能是什么原因造成的?

最佳答案

试试这个:

 public class MapActivity extends AppCompatActivity implements
    GoogleMap.OnCameraChangeListener{
    private GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.googlemap_layout);
        setUpMapIfNeeded();

     }

@Override
public void onCameraChange(CameraPosition cameraPosition) {
    double latitude = cameraPosition.target.latitude;
    double longitude = cameraPosition.target.longitude;

}
private void setUpMapIfNeeded() {

    if (googleMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        if (googleMap != null) {

            googleMap.setMyLocationEnabled(true);
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            googleMap.getUiSettings().setRotateGesturesEnabled(false);
            googleMap.getUiSettings().setZoomControlsEnabled(false);
            googleMap.setOnCameraChangeListener(this);

        }
    }
}

布局:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

关于android - Google Maps.on camera Change Listener 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31633404/

相关文章:

Android - fragment 中的 MapView

android - 新谷歌地图 v2 的奇怪异常

android - OKHTTP 3 MediaType as "application/x-www-form-urlencoded"崩溃问题

android - Android SDK源码部分地方无法设置断点(Eclipse)

android - 找不到提供程序类 SMACK 4.1.1 Android

android - 从 Android Google Maps API V2 开始

android - 如何在我的 Activity 中显示弹出窗口?

Android map 实用程序 : images from URL

java - GoogleApiClient 空指针异常

java - 如何将图像传输到另一个 Activity