我的目标是使用Android 2.2来查找使用GPS的距离,但是在我的位置更改方面存在一些问题。以前,我将我的代码放在On click listener上,但它不能正常工作。我设法在多次按下开始和停止按钮后才能获得距离。
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=coordinates;
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);
}
答案 0 :(得分:0)
答案 1 :(得分:0)
首先采取行动,如下所示
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);
}
}
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);
}
}
然后采取一项服务并将我们的位置变更逻辑放在此服务中。我有使用服务,因为服务在后台运行继续,并在位置更新时查找位置更改。
}
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)
{
}
并在android manifest.xml中添加使用权限android.permission.ACCESS_FINE_LOCATION
希望这可以帮到你