我正在尝试使用Geocoder对象在Google地图中显示位置地址。 为此我创建了一个类FetchAddressIntentService扩展IntentService并覆盖onHandleIntent()方法并使用显式意图启动此服务。 但问题是当我通过使用startService(intent)onHandleIntent()方法启动服务时不会调用。
我遵循android.developer网站上提供的相同代码:
https://developer.android.com/training/location/display-address.html
这是我的代码:
public class FetchAddressIntentService extends IntentService {
protected ResultReceiver mReceiver;
public FetchAddressIntentService() {
super("abc");
}
@Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(getBaseContext(),"onHandleIntent",Toast.LENGTH_LONG).show();
String errorMessage="";
Geocoder geocoder=new Geocoder(this, Locale.getDefault());
Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA);
List<Address> addresses=null;
try {
addresses=geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
} catch (IOException e) {
errorMessage="unhendle IO exception";
Toast.makeText(getBaseContext(),"unhendle IO exception",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
if(addresses==null && addresses.size()==0){
if(errorMessage.isEmpty()){
errorMessage="no address found";
Toast.makeText(getBaseContext(),"no address found",Toast.LENGTH_LONG).show();
}
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
}
else{
Address address=addresses.get(0);
ArrayList<String> arrayListFrag=new ArrayList<>();
for (int i=0;i<address.getMaxAddressLineIndex();i++){
arrayListFrag.add(address.getAddressLine(0));
}
deliverResultToReceiver(Constants.SUCCESS_RESULT,
TextUtils.join(System.getProperty("line.separator"),
arrayListFrag));
}
}
private void deliverResultToReceiver(int successResult, String message) {
Bundle bundle=new Bundle();
bundle.putString(Constants.RESULT_DATA_KEY,message);
Toast.makeText(getBaseContext(),"resultData",Toast.LENGTH_LONG).show();
mReceiver.send(successResult,bundle);
}
}
这些方法用于启动意图服务:
protected void startIntentService(){
Intent intent=new Intent(this,FetchAddressIntentService.class);
intent.putExtra(Constants.RECEIVER, mResultReceiver);
intent.putExtra(Constants.LOCATION_DATA_EXTRA, mLastLocation);
startService(intent);
Toast.makeText(getBaseContext(),"startIntentService",Toast.LENGTH_LONG).show();
}
public void fetchAddressButtonHandler(){
if(mGoogleApiClient.isConnected() && mLastLocation!=null){
startIntentService();
Toast.makeText(getBaseContext(),"fetchAddressButtonHandler",Toast.LENGTH_LONG).show();
}
}
清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.missionandroid.googlemapproject">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FetchAddressIntentService"
android:exported="false"></service>
</application>
</manifest>
答案 0 :(得分:2)
此代码适用于从当前lat long获取完整地址:
试试这个:
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(your current lat, your current long, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();