android如何获取经纬度
更新时间:2017年06月23日 15:10:22 作者:花落谁家2091572217
这篇文章主要为大家详细介绍了android获取经纬度的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
android 定位的两种方式:GPS_PROVIDER and NETWORK_PROVIDER
定位的可以借助LocationManager来实现
MainActivity代码
static final String TAG = "MainActivity";
private TextView locationTV;
private LocationManager locationManager;
private String provider;
ArrayList<ContactModel> dataList = new ArrayList<ContactModel>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initView();
locationTV = (TextView) findViewById(R.id.locaiton_tv);
locationManager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
// 获取所有可用的位置提供器
List<String> providerList = locationManager.getProviders(true);
if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
// 当没有可用的位置提供器时,弹出Toast提示用户
Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT).show();
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
// 显示当前设备的位置信息
showLocation(location);
}
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.
return;
}
locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
}
private void showLocation(Location location) {
String currentPosition = "latitude is " + location.getLatitude() + "\n"+ "longitude is " + location.getLongitude();
locationTV.setText(currentPosition);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (locationManager != null) {
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.
return;
}
// 关闭程序时将监听器移除
locationManager.removeUpdates(locationListener);
}
}
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(MainActivity.this,"onLocationChanged",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
Toast.makeText(MainActivity.this,"onStatusChanged",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,"onProviderEnabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String s) {
Toast.makeText(MainActivity.this,"onProviderDisabled",Toast.LENGTH_SHORT).show();
}
};
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android Jetpack Compose实现列表吸顶效果
安卓传统的Recyclerview打造悬浮头部StickyHeader的吸顶效果,十分麻烦,而在Compose中就简单多了。因此,本文将采用Jetpack Compose实现列表吸顶效果,需要的可以参考一下2022-02-02
Android 后台发送邮件示例 (收集应用异常信息+Demo代码)
今天介绍个更简单的方法,我们把异常信息收集后,通过后台发送邮件方法,把相关异常信息发送到我们指定的邮箱里面2013-07-07
Android checkbox的listView具体操作方法
这篇文章主要介绍了Android checkbox的listView具体操作方法,重点就是存储每个checkbox的状态值,感兴趣的小伙伴们可以参考一下2015-12-12
解决在eclipse中将android项目生成apk并且给apk签名的实现方法详解
本篇文章是对在eclipse中将android项目生成apk并且给apk签名的实现方法进行了详细的分析介绍,需要的朋友参考下2013-05-05
安卓APP测试之使用Burp Suite实现HTTPS抓包方法
这篇文章主要介绍了安卓APP测试之使用Burp Suite实现HTTPS抓包方法,本文详解讲解了测试环境和各个软件的配置方法,需要的朋友可以参考下2015-04-04


最新评论