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

标签 android google-maps

我正在尝试使用 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/

相关文章:

java - 无法将 ISO-8601 日期字符串解析为 Date

java - 从 AlertDialog 输入向 RecyclerView 添加项目

javascript - 我可以通过 Google map 自动填充获取唯一的迪拜城市地址吗?

c# - Google map - 从结果列表中显示标记窗口

javascript - 谷歌地图 : place ID array over 10 doesn't work

android - 如何允许您的应用程序使用访问权限?

Android - PullToRefresh ListView 始终为空

javascript - InfoWindow 图形 "messed up"使用 Google Maps API v3

android - 在 Mono for Android 中添加 Google Maps API v2

android - 如何在多个布局中重用 ImageView