【问题标题】:Marker's not showing: Google Map Android标记未显示:Google Map Android
【发布时间】:2016-04-06 13:35:54
【问题描述】:

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

我正在关注specification,所以我不知道它为什么不起作用。 (marker和相机的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)));


}

}

【问题讨论】:

  • 我只是隐藏了问题的坐标。但我把它放在我设置相机的同一个地方。 (这是一个有效的坐标)

标签: android google-maps


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-02
    • 2012-04-10
    • 2014-10-20
    • 1970-01-01
    • 2014-02-23
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多