Skip to content Skip to sidebar Skip to footer

Android - Using Locationmanager Does Not Give A Geo Fix

I am trying to get the GPS location of my G1 using the following code In Activity MyLocationListener myListener = new MyLocationListener(); LocationManager myManager = (LocationMan

Solution 1:

Firstly, in your activity, where you're calling requestLocationUpdates(), the second argument is the minimum time, not the scheduled time. You're currently asking for an update AT MOST every 2 seconds. It won't actually report anything until it can... sometimes it takes a little while.

If your GPS is unresponsive/slow elsewhere as well (like in Maps) try restarting the phone (some people claim you have to turn off the phone then pull out the battery to completely de-energized the GPS radio). Hopefully this will bring it back up to it's normally responsive state.

Also, it shouldn't matter, but you said you were using the 1.5 SDK, but you didn't mention if you were compiling against it (as opposed to 1.1). I only bring this up because the GPS works better/faster in Cupcake, so I was wondering what your phone was actually running.

UPDATE:

I wrote up a little test on my own to see if I could duplicate your problem. It consists of just three user-generated files and I tested it with SDK 1.1 and 1.5

In the main Activity (I simply used Main.java):

package org.example.LocationTest;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

publicclassMainextendsActivityimplementsLocationListener{
    privateLocationManager myManager;
    privateTextView tv;


    /********************************************************************** 
     * Activity overrides below 
     **********************************************************************/@OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // get a handle to our TextView so we can write to it later
        tv = (TextView) findViewById(R.id.TextView01);

        // set up the LocationManager
        myManager = (LocationManager) getSystemService(LOCATION_SERVICE);   
    }

    @OverrideprotectedvoidonDestroy() {
        stopListening();
        super.onDestroy();
    }

    @OverrideprotectedvoidonPause() {
        stopListening();
        super.onPause();
    }

    @OverrideprotectedvoidonResume() {
        startListening();
        super.onResume();
    }



    /**********************************************************************
     * helpers for starting/stopping monitoring of GPS changes below 
     **********************************************************************/privatevoidstartListening() {
        myManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            this
        );
    }

    privatevoidstopListening() {
        if (myManager != null)
            myManager.removeUpdates(this);
    }




    /**********************************************************************
     * LocationListener overrides below 
     **********************************************************************/@OverridepublicvoidonLocationChanged(Location location) {
        // we got new location info. lets display it in the textviewString s = "";
        s += "Time: "        + location.getTime() + "\n";
        s += "\tLatitude:  " + location.getLatitude()  + "\n";
        s += "\tLongitude: " + location.getLongitude() + "\n";
        s += "\tAccuracy:  " + location.getAccuracy()  + "\n";

        tv.setText(s);
    }    

    @OverridepublicvoidonProviderDisabled(String provider) {}    

    @OverridepublicvoidonProviderEnabled(String provider) {}    

    @OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {}
}

In your "main" layout file (I used main.xml):

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:id="@+id/TextView01"android:text="Waiting for location information..." 
    /></LinearLayout>

Then, in your AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="org.example.LocationTest"android:versionCode="1"android:versionName="1.0"><applicationandroid:icon="@drawable/icon"android:label="@string/app_name"><activityandroid:name=".Main"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application><uses-sdkandroid:minSdkVersion="3" /><uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /></manifest>

You should be able to compile/run this just fine. If, using this, you still can't get a fix it's not a software problem. You either have a bad GPS or it just can't get a lock on any satellites.

You can download the source here.

Solution 2:

  1. Is your GPS enabled in your device settings? I presume so since you are getting the GPS icon, but I figured I'd check.
  2. onProviderDisabled() will only get called if you disable GPS in the settings, and so that code is unlikely to get invoked.
  3. Have you tried a distance of 0 instead of 2000?
  4. Are you sure that your call to requestLocationUpdates() is actually getting executed?
  5. Does your log show any errors? You can examine your log via adb logcat, DDMS, or the DDMS perspective in Eclipse.
  6. Have you tried any existing code, published, that should work? For example, you can grab the source code to my Android book, where the Weather and WeatherPlus samples show the use of LocationManager.

Solution 3:

Post a Comment for "Android - Using Locationmanager Does Not Give A Geo Fix"