3

我在应用程序中使用的是 Android 4.0.3 版本。现在我想从LATITUDELONGITUDE获取城市名称或其他信息, 但是当我运行应用程序时显示服务不可用。

日志猫

  01-10 13:58:29.279: W/System.err(1211): java.io.IOException: Service not Available
    01-10 13:58:29.289: W/System.err(1211):     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
    01-10 13:58:29.299: W/System.err(1211):     at com.example.vixxa.VisitorActivity$CityAsyncTask.doInBackground(VisitorActivity.java:601)
    01-10 13:58:29.319: W/System.err(1211):     at com.example.vixxa.VisitorActivity$CityAsyncTask.doInBackground(VisitorActivity.java:1)
    01-10 13:58:29.329: W/System.err(1211):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
    01-10 13:58:29.329: W/System.err(1211):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    01-10 13:58:29.339: W/System.err(1211):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    01-10 13:58:29.349: W/System.err(1211):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    01-10 13:58:29.349: W/System.err(1211):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    01-10 13:58:29.359: W/System.err(1211):     at java.lang.Thread.run(Thread.java:856)

CodeAsynctak.java

public class CityAsyncTask extends AsyncTask<String , String,String>
    {

        @Override
        protected String doInBackground(String... params) {

             Geocoder geocoder = new Geocoder(VisitorActivity.this, Locale.getDefault());
                try
                {
                    List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
                    Log.e("Addresses","-->"+addresses);

                }
                catch (IOException e)
                {
                    e.printStackTrace();

                }

            return null;
        }

    }
4

4 回答 4

4

尝试使用此代码,它适用于我的 TAB2。

public class CityAsyncTask extends AsyncTask<String , String,String>
{

    @Override
    protected String doInBackground(String... params) {

         Geocoder geocoder = new Geocoder(LatLongActivity.this, Locale.getDefault());
            try
            {
                List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
                Log.e("Addresses","-->"+addresses);
                result = addresses.get(0).toString();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        return result;
    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        AlertDialog.Builder alert = new AlertDialog.Builder(LatLongActivity.this);
        alert.setTitle("ADDRESS");
        alert.setMessage(result);
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        alert.setCancelable(false);
        alert.show();
    }
}
于 2013-01-10T13:39:54.270 回答
3

尝试以下我在我的应用程序中使用过的代码:

Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);

        try {
            List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

            if(addresses != null) {
                Address returnedAddress = addresses.get(0);
                StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
                for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
                    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                }
                myAddress.setText(strReturnedAddress.toString());
            }
            else{
                myAddress.setText("No Address returned!");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            myAddress.setText("Can not get Address!");
        }
于 2013-01-10T09:24:41.087 回答
1

很可能您的项目的构建目标是Android 4.0而不是Google APIs. 在Eclipse中,右键项目并选择属性,进入Android,选择Google APIs

于 2013-01-10T09:14:43.417 回答
0

解决服务不可用错误

将此行作为清单中的直接子项添加到应用程序

<uses-library android:name="com.google.android.maps"/>

像这样更新你的清单

<application
        android:name="pkgname"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/AppTheme" >
        <uses-library android:name="com.google.android.maps" />

        <activity
            android:name=".yourlaunchingactivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
   <activity
             .......
     </activity>
   <activity
             ......
       </activity>
</application>
于 2015-07-21T12:57:33.407 回答