android如何获取经纬度
发布时间 - 2026-01-11 02:01:49 点击率:次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
# 经纬度
# 定位
# Android通过原生APi获取所在位置的经纬度
# android通过gps获取定位的位置数据和gps经纬度
# Android 通过当前经纬度获得城市的实例代码
# Android获取当前位置的经纬度数据
# Android获取经纬度计算距离介绍
# android手机获取gps和基站的经纬度地址实现代码
# Android简单获取经纬度的方法
# Android GPS获取当前经纬度坐标
# Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法
# Android通过原生方式获取经纬度与城市信息的方法
# 两种
# 弹出
# 来实现
# 时将
# 大家多多
# 移除
# setContentView
# super
# layout
# initView
# activity_main
# savedInstanceState
# onCreate
# void
# protected
# Bundle
# Override
# findViewById
# true
# getProviders
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
详解jQuery中基本的动画方法
如何在Windows服务器上快速搭建网站?
如何彻底删除建站之星生成的Banner?
🚀拖拽式CMS建站能否实现高效与个性化并存?
Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】
iOS正则表达式验证手机号、邮箱、身份证号等
如何在腾讯云服务器上快速搭建个人网站?
网站制作壁纸教程视频,电脑壁纸网站?
详解Huffman编码算法之Java实现
为什么要用作用域操作符_php中访问类常量与静态属性的优势【解答】
Laravel Seeder怎么填充数据_Laravel数据库填充器的使用方法与技巧
如何快速配置高效服务器建站软件?
免费视频制作网站,更新又快又好的免费电影网站?
浅析上传头像示例及其注意事项
php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】
Python高阶函数应用_函数作为参数说明【指导】
Linux网络带宽限制_tc配置实践解析【教程】
BootStrap整体框架之基础布局组件
html5如何设置样式_HTML5样式设置方法与CSS应用技巧【教程】
Laravel如何实现模型的全局作用域?(Global Scope示例)
如何用AWS免费套餐快速搭建高效网站?
如何快速生成高效建站系统源代码?
INTERNET浏览器怎样恢复关闭标签页_INTERNET浏览器标签恢复快捷键与方法【指南】
Laravel如何发送邮件_Laravel Mailables构建与发送邮件的简明教程
php485函数参数是什么意思_php485各参数详细说明【介绍】
java获取注册ip实例
微信小程序 wx.uploadFile无法上传解决办法
深圳网站制作的公司有哪些,dido官方网站?
Python结构化数据采集_字段抽取解析【教程】
Swift中swift中的switch 语句
香港服务器租用费用高吗?如何避免常见误区?
黑客入侵网站服务器的常见手法有哪些?
如何快速搭建高效WAP手机网站吸引移动用户?
网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?
Laravel如何使用缓存系统提升性能_Laravel缓存驱动和应用优化方案
如何在万网自助建站平台快速创建网站?
node.js报错:Cannot find module 'ejs'的解决办法
如何在万网自助建站中设置域名及备案?
Laravel全局作用域是什么_Laravel Eloquent Global Scopes应用指南
laravel怎么配置Redis作为缓存驱动_laravel Redis缓存配置教程
文字头像制作网站推荐软件,醒图能自动配文字吗?
北京企业网站设计制作公司,北京铁路集团官方网站?
ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】
javascript中数组(Array)对象和字符串(String)对象的常用方法总结
浅述节点的创建及常见功能的实现
Laravel如何使用Collections进行数据处理?(实用方法示例)
香港服务器部署网站为何提示未备案?
laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法
Laravel如何实现本地化和多语言支持?(i18n教程)
详解免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)

