Rashedul
Rashedul

Reputation: 121

Showing error while trying to get longititude and latitude using google maps api v2

I am trying to just get the latitude and longitude using google maps api v2. So, first added the libraries to my project- appCompact_v7 and google-play0service_lib which is shown in below image link(sorry cant post image, due to reputation):

Eclipse project screen shot

And the Codes are as below:

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>

Layout:

<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);
    }
 }
}

Its not showing runtime error. Error log from 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

I am trying to run the app on my android phone, where google map is running and GPS is on too. But still showing error and application stops by giving a message : unfortunately the application has stoped.

Help me out please. :-)

Upvotes: 0

Views: 95

Answers (3)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75798

Please Add this in your Manifest section .

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

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

You are misisng a meta tag in your manifest file which is a child of application tag

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

You don't require

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

Also require

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

If you want to display map read the docs. I don't see a MapFragment anywhere in your layout posted

https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2

Upvotes: 0

M D
M D

Reputation: 47817

You logcat clearly said:

 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) 

You should add Google play service version as <meta-data> tag under <application> tag in your manifest.xml

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

also add below permissions in your manifest.xml

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

Upvotes: 1

Related Questions