2016-01-20 72 views
0

我是Android新手。我開發了一個必須顯示當前位置名稱的應用程序。我試圖使用Geocoder類。但它似乎在某些電話顯示正確的位置,但在一些它顯示任何位置並變空。Android地理編碼問題

我的代碼是在這裏,

Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault()); 
    List<Address> listAddresses = null; 
    try { 
     listAddresses = geocoder.getFromLocation(gps.getLatitude(), gps.getLongitude(), 1); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    if (null != listAddresses && listAddresses.size() > 0) { 
     Location = ""; 
     for (int i = 0; i < 4; i++) { 
      if (listAddresses.get(0).getAddressLine(i) != null) { 
       Location += listAddresses.get(0).getAddressLine(i) + ","; 
      } 
     } 
     Log.e("Location", Location); 
    } 

我Manifest類,

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

我不明白是什麼問題?它取決於不同的設備?或者其他原因?

+0

可能的複製http://stackoverflow.com/questions/4761052/why-is-android-geocoder-throwing-a-service-not-available-exception – Brendon

+0

你可以檢查,如果你是否獲取當前位置的經緯度以及日誌中是否有錯誤或異常? –

+0

是的,我得到拉特和長期適當.. –

回答

0
**Just pass your current lat-long in this method and you will get address:**  

private void getAddress(double locationLatitude, double locationLongitude) { 
       Geocoder gCoder = new Geocoder(getActivity()); 
       try { 
        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.getAddressLine(0) != null) { 
          sb.append(address.getAddressLine(0)).append("\n"); 
         } 
         sb.append(address.getLocality()).append(","); 
         // sb.append(address.getPostalCode()).append(","); 
         sb.append(address.getCountryName()); 
         strAddress = sb.toString(); 
         strAddress = strAddress.replace(",null", ""); 
         strAddress = strAddress.replace("null", ""); 
         strAddress = strAddress.replace("Unnamed", ""); 
        } 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          if (!TextUtils.isEmpty(strAddress)) { 
           Logg.e("address++++", "" + strAddress); 
           edtZipcode.setFocusable(false); 
           edtZipcode.setFocusableInTouchMode(false); 
           edtZipcode.setText(strAddress); // set your current address in this edittext using your current lat-long 
           edtZipcode.setFocusable(true); 
           edtZipcode.setFocusableInTouchMode(true); 
          } else { 
           Logg.e("address", ""); 
           edtZipcode.setText(""); 
           Toast.show(getActivity(), getResources().getString(R.string.toast_enter_zipcode)); 
           new Animation(Techniques.Shake, 1000, edtZipcode); 
          } 
         } 
        }); 

       } catch (IOException exc) { 
        exc.printStackTrace(); 
       } catch (NullPointerException e) { 
        e.printStackTrace(); 
       } 
      } 
+0

其不工作棉花糖... –

0

首先創建一個意向服務來獲取位置地址。

AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.google.android.gms.location.sample.locationaddress" > 
<application 
    ... 
    <service 
     android:name=".FetchAddressIntentService" 
     android:exported="false"/> 
</application> 
... 

然後擴展了IntentService類使用onHandle

@Override 
protected void onHandleIntent(Intent intent) { 
String errorMessage = ""; 

// Get the location passed to this service through an extra. 
Location location = intent.getParcelableExtra(
     Constants.LOCATION_DATA_EXTRA); 

... 

List<Address> addresses = null; 

try { 
    addresses = geocoder.getFromLocation(
      location.getLatitude(), 
      location.getLongitude(), 
      // In this sample, get just a single address. 
      1); 
} catch (IOException ioException) { 
    // Catch network or other I/O problems. 
    errorMessage = getString(R.string.service_not_available); 
    Log.e(TAG, errorMessage, ioException); 
} catch (IllegalArgumentException illegalArgumentException) { 
    // Catch invalid latitude or longitude values. 
    errorMessage = getString(R.string.invalid_lat_long_used); 
    Log.e(TAG, errorMessage + ". " + 
      "Latitude = " + location.getLatitude() + 
      ", Longitude = " + 
      location.getLongitude(), illegalArgumentException); 
} 

// Handle case where no address was found. 
if (addresses == null || addresses.size() == 0) { 
    if (errorMessage.isEmpty()) { 
     errorMessage = getString(R.string.no_address_found); 
     Log.e(TAG, errorMessage); 
    } 
    deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage); 
} else { 
    Address address = addresses.get(0); 
    ArrayList<String> addressFragments = new ArrayList<String>(); 

    // Fetch the address lines using getAddressLine, 
    // join them, and send them to the thread. 
    for(int i = 0; i < address.getMaxAddressLineIndex(); i++) { 
     addressFragments.add(address.getAddressLine(i)); 
    } 
    Log.i(TAG, getString(R.string.address_found)); 
    deliverResultToReceiver(Constants.SUCCESS_RESULT, 
      TextUtils.join(System.getProperty("line.separator"), 
        addressFragments)); 
} 
    } 

更多的澄清,您可以使用下面的鏈接過於

Developer Android

Github Sample Project

+0

它不工作棉花糖 –