gpt4 book ai didi

android - 无法解决Fragment中的getMapAsync问题

转载 作者:行者123 更新时间:2023-11-29 15:02:31 24 4
gpt4 key购买 nike

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

必需:com.google.android.gms.map.GoogleMap

发现:void 如何解决此问题?

我也尝试了 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);
}

map .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>

最佳答案

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

关于android - 无法解决Fragment中的getMapAsync问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42266281/

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