Running example:
package com.layout.test;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class Pruebas extends Activity {
LocationManager lm = null;
Location loc = null;
public static final String TAG = "LOQUERIO";
private LocationListener locationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
}
private void initActivity() {
loc = lm.getLastKnownLocation("gps");
if (loc == null) {
Log.e("mylocation", "failed to determine my location");
} else {
Log.i("mylocation", loc.toString());
TextView text = (TextView) findViewById(R.id.lblTest);
text.setText(loc.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, 27, 0, "GetLocation").setIcon(
android.R.drawable.ic_menu_add);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 27:
initActivity();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
// Called when the location has changed.
Log.d(TAG, "===>>> MinhaLocalizacaoListener
onLocationChanged");
if (loc != null) {
Toast.makeText(
getBaseContext(),
"Location changed : Lat: " +
loc.getLatitude()
+ " Lng: " +
loc.getLongitude(),
Toast.LENGTH_SHORT).show();
}
}
public void onProviderDisabled(String provider) {
// Called when the provider is disabled by the user.
Log.d(TAG, "===>>> MyLocationListener
onProviderDisabled PROVIDER:"
+ provider);
// Mapa.ehPraAtualizar = false;
}
public void onProviderEnabled(String provider) {
Log.d(TAG, "===>>> MyLocationListener onProviderEnabled
PROVIDER:"
+ provider);
// Called when the provider is enabled by the user.
}
public void onStatusChanged(String provider, int status, Bundle
extras) {
Log.d(TAG, "===>>> MyLocationListener onStatusChanged
STATUS:"
+ status + " PROVIDER:" + provider);
// Called when the provider status changes.
}
}
}
On Nov 21, 2:46 am, Shibbs <[EMAIL PROTECTED]> wrote:
> Use Geocoder, the newly class introduced in 1.0 SDK.
> Its helps in reverse geocodeing( i.e getting address from lat/lon)
>
> http://code.google.com/android/reference/android/location/Geocoder.html
>
> Thanks,
> Shibbs
>
> On Oct 17, 2:12 pm, Matthias <[EMAIL PROTECTED]> wrote:
>
> > Well, in other words, this whole location manager thing is pretty
> > broken in Android 1.0?
>
> > From what I've read and experienced myself so far, it's pretty much
> > like this:
>
> > 1) LocationManager cannot be subclassed anymore, instead you use the
> > debug bridge to send location fixes. Unfortunately, this thing is
> > broken in two ways: Firstly, it only works on systems with a US locale
> > (?? sounds odd, but I can definitely tell it does not work for me...),
> > and secondly, the proclaimed "geo" command from the tools/ folder
> > isn't even there...!
>
> > 2) In order for LocationManager.getLastKnownLocation() to actually
> > work, one has to register a listener, although this may not even what
> > the programmer needs -- I surely don't want it, I want to poll for
> > updates only on well-defined occasions. In fact, I believe this whole
> > location listener thing is a bad idea, because it will result in each
> > and every location-aware Android app running its own
> > ListenForLocationUpdatesService, in order to always have access to the
> > latest known location... yuck.
>
> > Is that correct so far? If so, I have to say I am pretty much
> > disappointed with the progress Android makes.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---