2013-07-18 125 views
0

我期待在android中實現反向地理編碼,但我有一些問題可以有人給我一些方向? 這裏是我的代碼:反向地理編碼實現

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     textLong = (TextView) findViewById(R.id.textLong); 
     textLat = (TextView) findViewById(R.id.textLat); 


     LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     LocationListener listener = new myLocationListner(); 
     manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener); 




    } 

    public class myLocationListner implements LocationListener{ 

     @Override 
     public void onLocationChanged(Location location) { 
      if (location != null){ 

       double gettextLong = location.getLongitude(); 
       double gettextLat = location.getLatitude(); 

       textLat.setText(Double.toString(gettextLat)); 
       textLong.setText(Double.toString(gettextLong)); 


      } 
+0

谷歌api提供反向地理編碼http://maps.googleapis.com/maps/api/geocode/json?l atlng =「+ latitude +」,「+ longitude +」&sensor = false –

回答

0

你需要創建一個地理編碼對象,並使用.getFromLocation方法:

Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
List<Address> addresses = geocoder.getFromLocation(gettextLat, getTextLong, 1); 

即:

public class myLocationListner implements LocationListener{ 

    Geocoder mGeocoder; 

    // default constructor 
    myLocationListener() { 
     // instantiate geocoder object: 
     mGeocoder = new Geocoder(this, Locale.getDefault()); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null){ 

      double gettextLong = location.getLongitude(); 
      double gettextLat = location.getLatitude(); 

      textLat.setText(Double.toString(gettextLat)); 
      textLong.setText(Double.toString(gettextLong)); 

      List<Address> addresses = mGeocoder.getFromLocation(gettextLat, getTextLong, 1); 

     } 
    } 

見:http://developer.android.com/reference/android/location/Geocoder.html

+0

的列表上行我得到一個錯誤mGeocoder無法解析我試圖在onlocChanged內聲明地理編碼器obj,但得到了相同的錯誤\ – ffs

+0

對不起 - 應該在'onLocationChanged'函數中'mGeocoder',而不是'goecoder'。我編輯了答案。 – ccbunney

+0

沒有問題,但我沒有注意到地理編碼器,並用mGeocoder替換它,但它認爲mGeocoder無法在該行上解析。它沒有訪問在onLocationChanged方法中創建的地理編碼器嗎? – ffs