Android, Google 地图 Android api, 设置位置和地图类型

Android, Google Maps Android api, setting location and map type

我是 Android 的新手,正在尝试让我的应用程序在单击按钮时打开一个新的 activity,它使用 Google 地图 Android 显示地图api.

我可以在单击按钮时毫无问题地获得标准地图,但是当我尝试添加位置和地图类型时,我的应用程序崩溃并出现此错误。

"Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference"

这是我的 Activity 指向的 xml 片段布局。

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment"
    />

这是我的 java 代码。

map1 = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1)).getMap();
        if (map1 == null)
            Toast.makeText(this, "No Maps", Toast.LENGTH_LONG).show();
        else
            map1.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            final LatLng LOCATION_1 = new LatLng(37.7576171,-122.5776844);
            CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(LOCATION_1)
                .zoom(17)
                .bearing(90)
                .tilt(30)
                .build();
      map1.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

我已经做了几个小时的研究,但不知所措。我想知道我的清单中是否可能缺少某些内容?

如有任何帮助,我们将不胜感激。

我的清单以防相关。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="...."
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity2">
        </activity>
        <activity
            android:name=".Activity3">
        </activity>


        <meta-data
            android:name="com.google.android.gms.version"
            android:value="8298000"
            tools:replace="android:value"/>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCCk5zREDbtWJD-tFru8xmRZow0ocVXIps"/>

    </application>


    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


</manifest>

首先,稍微修改一下代码,你在xml中输入MapFragment,你需要输入SupportMapFragment。

代码:

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>

其次,根据 google 地图文档,不推荐使用 getMap() 方法。因为,它并不总是 return 非空值。它可以多次为空: https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment.html#getMap()

使用getMapAsync()方法,该方法有回调机制。一旦地图准备好,它将回调。因此,它 return 始终是非空值。

完整代码:

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map1);
mapFragment.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {

        googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        final LatLng LOCATION_1 = new LatLng(37.7576171,-122.5776844);
        CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(LOCATION_1)
            .zoom(17)
            .bearing(90)
            .tilt(30)
            .build();
             googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

    }
});