Reputation: 49
I have some rough code for my project, and I am trying to get the GPS location of the user when a button is pressed and I want the location to be a GeoPoint that I can then pass to a method.
The method is for an Overlay class which draws the Overlay that point. I was using some code from Stack Overflow question How can I obtain the current GPS location?. I have the program set up so that the GeoPoint is added to an array and then it is retreived from that array and added to the method. Is there is a better way of doing this or is there any way of using the point without putting it in an array?
This is the button where the Overlay is added and the GeoPoint is gotten from the array:
Button startBtn = (Button) findViewById(R.id.startBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
getLocation();
tempOverlay = (MapOverlay) listOfOverlays.get(arrayNumber);
tempOverlay.addPoint(points.get(pointCount));
mapView.postInvalidate();
}
}
});
This is the getLocation() method which creates the locationManager and requestUpdates.
public void getLocation()
{
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
System.out.println("Get Location Called");
}
This is the onLocationChanged
method inside the MyLocationListener
class which gets the updates and once I have a location I stop the request for updates.
@Override
public void onLocationChanged(Location loc)
{
if(loc != null)
{
Toast.makeText(getBaseContext(),"LocationChanged : Lat: " + loc.getLatitude()+ " Lng: " + loc.getLongitude(),
Toast.LENGTH_SHORT).show();
}
p = new GeoPoint((int) (loc.getLatitude() * 1E6),(int) (loc.getLongitude() * 1E6));
points.add(p);
System.out.println("Point Added to Array");
LBServicesActivity.this.lm.removeUpdates(this);
mc.animateTo(p);
mc.setZoom(18);
}
How would I pass that GeoPoint to the method in the buttonListener without adding it to an array?
Upvotes: 0
Views: 3219
Reputation: 532
Create a GPS class that gets updated when the location is changed, kind of like this:
public class GPSData {
private static double latitude;
/**
* @return the latitude
*/
public static double getLatitude() {
return latitude;
}
/**
* @param latitude the latitude to set
*/
public static void setLatitude(double l) {
latitude = l;
}
private static double longitude;
/**
* @return the longitude
*/
public static double getLongitude() {
return longitude;
}
/**
* @param d the longitude to set
*/
public static void setLongitude(double d) {
longitude = d;
}
}
public static class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
setAccuracy(location.getAccuracy());
setLongitude(location.getLongitude());
setLatitude(location.getLatitude());
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Then when the user clicks the button access the values of that GPS class
Upvotes: 1