十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要讲解了Android如何通过原生方式获取经纬度与城市信息,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
创新互联建站成立以来不断整合自身及行业资源、不断突破观念以使企业策略得到完善和成熟,建立了一套“以技术为基点,以客户需求中心、市场为导向”的快速反应体系。对公司的主营项目,如中高端企业网站企划 / 设计、行业 / 企业门户设计推广、行业门户平台运营、重庆APP开发公司、手机网站开发、微信网站制作、软件开发、成都联通服务器托管等实行标准化操作,让客户可以直观的预知到从创新互联建站可以获得的服务效果。一、概述
在项目中需要获取用户所在位置的经纬度和城市上送给风控系统。一般来说,定位有两种方式:
本文讲述定位的第二种方式--用Android原生的SDK中的api定位,如果项目定位要求较高还是建议使用第三方地图库。
二、Android原生SDK中的api定位
Android原生方式获取经纬度两种定位方式:GPS定位和Wifi定位
示例代码如下:
public class TestLocationActivity extends AppCompatActivity { public static final int LOCATION_CODE = 301; private LocationManager locationManager; private String locationProvider = null; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); getLocation(); } private void getLocation(){ //1.获取位置管理器 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //2.获取位置提供器,GPS或是NetWork Listproviders = locationManager.getProviders(true); if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS locationProvider = LocationManager.GPS_PROVIDER; Log.v("TAG", "定位方式GPS"); } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是Network locationProvider = LocationManager.NETWORK_PROVIDER; Log.v("TAG", "定位方式Network"); }else { Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show(); return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //获取权限(如果没有开启权限,会弹出对话框,询问是否开启权限) if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { //请求权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_CODE); } else { //3.获取上次的位置,一般第一次运行,此值为null Location location = locationManager.getLastKnownLocation(locationProvider); if (location!=null){ Toast.makeText(this, location.getLongitude() + " " + location.getLatitude() + "",Toast.LENGTH_SHORT).show(); Log.v("TAG", "获取上次的位置-经纬度:"+location.getLongitude()+" "+location.getLatitude()); getAddress(location); }else{ //监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace locationManager.requestLocationUpdates(locationProvider, 3000, 1,locationListener); } } } else { Location location = locationManager.getLastKnownLocation(locationProvider); if (location!=null){ Toast.makeText(this, location.getLongitude() + " " + location.getLatitude() + "", Toast.LENGTH_SHORT).show(); Log.v("TAG", "获取上次的位置-经纬度:"+location.getLongitude()+" "+location.getLatitude()); getAddress(location); }else{ //监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace locationManager.requestLocationUpdates(locationProvider, 3000, 1,locationListener); } } } public LocationListener locationListener = new LocationListener() { // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数 @Override public void onStatusChanged(String provider, int status, Bundle extras) { } // Provider被enable时触发此函数,比如GPS被打开 @Override public void onProviderEnabled(String provider) { } // Provider被disable时触发此函数,比如GPS被关闭 @Override public void onProviderDisabled(String provider) { } //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发 @Override public void onLocationChanged(Location location) { if (location != null) { //如果位置发生变化,重新显示地理位置经纬度 Toast.makeText(TestLocationActivity.this, location.getLongitude() + " " + location.getLatitude() + "", Toast.LENGTH_SHORT).show(); Log.v("TAG", "监视地理位置变化-经纬度:"+location.getLongitude()+" "+location.getLatitude()); } } }; @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case LOCATION_CODE: if(grantResults.length > 0 && grantResults[0] == getPackageManager().PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "申请权限", Toast.LENGTH_LONG).show(); try { List providers = locationManager.getProviders(true); if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是Network locationProvider = LocationManager.NETWORK_PROVIDER; }else if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS locationProvider = LocationManager.GPS_PROVIDER; } Location location = locationManager.getLastKnownLocation(locationProvider); if (location!=null){ Toast.makeText(this, location.getLongitude() + " " + location.getLatitude() + "", Toast.LENGTH_SHORT).show(); Log.v("TAG", "获取上次的位置-经纬度:"+location.getLongitude()+" "+location.getLatitude()); }else{ // 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace locationManager.requestLocationUpdates(locationProvider, 0, 0,locationListener); } }catch (SecurityException e){ e.printStackTrace(); } } else { Toast.makeText(this, "缺少权限", Toast.LENGTH_LONG).show(); finish(); } break; } } //获取地址信息:城市、街道等信息 private List getAddress(Location location) { List result = null; try { if (location != null) { Geocoder gc = new Geocoder(this, Locale.getDefault()); result = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1); Toast.makeText(this, "获取地址信息:"+result.toString(), Toast.LENGTH_LONG).show(); Log.v("TAG", "获取地址信息:"+result.toString()); } } catch (Exception e) { e.printStackTrace(); } return result; } @Override protected void onDestroy() { super.onDestroy(); locationManager.removeUpdates(locationListener); } }