![](/img/trans.png)
[英]Android Location Manager Takes A Long Time Finding Longitude and Latitude
[英]Getting longitude and latitude takes a very long time
我正在獲取設備的經度和緯度,但是這需要30秒到一分鍾的時間。 有什么建議可以減少時間嗎?
public class MainActivity extends Activity
{
public String zipcode;
public double latG;
public double lonG;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled)
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.getLastKnownLocation(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
if (location != null)
{
latG = location.getLatitude();
lonG = location.getLongitude();
Toast.makeText(MainActivity.this,
latG + " " + lonG,
Toast.LENGTH_LONG).show();
}
}
public void onProviderDisabled(String provider)
{
}
public void onProviderEnabled(String provider)
{
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try
{
addresses = geocoder.getFromLocation(latG, lonG, 1);
}
catch (IOException e)
{
Context context = this;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setMessage("Error in getting address information.");
alertDialogBuilder.setCancelable(true);
}
for (Address address : addresses)
{
if(address.getPostalCode() != null)
{
zipcode = address.getPostalCode();
Toast.makeText(MainActivity.this, zipcode, Toast.LENGTH_LONG).show();
break;
}
}
}
}
您正在使用GPS_PROVIDER
來獲取GPS數據。 GPS_PROVIDER
直接從衛星獲取詳細信息,因此首次加載時需要時間。 此外,如果您不在GPS_PROVIDER
會花費30秒鍾以上的時間。 當您在辦公室或地下室中時, GPS_PROVIDER
可能返回NULL。
有另一種替代方法是使用NETWORK_PROVIDER
。 該提供商將根據您當前的網絡狀態為您提供GPS詳細信息。 這不會像GPS_PROVIDER
那樣GPS_PROVIDER
但是可以更快地工作。
您好,您正在使用GPS PROVIDER,因為它可能要花費一些時間,這取決於幾個限制因素,例如您的建築物位置,物理位置,天氣,因為可以從衛星獲得gps數據,因此請使用網絡提供商,這種情況下您可能會更快給定的代碼段位於
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
最好的選擇是使用Google Play服務及其LocationClient。
http://developer.android.com/google/play-services/location.html
這為您提供了一個提供程序,該提供程序可以自動從所有提供程序類型中選擇最佳的可用信息,並可以在某些情況下使用getLastLocation()
立即返回位置。
嘗試在設備中運行而不是在仿真器中運行。
嘗試這個
try {
gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {}
if (gps_enabled) {
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
if (network_enabled) {
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
無論您可以使用什么服務,它都會給出響應。
使用此代碼可以更快地獲取信息,
String provider;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
if (location != null)
{
onLocationChanged(location);
//your remaining code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.