在我的应用程序中,我想通过提供城市来获取我正在使用的天气报告http://www.google.com/ig/api?weather= "" 。但对我来说只有纬度和经度可用
如何通过纬度和经度获取地名。
在我的应用程序中,我想通过提供城市来获取我正在使用的天气报告http://www.google.com/ig/api?weather= "" 。但对我来说只有纬度和经度可用
如何通过纬度和经度获取地名。
不需要Geocoder
在这个天气 API 中调用 , 顺便说一句,它是不公开的,所以你应该谨慎使用它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());
仅供参考,您正在做的事情称为反向地理编码。
大多数 Geonames Web 服务都以 JSON 格式提供,包括 findNearby。
谷歌还有一个反向地理编码网络服务
您要问的不是一件小事——lat/lng 数据库几乎肯定会以 XML 或 JSON 的形式呈现,因此在这方面投入一点时间和精力可能是值得的。Android 必须有 xml/json 包装器?
您可以使用Location API获取地址对象并从那里获取位置
在你的方法中传入经纬度,得到对应的地名。不要忘记用户权限:
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();
}
Android 有一个名为Geocoder的类来处理这些东西。看getFromLocation
方法。