Marker没有显示:Google Map Android

时间:2016-04-06 13:35:55

标签: android google-maps

在我的项目中,我以编程方式在片段上加载我的地​​图。但是,在此地图上添加标记无效。没有显示错误,但标记也不在地图上。

我正在关注specification所以我不知道为什么它不起作用。 (标记和相机的Lat和Lng是相同的)

我的片段代码

public class MainMapFragment extends Fragment implements OnMapReadyCallback {
SupportMapFragment mMapFragment;
static final LatLng LIBRARY = new LatLng(Lat, Lng);
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    return inflater.inflate(R.layout.main_map_layout,container,false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    //Set the initial stage of the map
    //It is set on code (not on the xml) because the map is created programmatically
    GoogleMapOptions options = new GoogleMapOptions();
    CameraPosition ufv_position = new CameraPosition.Builder()
            .target(new LatLng(Lat,Lng))
            .zoom(15)
            .tilt(0)
            .bearing(40)
            .build();

    options.mapType(GoogleMap.MAP_TYPE_NORMAL)
            .compassEnabled(false)
            .rotateGesturesEnabled(false)
            .tiltGesturesEnabled(false)
            .camera(ufv_position);
    //Load the map with the given options
    mMapFragment = SupportMapFragment.newInstance(options);
    FragmentTransaction fragmentTransaction =
            getChildFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.map, mMapFragment);
    fragmentTransaction.commit();
}

@Override
public void onMapReady(GoogleMap googleMap) {

    googleMap.addMarker(new MarkerOptions()
            .position(LIBRARY)
            .title("Library")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.book)));


}

}

1 个答案:

答案 0 :(得分:0)

添加mMapFragment.getMapAsync(this);,以便调用onMapReady方法