2013-05-12 117 views
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); 返回null可能是什麼問題?

最好,

+0

的可能重複[如何從緯度和經度完整地址?(http://stackoverflow.com/questions/9409195/how-to-get-complete-address-from-latitude-and-經度) – Raghunandan 2013-05-12 06:01:16

回答

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(); 
    } 

但是,當然嘗試在真正的設備上。

+0

是的,我在真正的設備上工作。同樣的問題happended.I <使用權限android:name =「android.permission.INTERNET」/><使用權限android:name =「android.permission.ACCESS_FINE_LOCATION」/>我還缺少什麼。 – Bryanyan 2013-05-12 06:01:05

0

您可以通過谷歌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; 
    }