我在我的应用程序中使用的是osm而不是google地图。我有纬度和经度。那么,从这里开始,我将如何查询osm数据库以获取城市名称...请帮助我。我正在使用osmdroid-android-3.0.8。是否需要下载任何额外的库?
我在我的应用程序中使用的是osm而不是google地图。我有纬度和经度。那么,从这里开始,我将如何查询osm数据库以获取城市名称...请帮助我。我正在使用osmdroid-android-3.0.8。是否需要下载任何额外的库?
这可以通过反向地理编码实现,即将纬度/经度点转换为可读的地址。通常,反向地理编码是通过网络请求完成的。
在您的情况下,Open Street Maps提供了反向地理编码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>
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) {
}