【问题标题】:GetMap() return null on Android with Google Maps API v2GetMap() 在 Android 上使用 Google Maps API v2 返回 null
【发布时间】:2014-08-18 06:40:16
【问题描述】:

我在使用 Google Maps API V2 时遇到了一些问题。我已经尝试了很多教程并搜索了很多答案(包括堆栈溢出),但我发现的都是无效的。我可以用 xml 标签<fragment> 绘制地图。没问题,它有效。但是当我尝试在MainActivity.java getMap() 中获取地图时,返回null,我不知道为什么。

MainActivity.java

package com.example.testmaps;

import android.app.Activity;
import android.os.Bundle;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {
 private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    //HERE mMap IS NULL
    mMap.addMarker(new MarkerOptions()
            .position(new LatLng(10, 10))
            .title("Hello world"));
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <fragment 
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout> 

Manifest.xml

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testmaps.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>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="my_key" />
        <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    </application>

</manifest>

【问题讨论】:

  • 您是否记得将 API 密钥添加到您的 AndroidManifest.xml 中?

标签: java android xml api google-maps


【解决方案1】:

添加您的 API KEY

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyA_YwwmN2h21nVVzuiQcpQsipZoAlaix7Z" />
     // place your API KEY

你可以像下面这样写

 android.support.v4.app.FragmentManager myFM = getSupportFragmentManager();
 final SupportMapFragment myMAPF = (SupportMapFragment) myFM.findFragmentById(R.id.mapView);
 googleMap = myMAPF.getMap();

我的布局文件是这样的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_horizontal_margin" >

<fragment
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true" />

<Button
    android:padding="5dp"
    android:layout_margin="10dp"
    android:background="@drawable/button"
    android:id="@+id/refreshMap"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="@string/button_refresh_map" />

 </RelativeLayout>

【讨论】:

  • 我有这个元数据。请参阅标签下方的 manifest.xml :)
  • 老兄添加您的 API KEY 代替 ACTUAL_KEY。像这样
  • 谢谢,我知道 x) 我刚刚用“my_key”替换了我的实际密钥。我的代码中有这个:
  • 最终 SupportMapFragment myMAPF = (SupportMapFragment) myFM.findFragmentById(R.id.mapView); googleMap = myMAPF.getMap();我正在使用支持地图。所以上面的代码对我有用。您可以使用最终 MapFragment myMap = (MapFragment) myFM.findFragmentById(R.id.mapView); googleMap = myMap .getMap();
  • 看来可行!非常感谢 ! :p 我尝试过支持 framgent,但“myMAPF”不是最终版本。可能是这样
猜你喜欢
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多