gpt4 book ai didi

android - 我的 GPS 提供商已启用,但我仍然获得空位置

转载 作者:太空狗 更新时间:2023-10-29 16:39:28 25 4
gpt4 key购买 nike

我正在使用这段代码来获取用户的地理位置

public Location getLocation() {

Location location = null;
double lat;
double lng;
try {
LocationManager mLocationManager = (LocationManager) con.getSystemService(LOCATION_SERVICE);

// getting GPS status
boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network status
boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


if (!isGPSEnabled && !isNetworkEnabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else {
// First get location from Network Provider
if (isNetworkEnabled) {
mLocationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, this);
Log.d("Network", "Network");
if (mLocationManager != null) {
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
System.out.println(lat);
}
}
}
//get the location by gps
if (isGPSEnabled) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, this);
Log.d("GPS Enabled", "GPS Enabled");
if (mLocationManager != null) {
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
System.out.println(lat);
}
}
}
}

} catch (Exception e) {
e.printStackTrace();
}



return location;
}

这是我得到的:

网络39.68967启用全局定位系统(无)

我无法理解启用 Gps 的原因,我仍然无法从 gps 提供商处获取坐标

最佳答案

我刚刚创建了一个示例项目以了解Location Services 的用法。我只是发布我的代码,您可以随意使用它来满足您的需要并了解 Location Services 的工作原理。

public class DSLVFragmentClicks extends Activity implements LocationListener {
private final int BESTAVAILABLEPROVIDERCODE = 1;
private final int BESTPROVIDERCODE = 2;
LocationManager locationManager;
String bestProvider;
String bestAvailableProvider;

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BESTPROVIDERCODE) {
if (resultCode!= Activity.RESULT_OK || locationManager.isProviderEnabled(bestProvider)) {
Toast.makeText(getActivity(), "Error! Location Service " + bestProvider + " not Enabled", Toast.LENGTH_LONG).show();

} else {
getLocation(bestProvider);
}
} else {
if (resultCode!= Activity.RESULT_OK || locationManager.isProviderEnabled(bestAvailableProvider)) {
Toast.makeText(getActivity(), "Error! Location Service " + bestAvailableProvider + " not Enabled", Toast.LENGTH_LONG).show();

} else {
getLocation(bestAvailableProvider);
}
}
}

public void getLocation(String usedLocationService) {
Toast.makeText(getActivity(), "getting Location", Toast.LENGTH_SHORT).show();
long updateTime = 0;
float updateDistance = 0;
// finding the current location
locationManager.requestLocationUpdates(usedLocationService, updateTime, updateDistance, this);

}


@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.main);
// set a Criteria specifying things you want from a particular Location Service
Criteria criteria = new Criteria();
criteria.setSpeedRequired(false);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(true);
criteria.setBearingAccuracy(Criteria.ACCURACY_HIGH);
criteria.setAltitudeRequired(false);

locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// finding best provider without fulfilling the criteria
bestProvider = locationManager.getBestProvider(criteria, false);
// finding best provider which fulfills the criteria
bestAvailableProvider = locationManager.getBestProvider(criteria, true);
String toastMessage = null;
if (bestProvider == null) {
toastMessage = "NO best Provider Found";
} else if (bestAvailableProvider != null && bestAvailableProvider.equals(bestAvailableProvider)) {
boolean enabled = locationManager.isProviderEnabled(bestAvailableProvider);
if (!enabled) {
Toast.makeText(getActivity(), " Please enable " + bestAvailableProvider + " to find your location", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(mainIntent, BESTAVAILABLEPROVIDERCODE);
} else {
getLocation(bestAvailableProvider);
}
toastMessage = bestAvailableProvider + " used for getting your current location";
} else {
boolean enabled = locationManager.isProviderEnabled(bestProvider);
if (!enabled) {
Toast.makeText(getActivity(), " Please enable " + bestProvider + " to find your location", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(mainIntent, BESTPROVIDERCODE);
} else {
getLocation(bestProvider);
}
toastMessage = bestProvider + " is used to get your current location";

}
Toast.makeText(getActivity(), toastMessage, Toast.LENGTH_LONG).show();
return true;
}
});
}

@Override
public void onLocationChanged(Location location) {
Log.d("Location Found", location.getLatitude() + " " + location.getLongitude());
// getting the street address from longitute and latitude
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
String addressString = "not found !!";
try {
List<Address> addressList = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
StringBuilder stringBuilder = new StringBuilder();
if (addressList.size() > 0) {
Address address = addressList.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
stringBuilder.append(address.getAddressLine(i)).append("\n");
stringBuilder.append(address.getLocality()).append("\n");
stringBuilder.append(address.getPostalCode()).append("\n");
stringBuilder.append(address.getCountryName()).append("\n");
}

addressString = stringBuilder.toString();
locationManager.removeUpdates(this);
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
Toast.makeText(getActivity(), " Your Location is " + addressString, Toast.LENGTH_LONG).show();
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
//To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onProviderEnabled(String s) {
//To change body of implemented methods use File | Settings | File Templates.
}

@Override
public void onProviderDisabled(String s) {
//To change body of implemented methods use File | Settings | File Templates.
}
}

如果您不明白其中的任何部分,请随时提问。

关于android - 我的 GPS 提供商已启用,但我仍然获得空位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20472955/

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