【问题标题】:OnMapReadyCallback not callingOnMapReadyCallback 没有调用
【发布时间】:2017-08-19 01:21:23
【问题描述】:

我正在研究谷歌地图 v2。 我在自定义地图片段中实现 OnMapReadyCallback,但一旦片段开始,它就不会触发 onMapReady。

我的代码:

public class CustomMapFragment extends Fragment implements OnMapReadyCallback,GoogleMap.OnMarkerClickListener{

OnCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootView = inflater.inflate(R.layout.fragment_custom_map, container, false);

    mMapView = (MapView) rootView.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);
    mMapView.onResume(); // needed to get the map to display immediately

    try {
        MapsInitializer.initialize(this.getActivity());
    } catch (Exception e) {
        e.printStackTrace();
    }


    mMapView.getMapAsync(this);


    return rootView;
}

重写方法

@Override
public void onMapReady(GoogleMap mMap) {
    googleMap = mMap;

    googleMap.setMyLocationEnabled(true);

    LatLng mMyLocation = new LatLng(mLat, mLng);


    CameraPosition cameraPosition = new CameraPosition.Builder().target(mMyLocation).zoom(13).build();
  googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


    Log.d("Map", "OnMapReady");
    googleMap.setOnMarkerClickListener(this);

}

四处寻找,但没有运气。

【问题讨论】:

  • Log.d("Map", "OnMapReady"); 没有显示任何内容?
  • 不,完全没有显示。那就是我确认 OnMapReady 根本不会触发。
  • 你在哪里测试这个应用程序?
  • 在我的设备上测试过。

标签: android google-maps


【解决方案1】:

应用几个更改:

onCreateView() 中删除mMapView.onResume();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_map, container, false);
    try {
        MapsInitializer.initialize(getActivity());
    } catch (Exception e) {
    }
    mMapView = (MapView) view.findViewById(R.id.map);
    mMapView.onCreate(savedInstanceState);
    mMapView.getMapAsync(this);
}

现在在onResume()

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

你的 onMapReady() 看起来不错。

确保在 xml 中添加:

  <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

【讨论】:

  • 试过了。还是不触发。我添加了所有生命周期方法。地图已显示,但 OnMapReady 未触发,camerazoom 不起作用。
猜你喜欢
  • 2020-04-18
  • 1970-01-01
  • 1970-01-01
  • 2015-11-30
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多