广告
返回顶部
首页 > 资讯 > 精选 >android如何获取经纬度
  • 144
分享到

android如何获取经纬度

android经纬度roi 2023-05-31 00:05:46 144人浏览 独家记忆
摘要

Android 定位的两种方式:GPS_PROVIDER and NETWORK_PROVIDER定位的可以借助LocationManager来实现MainActivity代码static final String TAG = "MainA

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();  } };

--结束END--

本文标题: android如何获取经纬度

本文链接: https://www.lsjlt.com/news/222656.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作