这个倒霉的死bug阻碍了我学习开发百度地图的其中一条路,宝宝很不开心。
1、我确定我已经加了权限,甚至还加多了以防我少加了。。。
2、我可以获取到gps 的Provider
3、有很多网上小伙伴说室内是测试不到gps的,然井卵。。。我单手骑车单手拿手机在校园里奔驰(gps肯定是开着的,耗电巨快),都溜成狗了,这死经纬度都不出来。。。另我设置的位置改变监听是0.1m和3秒。╮(╯▽╰)╭
最后附下列代码:如果有哪个小伙伴能够解决我的未解之谜请奉献上我的膝盖。。。
public class MainActivity extends Activity {
private LocationManager locationManager;
private String locationProvider;
private LocationListener locationListener;
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_getlalng);//获取经纬度的button
textView = (TextView) findViewById(R.id.tv);
locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle arg2) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
// 如果位置发生变化,重新显示
showLocation(location);
}
};
//----点击获取经纬度--------
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getlalng();
}
});
}
private void getlalng() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 获取所有可用的位置提供器
List<String> providers = locationManager.getProviders(true);
if (providers.contains(LocationManager.GPS_PROVIDER)) {
// 如果是GPS
locationProvider = LocationManager.GPS_PROVIDER;
} else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
// 如果是Network
locationProvider = LocationManager.NETWORK_PROVIDER;
} else {
Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
return;
}
Location location = locationManager
.getLastKnownLocation(locationProvider);//有人说第一次调用location是为空,可是我一直在改变位置啊!一直在改变啊!!!一直在监听啊!!!
while (location == null) {
locationManager.requestLocationUpdates(locationProvider, 3000,
(float) 0.1, locationListener);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (locationManager != null) {
// 移除监听器
locationManager.removeUpdates(locationListener);
}
}
private void showLocation(Location location) {
String locationStr = "维度:" + location.getLatitude() + "\n" + "经度:"
+ location.getLongitude();
textView.setText(locationStr);
Toast.makeText(getApplicationContext(), locationStr, 0).show();
Log.i("haha", locationStr);
}
}
虽然未解但希望是暂时的,等我解决了我再更新这死bug的方法