Ho cambiato qualcosina e commentato (non ho testato niente, l'ho modificato al volo con JCreator):
codice:
public class GPS extends MapActivity
{
private LocationManager lm;
private LocationListener locationListener;
private MapView mapView;
private MapController mc;
//meglio usare delle variabili
private double newlat, newlng, maxdist;
/** 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);
mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();
//il codice qui sotto basta eseguirlo una volta sola
//e non ogni volta che viene chiamata onLocationChanged()
Resources res=getResources();
//ti consiglio di usare le preferences qui, al posto delle resources
newlat=Double.parseDouble(res.getString(R.string.Lat));
newlng=Double.parseDouble(res.getString(R.string.Lng));
maxdist=Double.parseDouble(res.getString(R.string.MaxDist);
}
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();*/
GeoPoint p = new GeoPoint(
(int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
Location control=new Location(loc);
control.setLatitude(newlat);
control.setLongitude(newlng);
double dist=loc.distanceTo(control); //ho messo double qui, non e' bello mischiare float e double
if(dist>maxdist) //controllo se mi sono allontanato troppo
{
//qui toglierei la creazione di String destination e text
/*SmsManager m = SmsManager.getDefault();
String destination = "numeroscelto";
String text = "Warning!The objective is leaving the security area!Actual position:Lat:" + loc.getLatitude() + " Lng:" + loc.getLongitude() +"";
m.sendTextMessage(destination, null, text, null, null);*/
Toast.makeText(getBaseContext(),
"Area violated,the Master user has been informed of your effraction!",
Toast.LENGTH_SHORT).show();
}
mc.animateTo(p);
mc.setZoom(16);
mapView.invalidate();
}
}
}
Comunque alla fine andava bene, però così è più pulito e ordinato.
Un piccolo appunto: quando non serve, cerca di non usare le classi wrapper (Float, Double, Integer), ma usa i tipi base (float, double, int).