onMapReady Marker不起作用

时间:2016-08-25 13:35:17

标签: android google-maps android-fragments

我有Fragment应该在Google Map

上显示特定位置
public class ContactFragment extends Fragment implements OnMapReadyCallback {
public ContactFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    FragmentManager fragment = getActivity().getSupportFragmentManager();

    final SupportMapFragment myMAPF = (SupportMapFragment) fragment.findFragmentById(R.id.map);

    return inflater.inflate(R.layout.fragment_contact, container, false);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    LatLng marker = new LatLng(34.877737,-1.326545);
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker,13));
    googleMap.addMarker(new MarkerOptions().position(marker));
}
}

在Fragment的XML文件中我有这个:

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    android:layout_below="@+id/contact_domiciliation"
    class="com.google.android.gms.maps.MapFragment"
    ></fragment>

然而,当我启动时点击打开此片段的按钮,我会得到整个地球的地图。

1 个答案:

答案 0 :(得分:0)

将这行代码添加到片段中:

myMAPF.getMapAsync(this);