Android 为什么位置为空?
我正在尝试使用以下代码获取我应用程序中的当前位置:Android 为什么位置为空?,android,location,locationmanager,google-geocoder,Android,Location,Locationmanager,Google Geocoder,我正在尝试使用以下代码获取我应用程序中的当前位置: List<Address> adds = null; double lat; double lng; private Geocoder geocoder; String bestProvider; LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criter
List<Address> adds = null;
double lat;
double lng;
private Geocoder geocoder;
String bestProvider;
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(bestProvider);
if (location == null)
{
Toast.makeText(this,"Location Not found",Toast.LENGTH_LONG).show();
}
else
{
geocoder = new Geocoder(this);
try
{
users = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
lat=(double)users.get(0).getLatitude();
lng=(double)users.get(0).getLongitude();
System.out.println(" DDD lat: " +lat+", longitude: "+lng);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
List adds=null;
双lat;
双液化天然气;
私人地理编码器;
字符串提供程序;
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
标准=新标准();
bestProvider=lm.getBestProvider(标准,false);
位置=lm.getLastKnownLocation(bestProvider);
if(位置==null)
{
Toast.makeText(此“未找到位置”,Toast.LENGTH_LONG.show();
}
其他的
{
地理编码器=新地理编码器(本);
尝试
{
users=geocoder.getFromLocation(location.getLatitude(),location.getLatitude(),1);
lat=(double)users.get(0.getLatitude();
lng=(double)users.get(0.getLongitude();
系统输出打印项次(“DDD纬度:+lat+”,经度:+lng);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
}
但location始终显示为null,因此location Not found消息出现在应用程序上。我错过什么了吗
清单权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
如有任何帮助,将不胜感激。请使用本网站以正确方式获取位置: 这是一个权限: 如果您仍然需要我的代码:
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, OnConnectionFailedListener {
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
TextView mLatitudeText, mLongitudeText;
final int MY_PERMISSIONS_REQUEST_MAPS = 1;
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLatitudeText = (TextView) findViewById(R.id.textView);
mLongitudeText = (TextView) findViewById(R.id.textView2);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
Log.i("permit Fine", ""+ ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION));
Log.i("permit coarse", ""+ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION));
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
Log.i("in","Explaination");
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_MAPS);
Log.i("in","Else");
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_MAPS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Log.i("permit Fine", ""+ ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION));
Log.i("permit coarse", ""+ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION));
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}下次使用搜索功能!你是在一台设备上尝试吗?是的,结果表明网络是最好的定位服务提供商,但当时的设备设置关闭了网络定位服务,这是一个奇怪的设置,但现在它可以工作了。