2013-04-03 25 views
0

我試圖找出從谷歌地圖API的位置,名稱的android時,我有位置的經度和緯度。我想要實現的是,獲取地點名稱後,我要拍一個短信給我的朋友,告訴他們我的當前位置。我不確定是否需要打開地理編碼器服務。當我有經度和緯度時如何顯示位置名稱?

這是我迄今所做的,現在讀音字卡住。

class mylocationlistener implements LocationListener 
{ 


    @SuppressLint("NewApi") 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 
     if(location != null) 
     { 
      Double longi = location.getLongitude(); 
      Double lat = location.getLatitude(); 
      String str = ""; 

      str= "Longitude=" + longi + ", Latitude=" + lat; 
      Toast.makeText(getApplicationContext(),str,Toast.LENGTH_LONG).show(); 

      Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault()); 

      boolean abc = Geocoder.isPresent(); 
      try { 
       List<Address> addresses = geocoder.getFromLocation(lat, longi, 1); 
       if(!addresses.isEmpty()) 
       { 
       str = addresses.get(0).getLocality() + addresses.get(0).getAddressLine(1)+ addresses.get(0).getAddressLine(2); 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      Toast.makeText(getApplicationContext(),str, Toast.LENGTH_LONG).show(); 
     } 
    } 
    } 
+0

的代碼似乎是正確的。你有什麼錯誤嗎?請更好地描述你的問題。 –

+0

列表

地址= geocoder.getFromLocation(LAT,茇,1); 在這個語句上的地址是空的。 –

+0

嘗試反向GeoCodeing和檢查此鏈接http://mobiforge.com/developing/story/using-google-maps-android –

回答

0

我想你幾乎用它做,我該怎麼處理這之前,使用下面的代碼:

public static String getAddressStringFromLocation(Context context, Location location) { 
    GeoPoint gp = new GeoPoint((int) (location.getLatitude() * 1e6), 
      (int) (location.getLongitude() * 1e6)); 
    return getAddressStringFromGeoPoint(context, gp); 
} 

public static String getAddressStringFromGeoPoint(Context context, GeoPoint point) { 
    StringBuilder sb = new StringBuilder(); 
    Address ad = getAddressFromGeoPoint(context, point); 
    if(ad != null && ad.getMaxAddressLineIndex() > 0) { 
     for(int i = 0, max = ad.getMaxAddressLineIndex(); i < max; i++) { 
      sb.append(ad.getAddressLine(i)); 
     } 
     sb.append(ad.getThoroughfare()); 
     return sb.toString(); 
    } else { 
     return null; 
    } 
} 

public static Address getAddressFromGeoPoint(Context context, GeoPoint point){ 
    Geocoder geoCoder = new Geocoder(context, Locale.CHINA); 
    Address address = null; 
    try { 
     List<Address> addresses = geoCoder.getFromLocation(point.getLatitudeE6()/1E6, point.getLongitudeE6()/1E6, 1); 
     address = addresses.get(0); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return address; 
} 
+0

這個代碼不返回任何地址 –

+0

它在返回字符串中的最終地址名稱,以便ü可以作爲短信發送給其他人 – Longerian

+0

但它返回你改變'Locale' PARAM –

相關問題