我尝试按照文档使用谷歌地图API,但它不起作用。
我的清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gogplemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.example.gogplemap.GOOGLEMAP"
android:protectionLevel="signature"
></permission>
<uses-permission android:name="com.example.gogplemap.GoogleMap"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.gogplemap.GoogleMap"
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.gms.maps.MapFragment"
android:value="AIzaSyBPQPMrlJk-Ft48z0CWvpWBRcLyrIJJ4X4"/>
</application>
</manifest>
我的布局activity_google_map.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
我在GoogleMap.java中的方法setcontentView
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_map);
}
非常感谢
答案 0 :(得分:0)
尝试这样:它会对你有用!!
布局:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
的活动:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidmapview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<permission
android:name="com.example.androidmapview.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.androidmapview.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidmapview.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="*********************************uw"></meta-data>
</application>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
</manifest>
答案 1 :(得分:0)
到目前为止,我看到的错误很少:
1。首先,如果您的min SDK是V8,那么您必须使用SupportMapFragment
和FragmentActivity
个对象,而不是MapFragment
和{{1}对象。当然,您还需要将Activity
库的引用添加到项目中,因此对于映射XML文件,您应该写:
google_support-v4
2。其次,看起来你用错误的方式定义了你的键,而不是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
你应该写:
<meta-data
android:name="com.google.android.gms.maps.MapFragment"
android:value="AIzaSyBPQPMrlJk-Ft48z0CWvpWBRcLyrIJJ4X4"/>
3. 接下来,您使用的权限不正确,而不是:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBPQPMrlJk-Ft48z0CWvpWBRcLyrIJJ4X4" />
添加:
<permission
android:name="com.example.gogplemap.GOOGLEMAP"
android:protectionLevel="signature"
></permission>
<uses-permission android:name="com.example.gogplemap.GoogleMap"/>
不要忘记在前两个权限中放置应用程序包而不是“your.application.package”字符串,<permission android:name="your.application.package.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-permission android:name="your.application.package.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
。
您可以从我撰写的关于此主题的博客文章中获取更多信息: