Using GPS to two location (Position 1 to 2) , OnLocation Changed
I am targeting of using Android 2.2 to find distance using GPS however there are some problem in regarding my on Location changed. Previously, I put my code at On click listener and it does not work well. I manage to get the distance only after pressing the start and stop button after a numerous times.
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc) {
if(originalCoordinates!=null){
coordinates1 = getGPS();
gpsEnd=coordinates1;
double d = distFrom(originalCoordinates[0],originalCoordinates[1],gpsEnd[0],gpsEnd[1]);
totalDistanceTravel+=d;
disTrav.setText(Double.toString(d));
txtViews.setText("" + coordinates1.length + " new counts!!!" + coordinates1[0] + "," + coordinates1[1] );
}
if(coordinates!=null)
{
double[] coordinatesPrev=coordinates;
txtView.setText("" + coordinates.length + " counts!!!" );
//double d = distFrom(coordinatesPrev[0],coordinatesPrev[1],coordinates[0],coordinates[1]);
//totalDistanceTravel+=d;
}
else
{
coordinates = getGPS();
gpsOrg=coordi开发者_JS百科nates;
if(originalCoordinates == null){
originalCoordinates = gpsOrg;
coordinates1 = getGPS();
txtView.setText("" + originalCoordinates.length + " original counts!!! " + originalCoordinates[0] + "," + originalCoordinates[1] );
}
//txtView.setText("" + coordinates.length + " counts" );
}
startButton.setEnabled(true);
}
dear do it like this.
- Switch on GPS of your device
- Wait until it get connected to a satellite.
- Make a flag like booelan detecting_position.
- Set it true on start button.
- OnLocationChange Listner, check if flag is true, then get the points using location.getLat,and getLong... and save them.
- Here you can do a trick, initialize start location variables with null, and check if startPosition is null then on first iteration of location listner set long and lat as start location, in further location change iterations set long and lat to stop_location variables.
- Once you tap the stop button set flag to false.
- Now you have both start and end position. Use the distance formula to calculate distance.
first of all take activity and do like below
public class MainActivity extends Activity implements OnClickListener {
Button btnStart = null;
Button btnStop = null;
Context context = null;
Button btnTouch = null;
float distance ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
context = this;
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
Intent i = new Intent(context, MyService.class);
if(v.getId() == R.id.btnStart)
{
startService(i);
}
else if(v.getId() == R.id.btnStop)
{
distance = MyService.final_distance;
stopService(i);
}
}
}
then take one service and put our logic for location change in this service.i have use service because service run in background continue and look for location change when location is update.
public class MyService extends Service implements LocationListener
{
public static float final_distance = 0.0f;
private float distance = 0.0f;
private double old_latitude = 0.0;
private double old_longitude = 0.0;
private double new_latitude = 0.0;
private double new_longitude = 0.0;
LocationManager locationManager = null;
float[] result = null;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0.0f, this);
result = new float[3];
}
@Override
public void onStart(Intent intent, int startid)
{
}
@Override
public void onDestroy()
{
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location)
{
if(old_latitude == 0.0 && old_longitude == 0.0)
{
old_latitude = location.getLatitude();
old_longitude = location.getLongitude();
}
new_latitude = location.getLatitude();
new_longitude = location.getLongitude();
Location.distanceBetween(old_latitude, old_longitude, new_latitude, new_longitude, result);
distance = final_distance +result[0];//distance in meters between two locations
final_distance = distance;
old_latitude = new_latitude;
old_longitude = new_longitude;
}
@Override
public void onProviderDisabled(String provider)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
and also add uses permission android.permission.ACCESS_FINE_LOCATION in android manifest.xml
Hope this may help you
精彩评论