[英]In ANDROID, How to get a current position and tracking in map using GPS without giving any location in a program?
[英]How to get current GPS position without waiting for location change on Android?
我想使用gps提供程序獲取設備的當前位置,但這實際上是在等待gps位置更改。
因此,我有一個簡單的應用程序,用於偵聽位置更改。 這可以立即與NETWORK_PROVIDER
一起使用,但是對於GPS_PROVIDER
我等待了15分鍾,並且沒有任何動作。
我正在使用API級別8。
public class MainActivity extends Activity {
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) this.findViewById(R.id.textView1);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
//lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll); // this works fine
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
private class mylocationlistener implements LocationListener {
public void onLocationChanged(Location location) {
Date today = new Date();
Timestamp currentTimeStamp = new Timestamp(today.getTime());
if (location != null) {
Log.d("LOCATION CHANGED", location.getLatitude() + "");
Log.d("LOCATION CHANGED", location.getLongitude() + "");
String str = "\n CurrentLocation: " + "\n Latitude: "
+ location.getLatitude() + "\n Longitude: "
+ location.getLongitude() + "\n Accuracy: "
+ location.getAccuracy() + "\n CurrentTimeStamp "
+ currentTimeStamp + "\n Altitude: "
+ location.getAltitude() + "\n Bearing"
+ location.getBearing() + "\n Speed"
+ location.getSpeed() + "\n ";
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG)
.show();
tv.append(str);
}
}
public void onProviderDisabled(String provider) {
Toast.makeText(MainActivity.this, "Error onProviderDisabled",
Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(MainActivity.this, "onProviderEnabled",
Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Toast.makeText(MainActivity.this, "onStatusChanged",
Toast.LENGTH_LONG).show();
}
}
}
我確實將許可放入清單中,所以這不是問題。
你在里面測試嗎 除非您不在外面,否則根本不會獲得GPS鎖定,這意味着手機將嘗試盡可能多地獲得鎖定(因為您在requestLocationUpdates呼叫中指定了最短時間和距離的0和0 )。
不幸的是,您只需要等待設備獲得GPS鎖定,如果不是由於缺少信號而導致的,您將一直等待。 緩解此問題的第一件事就是嘗試獲取GPS提供商的最新已知位置。 如果設備最近具有GPS鎖定並存儲了位置,則它將返回一個位置:
Location lastLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
除此之外,您應該嘗試從網絡提供商處獲取位置,因為您已經觀察到,返回位置可能比GPS快得多。 這是因為位置數據源(蜂窩塔和WIFI)豐富,只要您有無線信號,這些位置數據就可以在任何地方(包括室內)使用。
try{
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String stlongitude = Double.toString(longitude);
String stlatitude = Double.toString(latitude);
TextView celllocation = (TextView) findViewById(R.id.txtCellLocation);
celllocation.setText(stlatitude + " , " + stlongitude);
//String uri = String.format(Locale.ENGLISH, "geo:%f,%f", latitude, longitude);
//Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
//startActivity(intent);
}
catch (Exception a) {
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
String stlongitude = Double.toString(longitude);
String stlatitude = Double.toString(latitude);
TextView celllocation = (TextView) findViewById(R.id.txtCellLocation);
celllocation.setText(stlatitude + " , " + stlongitude);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.