A class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.
https://developers.google.com/maps/documentation/geocoding/
http://stackoverflow.com/questions/9272918/service-not-available-in-geocoder
package com.example.android.location;
import java.util.List;
import java.util.Locale;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONArray;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Log;
public class GeocoderHelper {
private static final AndroidHttpClient ANDROID_HTTP_CLIENT = AndroidHttpClient.newInstance(GeocoderHelper.class.getName());
private boolean running = false;
@SuppressLint("NewApi")
public void fetchCityName(final Context contex, final Location location) {
if (running)
return;
new AsyncTask<Void, Void, String>() {
protected void onPreExecute() {
running = true;
};
@Override
protected String doInBackground(Void... params) {
String cityName = null;
if (Geocoder.isPresent()) {
try {
Geocoder geocoder = new Geocoder(contex, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
if (addresses.size() > 0) {
cityName = addresses.get(0).getLocality();
}
} catch (Exception ignored) {
// after a while, Geocoder start to trhow
// "Service not availalbe" exception. really weird since
// it was working before (same device, same Android
// version etc..
}
}
if (cityName != null) // i.e., Geocoder succeed
{
return cityName;
} else // i.e., Geocoder failed
{
return fetchCityNameUsingGoogleMap();
}
}
// Geocoder failed :-(
// Our B Plan : Google Map
private String fetchCityNameUsingGoogleMap() {
String googleMapUrl = "http://maps.googleapis.com/maps/api/geocode/json?latlng="
+ location.getLatitude() + "," + location.getLongitude()
+ "&sensor=false&language=zh-CN";
try {
JSONObject googleMapResponse = new JSONObject(ANDROID_HTTP_CLIENT.execute(new HttpGet(googleMapUrl),
new BasicResponseHandler()));
// many nested loops.. not great -> use expression instead
// loop among all results
JSONArray results = (JSONArray) googleMapResponse.get("results");
for (int i = 0; i < results.length(); i++) {
// loop among all addresses within this result
JSONObject result = results.getJSONObject(i);
if (result.has("address_components")) {
JSONArray addressComponents = result.getJSONArray("address_components");
// loop among all address component to find a
// 'locality' or 'sublocality'
for (int j = 0; j < addressComponents.length(); j++) {
JSONObject addressComponent = addressComponents.getJSONObject(j);
if (result.has("types")) {
JSONArray types = addressComponent.getJSONArray("types");
// search for locality and sublocality
String cityName = null;
for (int k = 0; k < types.length(); k++) {
if ("locality".equals(types.getString(k)) && cityName == null) {
if (addressComponent.has("long_name")) {
cityName = addressComponent.getString("long_name");
} else if (addressComponent.has("short_name")) {
cityName = addressComponent.getString("short_name");
}
}
if ("sublocality".equals(types.getString(k))) {
if (addressComponent.has("long_name")) {
cityName = addressComponent.getString("long_name");
} else if (addressComponent.has("short_name")) {
cityName = addressComponent.getString("short_name");
}
}
}
if (cityName != null) {
return cityName;
}
}
}
}
}
} catch (Exception ignored) {
ignored.printStackTrace();
}
return null;
}
protected void onPostExecute(String cityName) {
running = false;
if (cityName != null) {
// Do something with cityName
Log.i("GeocoderHelper", cityName);
}
};
}.execute();
}
}
相关推荐
《使用Java调用百度地图Geocoder API实现文字地址到经纬度转换》 在现代的地理信息系统(GIS)中,将地址转换为精确的经纬度坐标是至关重要的任务,这一过程通常被称为“地理编码”(Geocoding)。在本文中,我们将...
ol-geocoder, OpenLayers的Geocoder Nominatim OpenLayers控制编码器 用于 的编码器扩展。 需要 OpenLayers或者更高。 演示你可以在这里看到演示或者在 jsFiddle,如果你愿意。 还有一个用于创建自定义提供程序插件...
Java中的Geocoder是一个用于地理编码和反向地理编码的接口,它允许开发人员将地址转换为经纬度坐标或将坐标转换回地址。在Java应用程序中,如果你需要处理与地理位置相关的任务,比如地图显示、导航或者位置服务,...
在本项目中,我们关注的是"Laravel开发-geocoder .zip"这个压缩包,它显然与使用Laravel框架进行Web应用开发以及geocoding(地理编码)有关。Geocoding是将地址转换为地理坐标(如经度和纬度)的过程,这对于实现...
$adapter, '<MAXMIND_API_KEY>', $service, $useSsl ), new \Geocoder\Provider\ArcGISOnline( $adapter, $sourceCountry, $useSsl ), ]); $geocoder->registerProvider( new \Geocoder\...
【标题】"前端项目-esri-leaflet-geocoder.zip"涉及的是一个前端开发中的地理信息系统(GIS)应用,主要利用了Esri和Leaflet两个库来实现地理编码和地图搜索功能。 【描述】中提到的"ESRI地理编码实用程序"是指Esri...
前端项目-perliedman-leaflet-control-geocoder,可扩展的地理编码,内置支持nomingim、bing、google、mapbox、photon、what3words、mapquest、mapzen,此处
楼主实战,根据自身需求加在对应位置即可————————————————强调!强调!强调!,里面代码功能只包含...当点击获取坐标功能按钮,实时获取经纬度传输到文本框,然后通过Geocoder工具进行逆向地理编码。
Java Geocoder关于PinPoint地理编码项目Java Geocoder是基于Java,Spring,Hibernate,PostgreSQL,美国人口普查数据和辛苦工作建立的开源地理编码服务! 通过添加Geocoder模块,该应用程序分为多个层:控制器,服务...
导入package:geocoder/geocoder.dart ,然后使用Geocoder.local来访问设备系统提供的地理编码服务。 例子: import 'package:geocoder/geocoder.dart' ; // From a query final query = "1600 Amphiteatre Parkway...
2. 调用`getFromLocation()`方法,传入经纬度参数:`List<Address> addresses = geocoder.getFromLocation(latitude, longitude, maxResults);` 3. `getFromLocation()`返回一个Address对象的列表,这些对象包含了...
楼主实战,根据自身需求加在对应位置即可————————————————强调!强调!强调!,里面代码功能只包含通过获取经纬度坐标查找出对应的地址信息(地址信息=省+市+区+乡镇+具体信息(道路等等);...
addresses = geocoder.getFromLocation(latitude, longitude, 1); // 1表示最多返回一条地址结果 if (!addresses.isEmpty()) { Address address = addresses.get(0); String city = address.getLocality(); // ...
此 Geocoder 具有与 android.location.Geocoder 类似的 API,但它是独立于设备的实现并提供更丰富的 Address 对象。 有关更多详细信息,请参阅示例项目。 最低 API 级别 7 将此添加到 build.gradle 依赖项中,将 ...
地理名称地理编码器一个Solr SearchComponent,用于根据一组预先初始化的地理... 运行索引器: java -cp geonames-geocoder-0.0.1.jar in.geocoder.component.geocoder.util.Indexer US.txt 试用查询: 执照Apache 2.0
适用于Google Maps API的Node.js Batch Geocoder 例子 var Geocoder = require ( "../lib/batch-geocoder" ) , geocoder = new Geocoder ( "./geocode-cache.csv" ) ; geocoder . on ( "finish" , function ( ...
addresses = geocoder.getFromLocation(latitude, longitude, 1); // 1 表示最多返回1个结果 } catch (IOException e) { e.printStackTrace(); } if (addresses != null && !addresses.isEmpty()) { Address ...
OpenCage地理编码器 地理编码API的Ruby...geocoder = OpenCage :: Geocoder . new ( api_key : 'your-api-key-here' ) 对地址或地名进行地理编码 results = geocoder . geocode ( '82 Clerkenwell Road, London' )
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (addresses != null && !addresses.isEmpty()) { Address address = addresses.get(0); return address....
addresses = geocoder.getFromLocation(latitude, longitude, 1); // 获取最近的一个地址 } catch (IOException e) { // 处理异常情况 return; } if (!addresses.isEmpty()) { Address address = addresses....