0

我正在使用下面的代码并在地址中获取空值。代码如下:-

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 块中捕获的异常是服务不可用,当我在其他设备上运行相同的代码时,它会正常执行并根据需要提供完整的地址。伙计们请尝试解决这个问题。在此先感谢!

4

2 回答 2

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>
于 2013-08-26T09:29:33.157 回答
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();
    }
于 2013-08-26T09:27:08.567 回答