【问题标题】:Cannot solve getMapAsync in Fragment无法解决 Fragment 中的 getMapAsync
【发布时间】:2017-07-05 02:40:41
【问题描述】:

我想获取用户在地图中的当前位置,我无法在 mapAsync 函数中将“this”作为参数传递 我在getMapAsync(this) 中收到错误消息。它告诉不兼容的类型。

必填com.google.android.gms.map.GoogleMap

找到void how can I solve this issue ?

我也试过getActivity.getApplicationContext(),但也没有用! 这里我提供代码请检查它

 public class MapsActivity extends Fragment implements
    OnMapReadyCallback,
    View.OnClickListener ,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

private GoogleMap map;
private MapView mapView;
private boolean mapsSupported = true;
private GoogleApiClient mGoogleApiClient;
MapFragment mapFragment;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    MapsInitializer.initialize(getActivity());

    if (mapView != null) {
        mapView.onCreate(savedInstanceState);
    }
    initializeMap();
}
private void initializeMap() {
    SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
            .findFragmentById(R.id.map);
    //mapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
    if (map == null) {
        **map = mapFragment.getMapAsync(this);**
    }
    // Enable Zoom
    map.getUiSettings().setZoomGesturesEnabled(true);
    //set Map TYPE
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    //enable Current location Button
    map.setMyLocationEnabled(true);
    LocationManager locationManager = (LocationManager)getActivity().getSystemService(getActivity().LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);
    if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    Location location = locationManager.getLastKnownLocation(bestProvider);
    if (location != null) {
        onLocationChanged(location);
    }
    locationManager.requestLocationUpdates(bestProvider, 2000, 0, this);
}

地图.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"
tools:layout="@layout/abc_action_menu_layout"/>

<Button

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Nearby Resturants"
    android:id="@+id/btn_rest"
    android:layout_gravity="bottom" />


   </RelativeLayout>

【问题讨论】:

  • 你可以使用 getMapAsync(getActivity());
  • @pulkit 不工作
  • 要使getMapAsync() 工作,R.id.map 必须是MapFragment 元素。但它是一个普通的 Fragment
  • 您的布局中有 MapFragment 吗?如果没有 MapFragment 布局,您的代码将始终崩溃。添加 try catch 块并不是一个实际的解决方案。

标签: android google-maps fragment


【解决方案1】:

Fragment中调用你的代码onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View viewOBJ= inflater.inflate(R.layout.xxx, null, false);

     SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
     mapFragment.getMapAsync(this);
     return viewOBJ;
}

更多信息请阅读getMapAsync() in Fragment

【讨论】:

    【解决方案2】:

    试试这个代码

    try {
            if (map == null) {
                ((MapView) getView().findViewById(R.id.map)).getMapAsync(this);
    
            }
        }catch (NullPointerException e){
            e.printStackTrace();
        }
    

    希望这会有所帮助!

    【讨论】:

    • 很高兴知道!请将其标记为已接受(答案左侧的勾号),以便将来帮助其他开发人员。
    【解决方案3】:

    您缺少的是 getMapAsync 是一个异步函数。这意味着它不会立即返回地图对象。所以你不能只写map = mapFragment.getMapAsync();

    相反,您必须这样做:

    private void initializeMap() {
      SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
            .findFragmentById(R.id.map);
      if (map == null) {
        mapFragment.getMapAsync(this);
      }
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
    
        // Enable Zoom
        map.getUiSettings().setZoomGesturesEnabled(true);
        //set Map TYPE
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        //enable Current location Button
        map.setMyLocationEnabled(true);
    
        //... rest of your map related code.
    }
    

    这里,onMapReady 在地图对象可用时被调用,因此您的其余初始化代码将转到那里。您还必须实现OnMapReadyCallback,但我看到您已经这样做了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 2020-10-16
      相关资源
      最近更新 更多