gpt4 book ai didi

android - 如何使用地理编码器显示位置?

转载 作者:行者123 更新时间:2023-11-29 19:43:00 24 4
gpt4 key购买 nike

我正在尝试使用 Geocoder 对象在谷歌地图中显示位置地址。为此,我制作了一个类 FetchAddressIntentService extends IntentService 并覆盖 onHandleIntent() 方法并使用显式 Intent 启动此服务。但问题是当我使用 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);
}
}

这些方法用于启动 Intent 服务:

 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();
}


}

list 文件:

<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>

最佳答案

此代码适用于从当前经纬度获取完整地址:

试试这个:

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();

关于android - 如何使用地理编码器显示位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38296355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com