android - 谷歌地图 Android API v2 : GoogleMap is null

标签 android android-mapview android-maps-v2

我正在尝试使用 MapView 类来显示 GoogleMap,但没有成功,因为大多数代码示例都使用了我不想要的 MapFragment。

我正在使用 Google Maps Android API v2。

起初,只是为了测试 here from Google's example ,我设法获得了要显示的典型法线贴图。

public class POnlineMapView extends Activity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.online_map_activity);
    }
}

上面的代码运行良好,表明一切都已正确设置。

我现在正尝试使用 MapView 类来操作中心点等显示设置,但似乎每次尝试获取 GoogleMap 对象时我都获取了一个空对象。为什么会这样?

public class POnlineMapView extends Activity {

    private MapView myMapView;
    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myMapView = new MapView(getApplicationContext());
        Bundle b = getIntent().getExtras();
        double longitude = b.getDouble("longitude");
        double latitude = b.getDouble("latitude");

        setContentView(R.layout.online_map_activity);
        map = myMapView.getMap();

        CameraUpdate center= CameraUpdateFactory.newLatLng(new LatLng(latitude,longitude));
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(17);

        map.moveCamera(center); //this gives a NullPointerException, probably due to the myMapView.getMap() method?
        map.animateCamera(zoom);    
    }
}

最佳答案

关于android - 谷歌地图 Android API v2 : GoogleMap is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14518024/

相关文章:

android - 在android中从内部或外部存储器保存和检索图像

Android - 如果 View 不可见,则在 RecyclerViewAdapter 中添加 MapFragment 失败

android - ClusterManager 为谷歌地图 v2 utils 的 ClusterItems 标记提供多种样式

Android旋转位图而不制作副本

java - 如何从 View 获取 ConstraintLayout?

java - Android jni 和 opencv 运行时无法加载库

android - 在 com.google.android.maps.MapView 中禁用平移/缩放

java - 将 "findViewById"用于 map View 时出错

android - 如何在android map api V2中为标记设置动画?

android - 为什么我的位置不断变化,是我的代码不正确吗?