2017-11-08 7 views
0

私は以下のコードを、人間が読める長いアドレスに変換することができます。今、私は、アドレス、市、郵便番号&を含むアドレスの完全な詳細を取得していて、「インド」のような郡名のみを取得しています。どのように私は緯度&経度に基づいて正確なアドレスを取得できますか? 私を助けてください。 Geocoderオブジェクトから緯度、経度からジオコーディングされた住所を取得するにはどうすればよいですか?

final List<Address> list = gCoder.getFromLocation(locationLatitude, locationLongitude, 1); 
     if (list != null && list.size() > 0) { 
      Address address = list.get(0); 
      StringBuilder sb = new StringBuilder(); 

      if (address.getMaxAddressLineIndex() > 0) { 
       for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { 
        sb.append(address.getAddressLine(i)).append(","); 
       } 
       sb.append(address.getCountryName()); 
      } else { 
       try { 
        sb.append(address.getAddressLine(0)); 
       } catch (Exception ignored) { 
       } 
      } 

      strAddress = sb.toString(); 
      strAddress = strAddress.replace(" ", ""); 
      strAddress = strAddress.replace(",null", ""); 
      strAddress = strAddress.replace("null", ""); 
      strAddress = strAddress.replace("Unnamed", ""); 
     } 
     getActivity().runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       if (!TextUtils.isEmpty(strAddress)) { 
        tvLocation.setText(strAddress); 
        Log.e("Location", strAddress + ""); 
       }else { 
        Snackbar.make(getView(), "Couldn't get the location. Make sure location is enabled on the device", Snackbar.LENGTH_SHORT) 
          .show(); 
       } 
      } 
     }); 
+0

ここで確認できます。 https://stackoverflow.com/a/9409229/5580210 –

答えて

0

、あなたはgetFromLocation(double, double, int)メソッドを呼び出すことができます。

Geocoder gcd = new Geocoder(context, Locale.getDefault()); 
    List<Address> addresses = gcd.getFromLocation(lat, lng, 1); 
    if (addresses.size() > 0) { 
     System.out.println(addresses.get(0).getLocality()); 
    } 
    else { 
     // do your stuff 
    } 
+0

場所が不正確です – Gopal909

関連する問題