gpt4 book ai didi

当没有 GPS 返回 null 时来自网络的 Android 位置

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

我有一个代码可以从 GPS 获取位置,但是如果没有启用 GPS,那么我想使用网络。到目前为止,GPS 部分工作正常,但 NETWORK 部分返回空位置,这是为什么?这是我的代码。

private static final long MINIMUNDIST = 1;
private static final long MINTME = 1000;
protected LocationManager locationMan;
protected Button mybutton;

mybutton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
showCurrentLocation();
}
});

protected void showCurrentLocation(){
boolean gpsOn = locationMan.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean netOn = locationMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location loc;
if (gpsOn){
loc = locationMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);

}
else if (netOn){
loc = locationMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
else{
Toast.makeText(MainActivity.this, "BOth GPS and NETWORK are OFF", Toast.LENGTH_SHORT).show();
loc = null;
}
if (loc != null){
String latText = String.format("%1$s" , loc.getLatitude());
String logText = String.format("%1$s" , loc.getLongitude());
TextView latTView = (TextView) findViewById(R.id.textView1);
TextView longtext = (TextView) findViewById(R.id.TextView01);
latTView.setText("Latitude :" + latText);
longtext.setText("Longtude :" + logText);
}
else{
Toast.makeText(MainActivity.this, "Null location?!!" , Toast.LENGTH_SHORT).show();
}
}


private class MyLocationListener implements LocationListener {



public void onLocationChanged(Location location) {


}



public void onStatusChanged(String s, int i, Bundle b) {



}



public void onProviderDisabled(String s) {



}



public void onProviderEnabled(String s) {


}



}

我的 list 文件包含任务所需的所有权限

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

顺便说一下,我正在通过 USB 调试在真实手机上测试应用程序。

最佳答案

我最近也在研究 GPS,我也关注了,this tutorial , 它工作正常(您可能需要修复某些部分才能使其他功能发挥作用..)

public Location getLocation(){
try{
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

//getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);


if(!isGPSEnabled){

}else{
this.canGetLocation = true;

if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");

if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
speed = location.getSpeed();
}
}
}

if(isGPSEnabled){
if(location == null){


locationManager.requestLocationUpdates((LocationManager.GPS_PROVIDER), MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");

if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}

}
}
}

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

return location;
}

并且您必须将所需的权限添加到 AndroidManifest.xml 文件中,如下所示

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

只是为了测试,您可以再添加一项权限以使用模拟位置。

   <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

关于当没有 GPS 返回 null 时来自网络的 Android 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25013062/

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