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?
Create a GPS class that gets updated when the location is changed, kind of like this:
Then when the user clicks the button access the values of that GPS class