谷歌地图v2的简单应用程序不工作 [英] Google Maps v2 Simple Application Not Working
问题描述
我下面本的教程。这里是我的所有相关文件: activity_main.xml
I am following this tutorial. Here are all my related files: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MainActivity.java
MainActivity.java
package org.barisakkurt.googlemapsv2;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
AndroidManifest.xml中
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.barisakkurt.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="org.barisakkurt.googlemapsv2.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="AIzaSyAC7y4bP-MjeJljm6Fr9Og1FKxNe9ZiFkY" />
</application>
</manifest>
我完成了教程直到标题的设置标记的。但是我收到以下错误。
I completed the tutorial until the title Placing a Marker. However I am getting the following error.
Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4030500 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
如果我写我的清单文件中的以下行:
If I write my manifest file the following lines:
<meta-data
android:name="com.google.android.gms.version"
android:value="4030500" />
这个时候我收到此错误:
this time I am getting this error:
Caused by: java.lang.RuntimeException: API key not found. Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
也是Android在开玩笑吗?这是为什么code崩溃?我有什么做的,跑那该死的简单code?为什么它是工作在本教程中,而不是在我的机器。这让我生气。
Is android kidding me? Why is that code crashes? What do i have to do to run that damn simple code? Why it is working in the tutorial and not on my machine. It annoyed me.
(顺便说一句,我使用的是Windows 7与真实的设备的Nexus 7和我复制一个version.xml也尝试下面的元数据。
(By the way, I am using Windows 7 with a real device Nexus 7 and I copied a version.xml tried also the following meta-data
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
和再次没有工作。最后,我当然搜查SO和谷歌看了看流行的教程),请大家帮帮我。
and again didn't work. Lastly, of course I searched SO and google looked at the popular tutorials) Please help me.
推荐答案
您需要有两个的元数据
字段。一个是需要一般的谷歌播放服务,第二个在上面使用地图。
You need to have two meta-data
fields. One is required for google play services in general, the second one on top for using maps.
例如,从示例应用程序代码段,你应该在 [Android的SDK] /extras/google/google_play_services/samples/maps/AndroidManifest.xml
Example snippet from the sample application you should have in [android-sdk]/extras/google/google_play_services/samples/maps/AndroidManifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/demo_title"
android:hardwareAccelerated="true">
<!-- ** You need to replace the key below with your own key. **
The example key below will not be accepted because it is not linked to the
certificate which you will use to sign this application.
See: https://developers.google.com/maps/documentation/android/start
for instructions on how to get your own key. -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCvGBr5in13NK2yYBR7lhXTtnxj3mrXQy4" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
这篇关于谷歌地图v2的简单应用程序不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!