android如何获取经纬度

android如何获取经纬度,第1张

概述android定位的两种方式:GPS_PROVIDERandNETWORK_PROVIDER定位的可以借助LocationManager来实现

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 {   // 当没有可用的位置提供器时,d出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_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {    // Todo: ConsIDer calling    // ActivityCompat#requestPermissions    // here to request the missing permissions,and then overrIDing    // public voID onRequestPermissionsResult(int requestCode,//           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如何获取经纬度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145960.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存