繁体   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