Geting current Lat and Lon

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

Geting current Lat and Lon

Postby mcdemon » Wed Jul 22, 2009 11:01 pm

I have been looking all over this site and google for a simple way to get Latitude and Longitude coordinates for my phones current location. There are many code examples out there that embed the Lat and lon in in maps and such. What I want is different, I just what to set the phones lat and lon to strings accessible by the main activity.

Can someone give me some advice on using the LocationManager and LocationListener classes. An application as simple as pressing a button to update current lat and lon would be great. I just don't fully understand the connection between making your own LocationListener and getting the Lat and lon to where you can use them.

Any help/advice would be great.
mcdemon
Freshman
Freshman
 
Posts: 4
Joined: Fri Jul 17, 2009 6:43 pm

Top

Postby mcdemon » Wed Jul 22, 2009 11:27 pm

I finally got this application the work, from http://www.devx.com/wireless/Article/39239

Here is the source code:

import java.util.List;

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.telephony.TelephonyManager;
import android.widget.TextView;
import android.widget.Toast;

public class GpsTest extends Activity
{
private LocationManager lm;
private LocationListener locationListener;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//---use the LocationManager class to obtain GPS locations---
lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);

locationListener = new MyLocationListener();

lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
}

private class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
Toast.makeText(getBaseContext(),
"Location changed : Lat: " + loc.getLatitude() +
" Lng: " + loc.getLongitude(),
Toast.LENGTH_SHORT).show();
}
}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
}

}


This code uses a toast widget to show the lat and lon. Now the stupid question, How do I get these coordinates into lets say a simple TextView? They are currently being gotten from the LocationListener class. I would be a great help to be able to save them as Strings in the main activity.

Please let me know of any ideas or suggestions you guys may have.

Thanks
mcdemon
Freshman
Freshman
 
Posts: 4
Joined: Fri Jul 17, 2009 6:43 pm

Postby thanatos » Thu Jul 23, 2009 7:23 pm

Use a container object in your Activity to store the received from the GPS.

Send this object to your MyLocationListener class and use it to update the changes.

Use a Thread to update your Views reading the updated container Object.

I hope it help you
thanatos
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Jul 23, 2009 6:55 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 4 guests