1

Google マップにマーカーが表示されません。マーカーを追加するために使用される私のコードスニペットは次のとおりです

googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();

// check if map is created successfully or not
if (googleMap == null) {
  return ;
}

try {
  googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(0, 0))
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
} catch (Exception e) {
  Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
}

ここに私のxmlレイアウトがあります

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

マップと実際の画面に表示されているので、すべて問題ないようです。MapFragment から拡張されていないフラグメント (サポート lib) 内でこのフラグメントを使用していることに注意してください。何か案は?

4

2 に答える 2

0

この行を置き換えます

googleMap = ((MapFragment)getActivity().getFragmentManager().findFragmentById(R.id.map))
.getMap();

これで

googleMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
于 2015-02-23T21:13:50.427 に答える