getLastKnownLocation = null

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

getLastKnownLocation = null

Postby fedee » Sun Jan 11, 2009 12:28 am

Hi,

i am trying to develop an application and i need to have the location..

My code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Location loc;
  3.  
  4. LocationManager lm = (LocationManager)getSystemService(LOCATION_SERVICE);
  5.  
  6. loc = lm.getLastKnownLocation("network"); //OR getLastKnownLocation("gps");
  7.  
  8.  
  9.  
  10.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


But allways return NULL :'(, any ideas?

pd: other way to get country/state/city name ?

Thank you.
fedee
Freshman
Freshman
 
Posts: 4
Joined: Thu Jan 08, 2009 12:02 pm

Top

Postby arvchak » Mon Jan 12, 2009 10:19 am

Hi,

U have to add this line before the getlastKnownlocation

lm.requestLocationUpdates(this.myLocationManager.GPS_PROVIDER, 0, 0, this);

and also check if u have given permissions,

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


after u do this while u run the emulator u have to send the geo location manually or using the KML option from the DDMS
User avatar
arvchak
Developer
Developer
 
Posts: 37
Joined: Thu Dec 11, 2008 7:38 am

getLastKnownLocation example

Postby 0x1B » Tue Jan 13, 2009 1:57 am

Mine looks more like this--->

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);

provider = locationManager.getBestProvider(criteria,true);
if (provider != null) {
location = locationManager.getLastKnownLocation(provider);
/* TODO - adjust update times to minimize battery use. Can be changed as needed.
*
*/
locationManager.requestLocationUpdates(provider,
60000, // 1min
100, // 100m
locationListener);
}

Obviously you don't need to request location updates, but if you do then below is some code I use....


private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location newloc) {
// something to do here when location changes
// TODO get other datq (accuracy, velocity, altitude, etc...)
// write data to database
// add markers
location = newloc;

}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

and you need to remember to turn off/on the updating at onStart and onStop

@Override
public void onStop() {
super.onStop();
if (provider != null) {
locationManager.removeUpdates(locationListener);
}


@Override
public void onStart() {
super.onStart();

/* recheck which provider to use */
provider = locationManager.getBestProvider(criteria,true);
if (provider != null) {
location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider,
60000, // 1min
100, // 100m
locationListener);
}

}

Hope this helps.
________________
English: Fluent
Français: écrit, lu, parlé
Español: escrito, leído, hablado
0x1B
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 12, 2009 9:23 pm

Postby ashish » Fri Mar 20, 2009 6:43 pm

whether it is necessary to call request location update.............
ashish
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Fri Feb 20, 2009 8:09 am

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 6 guests