Java 除了使用地理编码器,如何在android中从lat long获取地址
我正在构建一个应用程序,使用lat-long通过Toast向用户显示当前地址。我正在使用Geocoder将lat long转换为地址,但情况是,在某些设备上,地址工作正常,但在某些设备上它崩溃了。然后我在上面的那些设备上调试应用程序崩溃了,我发现在geocoder行之后,它跳到catch(异常),而没有执行其他行。 那么,有没有其他方法可以通过lat long获得位置信息呢 还有一个问题和同样的问题,但没有给出太多的解决方案,所以我再问一遍 我的地理编码块是Java 除了使用地理编码器,如何在android中从lat long获取地址,java,android,google-maps,location,google-geocoder,Java,Android,Google Maps,Location,Google Geocoder,我正在构建一个应用程序,使用lat-long通过Toast向用户显示当前地址。我正在使用Geocoder将lat long转换为地址,但情况是,在某些设备上,地址工作正常,但在某些设备上它崩溃了。然后我在上面的那些设备上调试应用程序崩溃了,我发现在geocoder行之后,它跳到catch(异常),而没有执行其他行。 那么,有没有其他方法可以通过lat long获得位置信息呢 还有一个问题和同样的问题,但没有给出太多的解决方案,所以我再问一遍 我的地理编码块是 if (mLastLocation
if (mLastLocation != null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
try {
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude,longitude,1);
if (addresses != null && addresses.size() > 0){
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
Toast.makeText(getApplicationContext(), "Your address is: " +address+ " "+city+ " " + state+ "\n" +country+ " "+ postalCode, Toast.LENGTH_LONG).show();
}
}catch (Exception e){
e.printStackTrace();
}
您只需点击谷歌地图web服务,即可通过纬度和经度。它只是一个GET方法web服务
用您自己的lat&long替换lat和long。您可以使用此Google API获取与纬度和经度相关的完整信息
这里32是纬度,75是经度。String city=getLocationCityName(lat,lang)
公共静态字符串getLocationCityName(双lat,双lon){
JSONObject结果=getLocationFormGoogle(lat+,“+lon);
字符串city=getCityAddress(结果);
返回getCityAddress(结果);
}
受保护的静态JSONObject getLocationFormGoogle(字符串位置名称){
字符串API请求=”https://maps.googleapis.com/maps/api/geocode/json?latlng=“+placesName;//+”&ka&sensor=false”
HttpGet HttpGet=新的HttpGet(apiRequest);
HttpClient=new DefaultHttpClient();
org.apache.http.HttpResponse响应;
StringBuilder StringBuilder=新的StringBuilder();
试一试{
response=client.execute(httpGet);
HttpEntity=response.getEntity();
InputStream=entity.getContent();
int b;
而((b=stream.read())!=-1){
stringBuilder.append((char)b);
}
}捕获(客户端协议例外e){
}捕获(IOE异常){
}
JSONObject JSONObject=新的JSONObject();
试一试{
jsonObject=新的jsonObject(stringBuilder.toString());
}捕获(JSONException e){
e、 printStackTrace();
}
返回jsonObject;
}
受保护的静态字符串getCityAddress(JSONObject结果){
if(result.has(“results”)){
试一试{
JSONArray数组=result.getJSONArray(“results”);
if(array.length()>0){
JSONObject place=array.getJSONObject(0);
JSONArray组件=place.getJSONArray(“地址_组件”);
对于(int i=0;i
请参考此链接,谢谢@NancyY,但我认为您使用了相同的代码,不过我会尝试一下。在该链接中,我还添加了一个替代解决方案,您可以尝试使用它@NancyY,它可以工作,将再次尝试,谢谢..地理编码器不工作的实际原因是网络定位器在工作中被杀死。可能是因为内存不足,或者您使用任务管理器终止了所有服务?
public static String getLocationCityName(double lat, double lon) {
JSONObject result = getLocationFormGoogle(lat + "," + lon);
String city = getCityAddress(result);
return getCityAddress(result);
}
protected static JSONObject getLocationFormGoogle(String placesName) {
String apiRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + placesName; //+ "&ka&sensor=false"
HttpGet httpGet = new HttpGet(apiRequest);
HttpClient client = new DefaultHttpClient();
org.apache.http.HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
protected static String getCityAddress(JSONObject result) {
if (result.has("results")) {
try {
JSONArray array = result.getJSONArray("results");
if (array.length() > 0) {
JSONObject place = array.getJSONObject(0);
JSONArray components = place.getJSONArray("address_components");
for (int i = 0; i < components.length(); i++) {
JSONObject component = components.getJSONObject(i);
JSONArray types = component.getJSONArray("types");
for (int j = 0; j < types.length(); j++) {
if (types.getString(j).equals("locality")) {//city
String city = component.getString("long_name");
Log.d("city", city);
return component.getString("long_name");
}
if (types.getString(j).equals("postal_code")) {//pin
return component.getString("long_name");
}
if (types.getString(j).equals("country")) {//country
return component.getString("long_name");
}
if (types.getString(j).equals("administrative_area_level_1")) {//state
return component.getString("long_name");
}
if (types.getString(j).equals("administrative_area_level_2")) {
return component.getString("long_name");
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}