【发布时间】:2017-02-16 17:11:43
【问题描述】:
我是 Google Maps Android API 的新手。我知道这个问题已经发布,但它们很旧,我指的是新的 API 版本。 我正在开发一个导航抽屉,其中一个片段专用于地图。 我只能看到地图,但没有标记,没有缩放。听起来地图无法正确加载。
公共类 MapsFragment 扩展 Fragment 实现 OnMapReadyCallback{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_maps, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment mapFragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(44.797283, 20.460663);
googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
这是名为 fragment_maps.xml 的 xml
<FrameLayout 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"
tools:context="com.app.myapp.view.MapsFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
谢谢
【问题讨论】:
标签: android google-maps android-fragments maps google-maps-markers