谷歌地图视图因扩展片段类而损坏



你好,我是安卓新手。 此代码适用于扩展活动类,但我将此类与扩展一起使用片段,它不起作用。我添加了清单中的所有权限。我将这个片段类与底部导航菜单一起使用。请帮助我。

这是我的片段类(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);

相关内容

  • 没有找到相关文章

最新更新