[英]get place name by providing lat and long
在我的應用程序中,我希望通過提供城市來獲取我正在使用http://www.google.com/ig/api?weather= “”的天氣報告。 但對我來說只有緯度和經度可用
如何通過緯度和經度獲取地名。
僅供參考,你所做的就是反向地理編碼。
大多數Geonames Web服務都以JSON提供,包括findNearby。
谷歌還有一個反向地理編碼網絡服務
您要問的不是一件小事 - 一個lat / lng數據庫幾乎肯定會以XML或JSON形式呈現,因此在這方面投入一些時間和精力可能是值得的。 Android必須有xml / json包裝器嗎?
在這個天氣API中,沒有必要調用Geocoder
,BTW不公開,所以你應謹慎使用它, city
, state
和country
只是信息字段,所以你可以像這樣使用它(虛擬常量):
private static final String URL = "http://www.google.com/ig/api?weather=%s,%s,%s,%d,%d";
//...
final String url = String.format(URL, "city", "state", "country",
p.getLatitudeE6(), p.getLongitudeE6());
在您的方法中傳遞經度和緯度,並獲得相應的地名。 不要忘記用戶權限:
public static String getUserLocation(String lat, String lon) {
String userlocation = null;
String readUserFeed = readUserLocationFeed(lat.trim() + "," + lon.trim());
try {
JSONObject Strjson = new JSONObject(readUserFeed);
JSONArray jsonArray = new JSONArray(Strjson.getString("results"));
userlocation = jsonArray.getJSONObject(1)
.getString("formatted_address").toString();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("User Location ", userlocation);
return userlocation;
}
public static String readUserLocationFeed(String address) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"http://maps.google.com/maps/api/geocode/json?latlng=" + address
+ "&sensor=false");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(ReverseGeocode.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
您可以使用Location API獲取地址對象並從那里獲取位置
對於這個東西,Android有一個名為Geocoder的類。 查看getFromLocation
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.