user3374478
user3374478

Reputation: 11

unable to locate current gps position

I am constructing an Android application that is to return the current position of the device running the app. I have currently got the google map displaying on the screen but I am having an issue with the location listener, I put break points within it and they never be hit. I will include my MainActivity.java and androidMainfest.xml.

MainActivity.java

package com.example.androidgooglemap;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.GoogleMap;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.content.Context;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

public GoogleMap mMap;
LocationManager location;
public double latG;
public double lonG;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    location = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    location.getLastKnownLocation(Context.LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener(){

        @Override
        public void onLocationChanged(Location location) 
         {
                if (location != null) 
                {
                    latG = location.getLatitude();
                    lonG = location.getLongitude();
                    Toast.makeText(MainActivity.this,
                            latG + " " + lonG,
                            Toast.LENGTH_LONG).show();
                }
            }           
    };
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/* Request updates at startup */
}

AndroidManifest.xml

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

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="18" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

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

<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"/>
<!-- 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"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-library android:name="com.google.android.maps" />
    <activity
        android:name="com.example.androidgooglemap.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.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="API-KEY"/>
</application>

</manifest>

Some guidance needed. Thank you

Upvotes: 0

Views: 81

Answers (1)

user3099373
user3099373

Reputation:

You forgot to register the listener to the location service. Add this code after the declaration of locationListener variable.

if(location.isProviderEnabled("gps"))
        location.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
if(location.isProviderEnabled("network"))
        location.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

Upvotes: 2

Related Questions