本文介绍了GetMap() 在 Android 上使用 Google Maps API v2 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用 Google Maps API V2 时遇到了一些问题.我尝试了很多教程并搜索了很多答案(包括堆栈溢出),但我发现的所有内容都不起作用.我可以用xml标签绘制地图.没问题,它有效.但是当我尝试在
MainActivity.java
中获取地图时 getMap()
返回 null,我不知道为什么.
I have some problems with Google Maps API V2. I have tried many tutorials and searched many answers (include stack overflow) but all I have found was not work. I can draw a map with xml tag <fragment>
. No problem, it works. But when I try to get the map in MainActivity.java
getMap()
return null and I don't know why.
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 KEY
Add your 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>
这篇关于GetMap() 在 Android 上使用 Google Maps API v2 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!