在我的应用程序中我使用的是osm而不是谷歌地图。我有纬度和经度。所以从这里我将如何查询从osm数据库获取城市名称..plz帮助我。我正在使用osmdroid-android-3.0 .8.有没有额外的库我必须为此下载?
答案 0 :(得分:10)
这可以通过Reverse Geocoding完成,您可以将纬度/经度点转换为人类可读的地址。通常,反向地理编码是作为Web请求完成的。
在您的情况下,Open Street Maps有一个reverse geocoding API,您可以在其中向其Web服务发出请求,然后使用xml / json响应来获取城市名称。
向osm Web服务发出反向地理编码请求看起来像这样:
http://nominatim.openstreetmap.org/reverse?format=xml&lat=[LATITUDE]&lon=[LONGITUDE]&zoom=18&addressdetails=1
其中:
这会给你一个看起来像这样的结果:
<reversegeocode timestamp="Fri, 06 Nov 09 16:33:54 +0000" querystring="...">
<result place_id="1620612" osm_type="node" osm_id="452010817">
135, Pilkington Avenue, Wylde Green, City of Birmingham, West Midlands (county), B72, United Kingdom
</result>
<addressparts>
<house>135</house>
<road>Pilkington Avenue</road>
<village>Wylde Green</village>
<town>Sutton Coldfield</town>
<city>City of Birmingham</city>
<county>West Midlands (county)</county>
<postcode>B72</postcode>
<country>United Kingdom</country>
<country_code>gb</country_code>
</addressparts>
</reversegeocode>
然后,您可以将结果作为xml文档处理,以查找城市名称节点。
另一种方法是使用Google's Geolocation API。
答案 1 :(得分:6)
您可以使用nominatim reverse geocoding API进行此操作。
以下是如何获取城市名称的示例(为简洁起见,省略了一些代码):
final String requestString = "http://nominatim.openstreetmap.org/reverse?format=json&lat=" +
Double.toString(lat) + "&lon=" + Double.toString(lon) + "&zoom=18&addressdetails=1";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(requestString));
try {
@SuppressWarnings("unused")
Request request = builder.sendRequest(null, new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
if (response.getStatusCode() == 200) {
String city = "";
try {
JSONValue json = JSONParser.parseStrict(response);
JSONObject address = json.isObject().get("address").isObject();
final String quotes = "^\"|\"$";
if (address.get("city") != null) {
city = address.get("city").toString().replaceAll(quotes, "");
} else if (address.get("village") != null) {
city = address.get("village").toString().replaceAll(quotes, "");
}
} catch (Exception e) {
}
}
}
});
} catch (Exception e) {
}