使用Geocoder获取经度和纬度

时间:2013-12-02 18:35:44

标签: android google-maps geocoding google-maps-api-2

我可以知道如何制作搜索结果,显示我搜索的地方的经度和纬度,而不是显示我搜索的地名。顺便说一句,我从johnkil / GeocoderExample复制了这段代码。我可以知道什么是LOG_TAG吗?谢谢你的回复:D

package com.ccsy.mtravel;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
        private static final String LOG_TAG = MainActivity.class.getSimpleName();

        private ListView list;
        private EditText edit;
        private Button search_Btn;

        private AddressArrayAdapter adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
            Log.v(LOG_TAG, "onCreate() called");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = (ListView) findViewById(android.R.id.list);
        edit = (EditText) findViewById(android.R.id.edit);
        search_Btn = (Button) findViewById(R.id.search_btn);

        adapter = new AddressArrayAdapter(this);
        list.setAdapter(adapter);

        search_Btn.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                new SearchTask().execute(new String[] {edit.getText().toString()});
                        }
                });

        if (!Geocoder.isPresent()) {
                search_Btn.setEnabled(false);
                Toast.makeText(this, "Geocoder methods getFromLocation and getFromLocationName are not implemented", Toast.LENGTH_LONG).show();
        }
    }

    private class SearchTask extends AsyncTask<String, Void, List<Address>> {

                @Override
                protected List<Address> doInBackground(String... params) {
                        String locationName = params[0];
                        Log.v(LOG_TAG, String.format("search() called: locationName=[%s]", locationName));
                        List<Address> addresses = null;
                    Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
                    try {
                            addresses = geocoder.getFromLocationName(locationName, 5);
                    } catch (IOException e) {
                                Log.w(LOG_TAG, e);
                        }
                        return addresses;
                }

                @Override
                protected void onPostExecute(List<Address> result) {
                        if (result != null && !result.isEmpty()) {
                                adapter.setData(result);
                        }
                }

    }

    private class AddressArrayAdapter extends ArrayAdapter<Address> {

            private LayoutInflater mLayoutInflater;

            public AddressArrayAdapter(Context context) {
            super(context, android.R.layout.simple_list_item_2);
            mLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

            public void setData(List<Address> data) {
            clear();
            if (data != null) {
                for (Address address : data) {
                    add(address);
                }
            }
        }

            private class ViewHolder {
                        TextView text1;
                TextView text2;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                    final ViewHolder holder;
                    View view = convertView;
                if (view == null) {
                    view = mLayoutInflater.inflate(android.R.layout.simple_list_item_2, parent, false);
                    holder = new ViewHolder();
                    holder.text1 = (TextView) view.findViewById(android.R.id.text1);
                    holder.text2 = (TextView) view.findViewById(android.R.id.text2);
                    view.setTag(holder);
                } else {
                        holder = (ViewHolder) view.getTag();
                }
                Address address = getItem(position);
                holder.text1.setText(address.getAddressLine(0));
                    holder.text2.setText(address.getAddressLine(1));
                return view;
            }
        }

}

2 个答案:

答案 0 :(得分:0)

改变这个:

holder.text1.setText(address.getAddressLine(0));
holder.text2.setText(address.getAddressLine(1));  

对此:

holder.text1.setText(address.getLatitude());
holder.text2.setText(address.getLongitude());

答案 1 :(得分:0)

您的功能是在这里返回物理地址:  addresses = geocoder.getFromLocationName ...

重新生成一个地址对象列表,其中包含5个指定值。

要返回lat long,您需要使用Address对象中的方法来获取它们: http://developer.android.com/reference/android/location/Address.html

所以在地址列表中获取第一个地址,然后获取getLatitude&amp; getLongitude:

addresses.get(0).getLatitude(); addresses.get(0).getLongitude();

希望有所帮助。