0

我正在尝试从 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

4 回答 4

2

这很常见。如果服务器不返回任何值或过载,您将获得 Null。

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

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

于 2013-05-12T06:02:16.203 回答
1

如果你想找地方,你可以试试 google reserve-geocoding ,试试google place

于 2013-05-12T06:04:06.250 回答
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();
    }

但是,当然要在真机上试一试。

于 2013-05-12T05:51:35.957 回答
0

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

公共静态字符串 getAddressUrl(上下文上下文){ 字符串 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;
  }
于 2013-05-12T06:09:15.833 回答