gpt4 book ai didi

android - 使用网络和 gps 卫星 Android 的最简单方法

转载 作者:行者123 更新时间:2023-11-30 04:22:22 27 4
gpt4 key购买 nike

我需要监控用户的位置以获得纬度和经度,当我使用网络提供商或 gps 提供商时,它工作正常。但是当我使用这两个提供商时,我没有得到值(value)。任何人都请提供解决方案。正在搜索并尝试很多示例

   // Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, true);
networkLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
gpsLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (networkLoc != null) {
System.out.println("Provider " + provider + " has been selected.");
double l = (double) (networkLoc.getLatitude());
double lng11 = (double) (networkLoc.getLongitude());
latituteField1.setText(Double.toString(l));
longitudeField1.setText(Double.toString(lng11));
}
if ( gpsLoc != null) {
System.out.println("Provider " + provider + " has been selected.");
double l0t = (double) (gpsLoc.getLatitude());
double l0g = (double) (gpsLoc.getLongitude());
latituteField.setText(Double.toString(l0t));
longitudeField.setText(Double.toString(l0g));

}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

}
@Override
protected void onResume() {
super.onResume();

locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
this
); }
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
this
);
}

@Override
public void onLocationChanged(Location location) {
lat1 = (double) (networkLoc.getLatitude());
lng1 = (double) (networkLoc.getLongitude());
latituteField1.setText(Double.toString(lat1));
longitudeField1.setText(Double.toString(lng1));
lat = (double) (gpsLoc.getLatitude());
lng = (double) (gpsLoc.getLongitude());
latituteField.setText(Double.toString(lat));
longitudeField.setText(Double.toString(lng));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disenabled provider " + provider,
Toast.LENGTH_SHORT).show();
}

protected void Display(Cursor c) {
Toast.makeText(this, "rowid: " + c.getString(0) + "\n" +
"Latitude: " + c.getString(1) + "\n" + "Longitude: " + c.getString(2) + "\n" +
Toast.LENGTH_LONG, 0).show();
}

最佳答案

不要向多个供应商请求位置更新。相反,选择 Criteria Android 会选择最适合您的。

例如代替:

   locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

使用:

   Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_COARSE); //Or whatever criteria you want
final String PROVIDER = locationManager.getBestProvider(c, true);
locationManager.requestLocationUpdates(PROVIDER, minTime, minDistance, this);

关于android - 使用网络和 gps 卫星 Android 的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9077780/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com