Android(安卓)通过经纬度获取地理位置信息
16lz
2022-04-04
首先获取LocationManager、Location及Criteria
LocationManager locationManager;String context = Context.LOCATION_SERVICE;locationManager = (LocationManager) getSystemService(context);Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);criteria.setAltitudeRequired(false);criteria.setBearingRequired(false);criteria.setCostAllowed(false);criteria.setPowerRequirement(Criteria.POWER_LOW);String provider = locationManager.getBestProvider(criteria,true);Location location = locationManager.getLastKnownLocation(provider);
Criteria 类介绍:
Criteria是“精确度”的意思,Criteria类提供若干个查询条件:
(1) public void setAccuracy( int accuracy)
位置解析的精度,高或低
参数: Criteria. ACCURACY_FINE,
精确模式;
Criteria. ACCURACY_COARSE,模糊模式;
(2) public void setAltitudeRequired(booleanaltitudeRequired)
是否提供海拔高度信息,是或否
(3) public void setBearingRequired(boolean bearingRequired)
是否提供方向信息,是或否
(4) public void setCostAllowed(boolean costAllowed)
是否允许运营商计费,是或否
(5) public void setPowerRequirement(int level)
电池消耗,无、低、中、高,参数 Criteria. NO_REQUIREMENT,Criteria. POWER_LOW,Criteria. POWER_MEDIUM, orCriteria. POWER_HIGH,
(6) public void setSpeedRequired(boolean speedRequired)
是否提供速度信息,是或否
获取地理信息函数:
private void updateWithNewLocation(Location location) {String coordinate;TextView loc = (TextView) findViewById(R.id.loc);String addressStr = "no address \n";if (location != null) {double lat = location.getLatitude();double lng = location.getLongitude();coordinate = "Latitude:" + lat + "\nLongitude:" + lng;double latitude = location.getLatitude();double longitude = location.getLongitude();Geocoder geocoder = new Geocoder(context, Locale.getDefault());try {List addresses = geocoder.getFromLocation(latitude,longitude, 1);StringBuilder sb = new StringBuilder();if (addresses.size() > 0) {Address address = addresses.get(0);for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {sb.append(address.getAddressLine(i)).append("\n");}sb.append(address.getLocality()).append("\n");sb.append(address.getLocality()).append("\n");sb.append(address.getCountryName());addressStr = sb.toString();}} catch (IOException e) {e.printStackTrace();}} else {coordinate = "no coordinate!\n";}loc.setText("your coordinate:\n" + coordinate + "\n"+ addressStr);}
更多相关文章
- Android(安卓)基于4G模块 GPS定位
- 获取Android应用程序的签名
- Android(安卓)Http 异步请求
- Android中Bitmap、Drawable、byte[]转换
- android一些技巧
- Delphi XE5 android 获取网络状态《转》
- android 语音识别
- android截屏
- Android(安卓)文件打开方式