使用GPS计算距离时应该使用什么方便的优先级和间隔?
what is convenient priority and interval should be used while calculating distance using GPS?
我正在创建计算距离的应用程序。虽然 运行 我尝试将 priority=PRIORITY_BALANCED_POWER_ACCURACY
设置为 interval = 10 sec
。
我测试了它,它总是 return 总距离 = 0。问题可能出在优先级和间隔或我的代码上。
应该使用什么最方便的优先级和间隔?
代码
public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener {
private GoogleApiClient client;
private LocationRequest mLocationRequest ;
float distanceInMeters = 0;
TextView textView ;
Location A ;
Location B ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
A = new Location ("") ;
B = new Location ("") ;
}
public void activity (View v){
statusCheck();
startClient() ;
}
public void clear (View v){
if(client!=null) {
client.disconnect();
textView.setText(distanceInMeters + " m");
}
distanceInMeters = 0 ;
}
public void startClient (){
if(client==null) {
client = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
client.connect();
}
@Override
protected void onStop() {
super.onStop();
if(client!=null) {
client.disconnect();
}
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY );
mLocationRequest.setInterval(10000);
LocationServices.FusedLocationApi.requestLocationUpdates(client, mLocationRequest, this);
}
public float calculateDistance () {
if(A==null || A.getLatitude()==0){A=B;}
distanceInMeters = A.distanceTo(B);
Log.e("A","lat = "+A.getLatitude()+" lon = "+A.getLongitude() );
Log.e("B", "lat = " + B.getLatitude() + " lon = " + B.getLongitude());
Log.e("distance", "distance = " + distanceInMeters) ;
A =B ;
return distanceInMeters ;
}
@Override
public void onLocationChanged(Location location) {
B.setLatitude(location.getLatitude());
B.setLongitude(location.getLongitude());
calculateDistance();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public void statusCheck() {
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
在 onCreate 的 Location 构造函数中,将 "gps" 作为参数传递。有关详细信息,请参阅 documentation
我正在创建计算距离的应用程序。虽然 运行 我尝试将 priority=PRIORITY_BALANCED_POWER_ACCURACY
设置为 interval = 10 sec
。
我测试了它,它总是 return 总距离 = 0。问题可能出在优先级和间隔或我的代码上。
应该使用什么最方便的优先级和间隔?
代码
public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener,LocationListener {
private GoogleApiClient client;
private LocationRequest mLocationRequest ;
float distanceInMeters = 0;
TextView textView ;
Location A ;
Location B ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
A = new Location ("") ;
B = new Location ("") ;
}
public void activity (View v){
statusCheck();
startClient() ;
}
public void clear (View v){
if(client!=null) {
client.disconnect();
textView.setText(distanceInMeters + " m");
}
distanceInMeters = 0 ;
}
public void startClient (){
if(client==null) {
client = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
client.connect();
}
@Override
protected void onStop() {
super.onStop();
if(client!=null) {
client.disconnect();
}
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY );
mLocationRequest.setInterval(10000);
LocationServices.FusedLocationApi.requestLocationUpdates(client, mLocationRequest, this);
}
public float calculateDistance () {
if(A==null || A.getLatitude()==0){A=B;}
distanceInMeters = A.distanceTo(B);
Log.e("A","lat = "+A.getLatitude()+" lon = "+A.getLongitude() );
Log.e("B", "lat = " + B.getLatitude() + " lon = " + B.getLongitude());
Log.e("distance", "distance = " + distanceInMeters) ;
A =B ;
return distanceInMeters ;
}
@Override
public void onLocationChanged(Location location) {
B.setLatitude(location.getLatitude());
B.setLongitude(location.getLongitude());
calculateDistance();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public void statusCheck() {
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
在 onCreate 的 Location 构造函数中,将 "gps" 作为参数传递。有关详细信息,请参阅 documentation