我正在尝试在片段中创建一个简单的MapView。这是我的代码:
public class MapFragment extends Fragment {
MapView m;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflate and return the layout
View v = inflater.inflate(R.layout.fragment_map, container,
false);
m = (MapView) v.findViewById(R.id.mapView);
m.onCreate(savedInstanceState);
m.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(36.16,-86.78) , 14);
googleMap.moveCamera(cameraUpdate);
}
});
return v;
}}
但是,当我运行应用程序时,相机位置仍然始终停留在默认位置:全局视图以非洲为中心。任何帮助,将不胜感激。此外,当我单击返回加载此片段的选项卡时,它总是返回到默认视图,而不是我已更改的视图。
答案 0 :(得分:0)
试试这个:
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(36.16,-86.78)).zoom(14).build();
CameraUpdate update = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.moveCamera(update);