Android 位置 getBearing() 总是 returns 0
Android Location getBearing() always returns 0
我一直在尝试为我的 Android 应用实现一项功能,无论设备指向何处,它都能获取设备的移动速度和方向。例如:如果我的 Android 设备指向北方向,如果我向南方向向后移动,则 return 我正在向南移动。
我一直在四处寻找,并想到了使用 Location 的 getBearing() 方法的可能性(不过,我不知道这是否能解决我的整个问题)。当我调用 getBearing() 时,由于某种原因它总是 returns 0.0。我不知道为什么。这是我的代码:
LocationManager lm;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gcm);
setUpUI(findViewById(R.id.LinearLayout1));
isRegged = false;
// GCM startup
gcm = GoogleCloudMessaging.getInstance(this);
context = getApplicationContext();
gps = new GPSTracker(context);
// gps.startListening(context);
// gps.setGpsCall(this);
/*
* Variables to indicate location and device ID
*/
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (gps.getIsGPSTrackingEnabled())
{
longitude = Double.valueOf(gps.getLongitude()).toString();
latitude = Double.valueOf(gps.getLatitude()).toString();
}
deviceID = telephonyManager.getDeviceId();
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, (float) 0.0,
this);
}
这是我得到方位的地方。
@Override
public void onLocationChanged(Location currentLocation)
{
float speed = 0;
float speed_mph = 0;
if (previousLocation != null)
{
float distance = currentLocation.distanceTo(previousLocation);
// time taken (in seconds)
float timeTaken = ((currentLocation.getTime() - previousLocation
.getTime()) / 1000);
// calculate speed
if (timeTaken > 0)
{
speed = getAverageSpeed(distance, timeTaken);
speed_mph = (float) (getAverageSpeed(distance, timeTaken) / 1.6);
}
if (speed >= 0)
{
info_text.setVisibility(View.VISIBLE);
info_text_mph.setVisibility(View.VISIBLE);
DecimalFormat df = new DecimalFormat("#.#");
info_text.setText("Speed: " + df.format(speed) + " " + "km/h");
info_text_mph.setText(" Speed: " + df.format(speed_mph) + " "
+ "mph");
if (speed >= 10 && lm.getProvider(LocationManager.GPS_PROVIDER).supportsBearing())
{
float degree = currentLocation.getBearing();
direction_text.setVisibility(View.VISIBLE);
Log.i(TAG, String.valueOf(degree));
if (degree == 0 && degree < 45 || degree >= 315
&& degree == 360)
{
direction_text.setText("You are: Northbound");
}
if (degree >= 45 && degree < 90)
{
direction_text.setText("You are: NorthEastbound");
}
if (degree >= 90 && degree < 135)
{
direction_text.setText("You are: Eastbound");
}
if (degree >= 135 && degree < 180)
{
direction_text.setText("You are: SouthEastbound");
}
if (degree >= 180 && degree < 225)
{
direction_text.setText("You are: SouthWestbound");
}
if (degree >= 225 && degree < 270)
{
direction_text.setText("You are: Westbound");
}
if (degree >= 270 && degree < 315)
{
direction_text.setText("You are: NorthWestbound");
}
}
}
}
previousLocation = currentLocation;
}
非常感谢!
如果您使用 LocationManager.NETWORK_PROVIDER
获取数据,getBearing()
将 return 0,因为 signal/accuracy 太弱了。尝试将 GPS 提供程序设置为 GPS,并确保在室外进行测试(由于卫星无法直接通信,GPS 在室内或很高的建筑物中间无法工作)
为确保您选择的提供商支持 getBearing(),您可以使用 LocationProvider
中名为 supportsBearing ()
的方法 return 如果您选择的提供商支持 getBearing()
呼叫.
最后确保您在 AndroidManifest.xml
中拥有 ACCESS_COARSE_LOCATION
或 ACCESS_FINE_LOCATION
权限
根据我的建议,代码应该是这样的:
LocationManager mlocManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
资源:
http://developer.android.com/reference/android/location/LocationManager.html
http://developer.android.com/reference/android/location/LocationProvider.html
http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/
更新:答案是在 getBearing() 中用于计算的两个点太接近,因此给出的结果不准确。要纠正此问题,请手动抓取两个 GPS 点并使用 bearingTo() 以查看更准确的结果。
我一直在尝试为我的 Android 应用实现一项功能,无论设备指向何处,它都能获取设备的移动速度和方向。例如:如果我的 Android 设备指向北方向,如果我向南方向向后移动,则 return 我正在向南移动。
我一直在四处寻找,并想到了使用 Location 的 getBearing() 方法的可能性(不过,我不知道这是否能解决我的整个问题)。当我调用 getBearing() 时,由于某种原因它总是 returns 0.0。我不知道为什么。这是我的代码:
LocationManager lm;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gcm);
setUpUI(findViewById(R.id.LinearLayout1));
isRegged = false;
// GCM startup
gcm = GoogleCloudMessaging.getInstance(this);
context = getApplicationContext();
gps = new GPSTracker(context);
// gps.startListening(context);
// gps.setGpsCall(this);
/*
* Variables to indicate location and device ID
*/
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (gps.getIsGPSTrackingEnabled())
{
longitude = Double.valueOf(gps.getLongitude()).toString();
latitude = Double.valueOf(gps.getLatitude()).toString();
}
deviceID = telephonyManager.getDeviceId();
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, (float) 0.0,
this);
}
这是我得到方位的地方。
@Override
public void onLocationChanged(Location currentLocation)
{
float speed = 0;
float speed_mph = 0;
if (previousLocation != null)
{
float distance = currentLocation.distanceTo(previousLocation);
// time taken (in seconds)
float timeTaken = ((currentLocation.getTime() - previousLocation
.getTime()) / 1000);
// calculate speed
if (timeTaken > 0)
{
speed = getAverageSpeed(distance, timeTaken);
speed_mph = (float) (getAverageSpeed(distance, timeTaken) / 1.6);
}
if (speed >= 0)
{
info_text.setVisibility(View.VISIBLE);
info_text_mph.setVisibility(View.VISIBLE);
DecimalFormat df = new DecimalFormat("#.#");
info_text.setText("Speed: " + df.format(speed) + " " + "km/h");
info_text_mph.setText(" Speed: " + df.format(speed_mph) + " "
+ "mph");
if (speed >= 10 && lm.getProvider(LocationManager.GPS_PROVIDER).supportsBearing())
{
float degree = currentLocation.getBearing();
direction_text.setVisibility(View.VISIBLE);
Log.i(TAG, String.valueOf(degree));
if (degree == 0 && degree < 45 || degree >= 315
&& degree == 360)
{
direction_text.setText("You are: Northbound");
}
if (degree >= 45 && degree < 90)
{
direction_text.setText("You are: NorthEastbound");
}
if (degree >= 90 && degree < 135)
{
direction_text.setText("You are: Eastbound");
}
if (degree >= 135 && degree < 180)
{
direction_text.setText("You are: SouthEastbound");
}
if (degree >= 180 && degree < 225)
{
direction_text.setText("You are: SouthWestbound");
}
if (degree >= 225 && degree < 270)
{
direction_text.setText("You are: Westbound");
}
if (degree >= 270 && degree < 315)
{
direction_text.setText("You are: NorthWestbound");
}
}
}
}
previousLocation = currentLocation;
}
非常感谢!
LocationManager.NETWORK_PROVIDER
获取数据,getBearing()
将 return 0,因为 signal/accuracy 太弱了。尝试将 GPS 提供程序设置为 GPS,并确保在室外进行测试(由于卫星无法直接通信,GPS 在室内或很高的建筑物中间无法工作)
为确保您选择的提供商支持 getBearing(),您可以使用 LocationProvider
中名为 supportsBearing ()
的方法 return 如果您选择的提供商支持 getBearing()
呼叫.
最后确保您在 AndroidManifest.xml
中拥有ACCESS_COARSE_LOCATION
或 ACCESS_FINE_LOCATION
权限
根据我的建议,代码应该是这样的:
LocationManager mlocManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
资源: http://developer.android.com/reference/android/location/LocationManager.html http://developer.android.com/reference/android/location/LocationProvider.html http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/
更新:答案是在 getBearing() 中用于计算的两个点太接近,因此给出的结果不准确。要纠正此问题,请手动抓取两个 GPS 点并使用 bearingTo() 以查看更准确的结果。