我想使用扩展 fragment 在android fragment 中制作 map
这是我的代码: map 不出现,系统强制关闭
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.gmaps, container, false);
SupportMapFragment mapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager() .findFragmentById(R.id.map));
mapFragment.getMapAsync((OnMapReadyCallback) this.getActivity());
btnFindPath = (Button) view.findViewById(R.id.btnFindPath);
etOrigin = (EditText) view.findViewById(R.id.etOrigin);
etDestination = (EditText) view.findViewById(R.id.etDestination);
btnFindPath.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequest();
}
});
return view;
请帮帮我,谢谢
最佳答案
你可以试试下面的代码:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i("onActivityCreated", "onActivityCreated");
if(supportMapFragment==null){
supportMapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
loadMapAtPosition(googleMap, CAMERA_INITIAL_LATITUDE, CAMERA_INITIAL_LONGITUDE);
}
});
}
.....
private void loadMapAtPosition(GoogleMap googleMap, double latitude, double longitude){ googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 6.0f)); }
关于java - 如何在 Fragment 中使用 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37981869/