gpt4 book ai didi

java - Google Map getMapAsync 无法在 fragment 中使用

转载 作者:行者123 更新时间:2023-11-30 00:32:14 29 4
gpt4 key购买 nike

我正在尝试在我的项目中实现 googleMaps,我只是添加了一个新 fragment 和以下代码。目前我可以在我的项目中显示 map ,但添加标记不起作用并且 mapFragment.getMapAsync(this) 给出以下错误消息。无法解析方法 'getMapAsync(testCodes.project2.MapFragment)

public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
// private MapView mapView;
private Boolean mapReady = false;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

public MapFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
MapFragment mapFragment = (MapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);//remember getMap() is deprecated!


// mapFragment.getMapAsync(this);
return view;
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-33.852, 151.211);

MarkerOptions markerOptions = new MarkerOptions().position(sydney).title("Random position");
googleMap.addMarker(markerOptions);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
checkPermission("Permission", 1, 1);
mMap.setMyLocationEnabled(true);
}


}

fragment 的 xml。

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

最佳答案

在 xml 文件中,您使用的是 class="com.google.android.gms.maps.SupportMapFragment",因此在 MapFragment.java 中您必须使用SupportMapFragment 而不是 MapFragment

改变这个:

MapFragment mapFragment = (MapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);//remember getMap() is deprecated!

到:

            SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);//remember getMap() is deprecated!

关于java - Google Map getMapAsync 无法在 fragment 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44098559/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com