android - 谷歌地图视图因扩展片段类而损坏
问题描述
你好,我是安卓新手。此代码适用于扩展 Activity 类,但我将此类与扩展片段一起使用,它不起作用。我在清单中添加了所有权限。我将此 Fragment 类与底部导航菜单一起使用。请帮我。
这是我的片段类(LoctionFragment.java)
public class LoctionFragment extends Fragment implements OnMapReadyCallback, LocationListener{
GoogleMap map;
LocationManager locationManager;
TextView viewLocation;
public LoctionFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.location_layout, container, false);
viewLocation = v.findViewById(R.id.viewLocation);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, (LocationListener) this);
return v;
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
}
@Override
public void onLocationChanged(Location location) {
map.clear();
LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(currentLocation);
markerOptions.title("MY LOCATION");
map.addMarker(markerOptions);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 17.0f));
if (location != null) {
double latti = location.getLatitude();
double longi = location.getLongitude();
viewLocation.setText("Current Location is :" + latti + "," + longi);
} else {
viewLocation.setText("Unable to find current location . Try again later");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
这是布局 xml 地图视图 (location_layout.xml)
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="383dp"
android:layout_x="1dp"
android:layout_y="39dp" />
解决方案
你把 的MapFragment
作为子片段LocationFragment
,所以你需要使用getChildFragmentManager
:
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
推荐阅读
- swiftui - 如何在 SwiftUI 中的视图中调用方法
- excel - excel公式本地化
- javascript - 如何在将 .ticks() 应用于我的 x 刻度时解决 d3 js 中的此错误
- linux - 如何帮助可执行文件找到共享库?
- git - 不小心将一个大文件提交到本地 git。现在我必须删除它才能推送到远程
- r - 在 R 中向直方图添加正态曲线时遇到问题
- nativescript - Nativescript 6.3.0 在构建和运行命令上挂起
- ocr - OCR using Tesseract simple task failing
- javascript - 根据数组长度发送消息
- lisp - "Heap exhausted, game over" message in wxMaxima - Does ccl will work for me?