博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Displaying the Location Address
阅读量:4046 次
发布时间:2019-05-24

本文共 3241 字,大约阅读时间需要 10 分钟。

As shown in previous lessons, location updates are received in the form of latitude and longitude coordinates. While this format is useful for calculating distance or displaying a pushpin on a map, the decimal numbers make no sense to most end users. If you need to display a location to user, it is much more preferable to display the address instead.

Perform Reverse Geocoding

Reverse-geocoding is the process of translating latitude longitude coordinates to a human-readable address. The API is available for this purpose. Note that behind the scene, the API is dependent on a web service. If such service is unavailable on the device, the API will throw a "Service not Available exception" or return an empty list of addresses. A helper method called was added in Android 2.3 (API level 9) to check for the existence of the service.

The following code snippet demonstrates the use of the API to perform reverse-geocoding. Since the method is synchronous, you should not invoke it from the UI thread, hence an is used in the snippet.

private final LocationListener listener = new LocationListener() {    public void onLocationChanged(Location location) {        // Bypass reverse-geocoding if the Geocoder service is not available on the        // device. The isPresent() convenient method is only available on Gingerbread or above.        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD && Geocoder.isPresent()) {            // Since the geocoding API is synchronous and may take a while.  You don't want to lock            // up the UI thread.  Invoking reverse geocoding in an AsyncTask.            (new ReverseGeocodingTask(this)).execute(new Location[] {location});        }    }    ...};// AsyncTask encapsulating the reverse-geocoding API.  Since the geocoder API is blocked,// we do not want to invoke it from the UI thread.private class ReverseGeocodingTask extends AsyncTask
{ Context mContext; public ReverseGeocodingTask(Context context) { super(); mContext = context; } @Override protected Void doInBackground(Location... params) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); Location loc = params[0]; List
addresses = null; try { // Call the synchronous getFromLocation() method by passing in the lat/long values. addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); } catch (IOException e) { e.printStackTrace(); // Update UI field with the exception. Message.obtain(mHandler, UPDATE_ADDRESS, e.toString()).sendToTarget(); } if (addresses != null &s;&s; addresses.size() > 0) { Address address = addresses.get(0); // Format the first line of address (if available), city, and country name. String addressText = String.format("%s, %s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getLocality(), address.getCountryName()); // Update the UI via a message handler. Message.obtain(mHandler, UPDATE_ADDRESS, addressText).sendToTarget(); } return null; }}

转载地址:http://axgdi.baihongyu.com/

你可能感兴趣的文章
Golang 数据可视化利器 go-echarts ,实际使用
查看>>
mysql 跨机器查询,使用dblink
查看>>
mysql5.6.34 升级到mysql5.7.32
查看>>
dba 常用查询
查看>>
Oracle 异机恢复
查看>>
Oracle 12C DG 搭建(RAC-RAC/RAC-单机)
查看>>
Truncate 表之恢复
查看>>
Oracle DG failover 后恢复
查看>>
mysql 主从同步配置
查看>>
为什么很多程序员都选择跳槽?
查看>>
mongdb介绍
查看>>
mongdb安装使用
查看>>
mongdb在java中的应用
查看>>
区块链技术让Yotta企业云盘为行政事业服务助力
查看>>
Yotta企业云盘更好的为媒体广告业服务
查看>>
Yotta企业云盘助力旅游行业新发展
查看>>
Yotta企业云盘助力科技行业创高峰
查看>>
Yotta企业云盘更好地为教育行业服务
查看>>
Yotta企业云盘怎么帮助到能源化工行业
查看>>
企业云盘如何助力商业新发展
查看>>