gpt4 book ai didi

java - 尝试使用谷歌地图 API v2 获取经度和纬度时显示错误

转载 作者:行者123 更新时间:2023-12-02 05:36:25 25 4
gpt4 key购买 nike

我正在尝试使用谷歌地图 API v2 获取纬度和经度。因此,首先将库添加到我的项目中 - appCompact_v7 和 google-play0service_lib ,如下图链接所示(抱歉,由于声誉原因,无法发布图片):

<强> Eclipse project screen shot

代码如下:

AndroidManifest.xml:

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

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />


<permission
android:name="com.example.testmapact2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission
android:name="com.example.testmapact.permission.MAPS_RECEIVE" />

<!-- set other permissions -->
<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" />
<uses-permission
android:name=
"com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Maps API version 2 requires OpenGL ES version 2 -->
<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=".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="AIzaSxAZYjSWRbcxyZkCf35Fi75cLxxxaVHeQbw" />
</application>
</manifest>

布局:

<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="${relativePackage}.${activityClass}" >

<TextView
android:id="@+id/TextLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="68dp"
android:text="lat" />

<TextView
android:id="@+id/TextLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextLat"
android:layout_below="@+id/TextLat"
android:layout_marginTop="52dp"
android:text="lon" />
</RelativeLayout>

MainAcivity.java:

public class MainActivity extends Activity 
implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener
{
TextView txtLat, txtLong;
LocationClient locClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtLat = (TextView)findViewById(R.id.TextLat);
txtLong = (TextView)findViewById(R.id.TextLong);

locClient = new LocationClient(this, this, this);

gpsChk();
}

@Override
protected void onStart()
{ super.onStart();
locClient.connect(); }

@Override
protected void onStop()
{ locClient.disconnect();
super.onStop();}

@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub }

@Override
public void onConnected(Bundle connectionHint) {
Location location = locClient.getLastLocation();

if(location!=null)
{
txtLat.setText(String.valueOf(location.getLatitude()));
txtLong.setText(String.valueOf(location.getLongitude()));
}
}

@Override
public void onDisconnected() {
// TODO Auto-generated method stub }

private void gpsChk()
{
// if GPS is not enabled, start GPS settings activity
LocationManager locationManager =
(LocationManager) getSystemService(LOCATION_SERVICE);

if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Toast.makeText(this, "Please activate GPS settings",
Toast.LENGTH_LONG).show();
Intent intent =
new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
}

它没有显示运行时错误。 来自 logcat 的错误日志:

07-24 11:23:34.259: E/AndroidRuntime(4433): FATAL EXCEPTION: main
07-24 11:23:34.259: E/AndroidRuntime(4433): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testmapact2/com.example.testmapact2.MainActivity}: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 5077000 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" />
07-24 11:23:34.259: E/AndroidRuntime(4433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
07-24 11:23:34.259: E/AndroidRuntime(4433): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
07-24 11:23:34.259: E/AndroidRuntime(4433): at android.app.ActivityThread.access$600(ActivityThread.java:162)
07-24 11:23:34.259: E/AndroidRuntime(4433): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
07-24 11:23:34.259: E/AndroidRuntime(4433): at android.os.Handler.dispatchMessage(Handler.java:107)
07-24 11:23:34.259: E/AndroidRuntime(4433): at android.os.Looper.loop(Looper.java:194)
07-24 11:23:34.259: E/AndroidRuntime(4433): at android.app.ActivityThread.main(ActivityThread.java:5371)
07-24 11:23:34.259: E/AndroidRuntime(4433): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 11:23:34.259: E/AndroidRuntime(4433): at java.lang.reflect.Method.invoke(Method.java:525)
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-24 11:23:34.259: E/AndroidRuntime(4433): at dalvik.system.NativeStart.main(Native Method)
07-24 11:23:34.259: E/AndroidRuntime(4433): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 5077000 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" />
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.google.android.gms.common.GooglePlayServicesUtil.x(Unknown Source)
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.google.android.gms.internal.hc.connect(Unknown Source)
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.google.android.gms.location.LocationClient.connect(Unknown Source)
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.example.testmapact2.MainActivity.onStart(MainActivity.java:42)
07-24 11:23:34.259: E/AndroidRuntime(4433): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1170)
07-24 11:23:34.259: E/AndroidRuntime(4433): at android.app.Activity.performStart(Activity.java:5132)
07-24 11:23:34.259: E/AndroidRuntime(4433): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
07-24 11:23:34.259: E/AndroidRuntime(4433): ... 11 more

我正在尝试在我的 Android 手机上运行该应用程序,其中 google map 正在运行并且 GPS 也已打开。但仍然显示错误,并且应用程序通过给出一条消息停止:不幸的是,应用程序已停止。

请帮帮我。 :-)

最佳答案

你logcat明确地说:

 Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 5077000 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" />
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.google.android.gms.common.GooglePlayServicesUtil.x(Unknown Source)
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.google.android.gms.internal.hc.connect(Unknown Source)
07-24 11:23:34.259: E/AndroidRuntime(4433): at com.google.android.gms.location.LocationClient.connect(Unknown Source)

您应该将 Google play 服务版本添加为 <meta-data>标签 <application>标签在你的 manifest.xml

  <meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

还添加以下permissions在你的manifest.xml

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

关于java - 尝试使用谷歌地图 API v2 获取经度和纬度时显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24926529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com