【问题标题】:SupportMapFragment with ViewPager: Cannot move camera or change map settings带有 ViewPager 的 SupportMapFragment:无法移动相机或更改地图设置
【发布时间】:2013-01-31 19:41:18
【问题描述】:

我在使用SupportMapFragment 并将相机移动到地图上的某个位置时遇到问题。

public class MapPositionFragment extends SupportMapFragment implements LocationListener{

private GoogleMap map = null;
private Button lockButton = null;
private Location currentLocation = null;
private LocationManager locationManager = null;
private View mapView = null;

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mapView = super.onCreateView(inflater, container, savedInstanceState);
    View v = inflater.inflate(R.layout.fragment_route_map, container, false);

    SetUpMap(v);
    return v;
  }

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {

    if (isVisibleToUser){
        SetUpLocationService();
    }
}   

private void SetUpLockButton(){
    lockButton = (Button)getActivity().findViewById(R.id.lock_screen_button);
    lockButton.setOnClickListener(new OnClickListener() {           
        public void onClick(View v) {
            ToggleLockButton();
        }
    });     
}

private void ToggleLockButton(){
    BaseSwipeActivity baseActivity = (BaseSwipeActivity)getActivity();
    boolean swipeEnabled = baseActivity.TogglePaging();

    if (swipeEnabled){
        lockButton.setBackgroundResource(R.drawable.stock_lock_open);
    }
    else{
        lockButton.setBackgroundResource(R.drawable.stock_lock);
    }
}

private void SetUpLocationService(){
    boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (networkIsEnabled){
        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    } else if (gpsIsEnabled){
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }

}

private void SetCurrentLocation(final Location location){

    if (map != null){
        getActivity().runOnUiThread(new Runnable() {
              public void run() {
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
                map.moveCamera(cameraUpdate);
              }
        });

    }
}

private void SetUpMap(View view) {
    if (map == null){
        getActivity().runOnUiThread(new Runnable() {
              public void run() {
                map = getMap();
                map.getUiSettings().setMyLocationButtonEnabled(true);
                map.setIndoorEnabled(true);
                map.getUiSettings().setZoomControlsEnabled(false);
                locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
              }
        });
    }
}

@Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SetUpLockButton();
  }


public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      SetCurrentLocation(location);
}

public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}   

}

SupportMapFragment 位于 ViewPager 内。地图已显示,但我无法更改地图设置,也无法更改相机位置。我想可能是因为它在另一个线程中我必须使用 UI 线程,但这并没有改变任何东西。

所以也许有人有一个想法,那会很棒!

谢谢曼努埃尔

【问题讨论】:

  • 这可能与ViewPager 无关。检查您的 LogCat 以查看您是否有授权失败,在这种情况下,您的问题是您的清单中的 API 密钥错误,或者 API 密钥未授权用于 API 控制台中的签名密钥或包名称。这是ViewPagerSupportMapFragments 的示例项目:github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Pager
  • logcat 没有错误。我帖子的标题是错误的,我可以显示地图。现在的问题是我无法更改地图上的相机位置。所以我的 api 密钥应该可以工作。
  • 在更改相机位置的链接中查看我的示例代码。
  • 是的,我已经看到了,我就是这样做的。我还使用camerupdatefactory 设置新的latlng 位置和缩放。之后我使用 movecamera 方法更改位置和缩放,我也尝试使用 animatecamera 方法。我进入了我的 setcurrentlocation 方法,locationmanger 注入了正确的位置,但没有改变相机。我在我的 nexus 7 上试过。

标签: android map android-viewpager google-maps-android-api-2 supportmapfragment


【解决方案1】:

我今天遇到了同样的问题。这是我的解决方案。

如你所愿:

// @ SetUpMap()
map = getMap();

替换为:

// Remeber to change the map id (R.id.map) to what you have in the layout .xml
map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
        .getMap();


似乎此解决方案不适用于 Android 支持库 21.0.x(我已针对 .2 和 .3 进行了测试)。由于此方法适用于较旧的 Android 支持库,因此可能存在错误等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多