簡體   English   中英

Android,Google Maps Android API,設置位置和地圖類型

[英]Android, Google Maps Android api, setting location and map type

我是Android的新手,因此嘗試讓我的應用程序在按鈕單擊時打開一個新活動,該活動使用Google Maps Android API顯示地圖。

我能夠獲得標准的地圖以在單擊按鈕時呈現,而沒有問題,但是當我嘗試添加位置和地圖類型時,我的應用因此錯誤而崩潰。

“由於:java.lang.NullPointerException:嘗試在空對象引用上調用虛擬方法'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()'”

這是我的活動指向的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 map docs,不贊成使用getMap()方法。 由於,它並不總是返回非空值。 它可以多次為null: https : //developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment.html#getMap()

使用getMapAsync()方法,此方法具有回調機制。 地圖准備好后,它將回叫。 因此,它總是返回非空值。

完整的代碼:

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));

    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM