android - 如何获取设备的gps纬度和经度?
问题描述
有人请建议一个代码来获取android中的android设备的gps 位置(纬度和经度) 。我尝试了很多例子,但在
Location location = locationManager.getLastKnownLocation(bestProvider);
得到location=null
.
我在下面发布我的代码。
public class MainActivity extends AppCompatActivity {
Geocoder geocoder;
String bestProvider;
List<Address> user = null;
double lat,lng;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = lm.getBestProvider(criteria, false);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
return;
}
Location location = lm.getLastKnownLocation(bestProvider);
if (location == null)
{
Toast.makeText(this,"Location Not found",Toast.LENGTH_LONG).show();
}
else{
geocoder = new Geocoder(this);
try
{
user = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
lat=(double)user.get(0).getLatitude();
lng=(double)user.get(0).getLongitude();
System.out.println(" DDD lat: " +lat+", longitude: "+lng);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
解决方案
you can try with the LocationManager.
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
then you LocationListener which will give you asynchronous updates of your location.
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
also add permissions into your manifest file:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.INTERNET" />
I hope this will help you.
推荐阅读
- python - 如何修复 Python 上 p2p 聊天应用程序的“IndexError: list index out of range”错误?
- python-2.7 - 带有 CeleryExecutor 的气流没有从气流.cfg 中选择 Celery 的配置集
- groovy - 将参数传递给 groovy 闭包
- vue.js - vue cli build 为什么会两次注入app.js、app.css、vendors.js和vendors.css?
- python - 根据规则 Pandas 数据框创建新列
- javascript - 正则表达式匹配 &foo= 之间的所有内容,直到它再次出现
- c# - Mono 编译器生成的 .exe 直接从 linux 命令行运行,为什么?
- angular - Angular`ngOnInit`中的异步/等待
- php - 如何修复“包含”不显示文件内容?
- python - “NoneType”对象没有属性“attrs”