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(); } };
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容猜你喜欢
AI推送时光机android如何获取经纬度
后端开发2023-05-31
Android 获取经纬度
后端开发2022-06-06
Android简单获取经纬度的方法
后端开发2022-06-06
Android获取经纬度计算距离介绍
后端开发2022-06-06
Android获取当前位置的经纬度数据
后端开发2022-06-06
如何用php根据地址获取经纬度
后端开发2024-04-02
uni-app如何获取位置信息(经纬度)
后端开发2023-08-18
C#如何提取经纬度文件中的经纬度数据
后端开发2024-04-02
Android编程获取地理位置的经度和纬度实例
后端开发2022-06-06
uniapp小程序如何获取位置经纬度信息
后端开发2023-07-04
如何在Android中利用原生APi获取所在位置的经纬度
后端开发2023-05-30
php根据经度纬度坐标获取区域信息
后端开发2023-10-12
android通过gps获取定位的位置数据和gps经纬度
后端开发2022-06-06
vue中调用百度地图获取经纬度的实现
后端开发2024-04-02
Android利用百度地图bomb服务器实现经纬度获取、测距等
后端开发2022-06-06
android手机获取gps和基站的经纬度地址实现代码
后端开发2022-06-06
PHP/Laravel如何通过经纬度计算距离获取附近商家
后端开发2024-04-02
php如何根据IP获取当前经纬度以及地域信息
后端开发2024-04-02
咦!没有更多了?去看看其它编程学习网 内容吧