从纬度和经度获取地址

时间:2013-05-12 05:48:35

标签: android google-maps

我正在尝试从Android中的纬度和经度获取位置地址。 我的代码如下。

public String getAddress(double latitude, double longitude) {
        Geocoder myLocation = new Geocoder(this, Locale.getDefault());
        List<Address> myList = null;
        try {
            myList = myLocation.getFromLocation(latitude,longitude,1);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String address = myList.get(0).getAddressLine(0);
        return address;
    }

但不知怎的,我在myList = myLocation.getFromLocation(latitude,longitude,1);得到空回报 可能有什么不对?

最佳,

4 个答案:

答案 0 :(得分:2)

很常见。如果服务器没有返回任何值或者它是否过载,你将获得Null。

我遇到了类似的问题,并在以下帖子中得到了解决方案。

http://girishbhutiya.blogspot.in/2010/04/reverse-geocoding-in-android-2-2-froyo-api-level-greater-than-8.html

答案 1 :(得分:1)

你可以试试谷歌reserve-geocoding 如果您想查找地点,请尝试google place

答案 2 :(得分:0)

试试,以下代码。

try {
                    // Get the location manager
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                        Criteria criteria = new Criteria();
                        bestProvider = locationManager.getBestProvider(criteria, false);
                        Location location = locationManager
                                .getLastKnownLocation(bestProvider);
                        double lat = location.getLatitude();
                        double lon = location.getLongitude();
                        Geocoder gc = new Geocoder(this);
                        List<Address> lstAdd = gc.getFromLocation(lat, lon, 1);
                        Address ad = lstAdd.get(0);
                        String str = ad.getAddressLine(0);
                    Toast tst = Toast.makeText(this, "Your current location is " + str,
                            Toast.LENGTH_LONG);
                    tst.show();
    } catch (Exception e) {
                    Toast tst = Toast.makeText(this,"Please check your gps settings",
                            Toast.LENGTH_LONG);
                    tst.show();
    }

但是,当然试试真实的设备。

答案 3 :(得分:0)

您可以使用google api,例如使用参数插入您的经度和纬度,并向服务器和服务器发送请求,并提供带地址的响应。

public static String getAddressUrl(Context context)     {         String responseText = null;

  /*String latitude="38.89";
  String longitude ="-77.03";*/
  Log.v(TAG , "Latitude is: " + latitude + "Longitude is:"+ longitude);
  StringBuilder sbuilder=new StringBuilder();
      String googleurl="http://maps.google.com/maps/geo?"
  sbuilder.append(googleurl);

  sbuilder.append("q="+latitude+","+longitude);
  sbuilder.append("&amp;output=responseText&amp;sensor=true");
  String url=sbuilder.toString();

 Log.v(TAG, "url is: "+url); 
  try { 
      DefaultHttpClient httpclient=new DefaultHttpClient(); 
      HttpPost httppost=new HttpPost(url); 
      HttpResponse httpresponse=httpclient.execute(httppost); 
      HttpEntity httpentity=httpresponse.getEntity(); 
      InputStream is=httpentity.getContent();
      BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb=new StringBuilder();
      String line=null;
    while((line=reader.readLine())!=null)
     { 
      sb.append(line+"\n");

     } 
    is.close(); 
    responseText=sb.toString();
  } 
  catch(ClientProtocolException e) 
  { 
      e.printStackTrace(); 
  } 
  catch (IOException e) 
  { 
      e.printStackTrace();
  }
  return responseText;
  }