Próbowałem wprowadzić funkcję dla mojej aplikacji na Androida, która pobiera prędkość i kierunek podróży urządzenia, bez względu na to, gdzie urządzenie jest wskazane. Na przykład: jeśli moje urządzenie z Androidem jest skierowane w kierunku północnym i jeśli poruszam się wstecz w kierunku południowym, zwróci to, że poruszam się w kierunku południowym.Lokalizacja Androida getBearing() zawsze zwraca 0
Rozglądałem się i udało mi się skorzystać z metody GetBearing (Location) w lokalizacji (Wciąż nie wiem, czy to rozwiąże mój problem). Kiedy wywołuję funkcję getBearing(), zawsze zwraca ona wartość 0,0 z jakiegoś powodu. Nie mam pojęcia dlaczego. Oto mój kod:
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);
}
To tutaj otrzymuję łożysko.
@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;
}
Dziękuję bardzo!
Dziękuję za twoją opinię i przepraszam, że zupełnie zapomniałem wspomnieć w moim poście, że korzystam z dostawcy GPS. Zaktualizuję mój kod. – user3171597
Czy próbowałeś użyć getBearing() na zewnątrz iw miejscu z dala od wysokich budynków lub innych obiektów, które mogłyby uniemożliwić ci komunikację satelitarną? –
Tak, zrobiłem. Próbowałem testować tę funkcję podczas jazdy. Mimo że prędkość jest dość dokładna (5-10 km), funkcja getBearing() zawsze zwraca 0, co zawsze zwraca wartość "You are: Northbound", niezależnie od kierunku, w którym się znajduję. – user3171597