Get City Name From Latitude And Longitude in android Gives Service Not Available Android 4.0.3?

醉酒当歌 提交于 2019-12-05 21:35:12

Try using this code, it worked on my 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();
    }
}

try with below code that i have Used it in my application :

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!");
        }

Most probably your project's build target is Android 4.0 instead of Google APIs. In Eclipse, right click on the project and select properties, go to Android, select Google APIs

To solve Service Not Available error

Add this line to application as a direct child in manifest

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

update your manifest like this

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!