gpt4 book ai didi

android - 在android 2.1中获取gps坐标的问题(找到答案)

转载 作者:行者123 更新时间:2023-11-29 14:57:11 24 4
gpt4 key购买 nike

我在 2.1 中获取 GPS 坐标时遇到问题。

我现在使用的代码在 1.6 中运行良好,但是当我测试时1.6 设备中的同一个 apk 显示空值

请帮我找到一种方法也可以使用 2.1 设备

这是我的代码。

public class GpsLocator {

private static String PROVIDER="gps";
private LocationManager myLocationManager=null;

public GpsLocator(Context context) {
myLocationManager=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
}

public void myOnresume() {
myLocationManager.requestLocationUpdates(PROVIDER, 0, 0, onLocationChange);
}

public void myonPause() {
myLocationManager.removeUpdates(onLocationChange);
}

public double getLatitude() {
Location loc=myLocationManager.getLastKnownLocation(PROVIDER);
if (loc==null) {
return(0);
}
return(loc.getLatitude());
}

public double getLongitude() {
Location loc=myLocationManager.getLastKnownLocation(PROVIDER);
if (loc==null) {
return(0);
}

return(loc.getLongitude());
}

LocationListener onLocationChange=new LocationListener() {

public void onLocationChanged(Location location) {
}

public void onProviderDisabled(String provider) {
// required for interface, not used
}

public void onProviderEnabled(String provider) {
// required for interface, not used
}

public void onStatusChanged(String provider, int status,Bundle extras) {
// required for interface, not used
}
};
}

在 list 文件中我添加了访问文件的权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

最佳答案

    LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);

Location location = locationManager.getLastKnownLocation(provider);

updateWithNewLocation(location);

locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
}

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}

public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}

public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};

关于android - 在android 2.1中获取gps坐标的问题(找到答案),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3819505/

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