在我的应用程序中,我希望通过提供城市来获取我正在使用http://www.google.com/ig/api?weather=“”的天气报告。但对我来说只有纬度和经度
如何通过纬度和经度获取地名。
答案 0 :(得分:3)
仅供参考,您正在做的事情称为反向地理编码。
大多数Geonames Web服务都以JSON提供,包括findNearby。
Google也有reverse geocoding webservice
你所要求的并不是一件小事 - 一个lat / lng数据库几乎肯定会以XML或JSON形式出现,因此在这方面投入一些时间和精力可能是值得的。 Android必须有xml / json包装器吗?
答案 1 :(得分:3)
在此天气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());
答案 2 :(得分:2)
您可以使用Location API获取Address Object并从那里获取地点
答案 3 :(得分:2)
在您的方法中传递经度和纬度,并获取相应的地名。不要忘记用户权限:
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();
}
答案 4 :(得分:1)
Android为此提供了一个名为Geocoder的课程。查看getFromLocation
方法。