在android设备中从lat和long获取完整地址的问题

时间:2013-08-26 09:21:32

标签: android location

我使用以下代码并在地址中获取空值.Code如下: -

Geocoder geocoder;
List<Address> addresses;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    geocoder = new Geocoder(this, Locale.getDefault());
    try {
        addresses = geocoder.getFromLocation(28.5202154,77.2006815, 1);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String address = addresses.get(0).getAddressLine(0);
    String city = addresses.get(0).getAddressLine(1);
    String country = addresses.get(0).getAddressLine(2);

      Toast.makeText(
              MainActivity.this,""+address+"  "+city+"  "+country, Toast.LENGTH_LONG).show();
}

当我调试代码时,它会给出一个异常,它在catch块中被捕获服务不可用,当我在其他设备中运行相同的代码时,它会正常执行,并根据需要提供完整的地址。 伙计们请尽力解决这个问题。谢谢!

2 个答案:

答案 0 :(得分:0)

试试这种方式

   public  String getAddress(Context ctx, double latitude, double longitude) {
        StringBuilder result = new StringBuilder();
        try {
            Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);

                String locality=address.getLocality();
                String city=address.getCountryName();
                String region_code=address.getCountryCode();
                String zipcode=address.getPostalCode();
                double lat =address.getLatitude();
                double lon= address.getLongitude();

                result.append(locality+" ");
                result.append(city+" "+ region_code+" ");
                result.append(zipcode);
             Toast.makeText(
          MainActivity.this,"Address: "+result, Toast.LENGTH_LONG).show();  
            }
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }

        return result.toString();
    }

答案 1 :(得分:0)

更改密钥并确保已按以下方式添加所有权限

                   

<uses-feature
  android:glEsVersion="0x00020000"
  android:required="true"/>

<permission
      android:name="com.spotfinder.main.permission.MAPS_RECEIVE"
      android:protectionLevel="signature"/>
    <uses-permission android:name="com.spotfinder.main.permission.MAPS_RECEIVE"/>

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.mapstest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="your sha"/>

</application>
相关问题