Android 使用地理编码器获取纬度和经度

Android 使用地理编码器获取纬度和经度,android,google-maps,geocoding,google-maps-api-2,Android,Google Maps,Geocoding,Google Maps Api 2,我可以知道我如何使搜索结果显示我搜索的地方的经度和纬度,而不是显示我搜索的地名吗。顺便说一句,这段代码是我从johnkil/GeocoderExample复制的。我可以知道什么是LOG_标签吗?谢谢你的回复:D package com.ccsy.mtravel; import java.io.IOException; import java.util.List; import java.util.Locale; import android.app.Activity; import andr

我可以知道我如何使搜索结果显示我搜索的地方的经度和纬度,而不是显示我搜索的地名吗。顺便说一句,这段代码是我从johnkil/GeocoderExample复制的。我可以知道什么是LOG_标签吗?谢谢你的回复: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;
            }
        }

}
package com.ccsy.mtravel;
导入java.io.IOException;
导入java.util.List;
导入java.util.Locale;
导入android.app.Activity;
导入android.content.Context;
导入android.location.Address;
导入android.location.Geocoder;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
私有静态最终字符串LOG_TAG=MainActivity.class.getSimpleName();
私有列表视图列表;
私人编辑文本编辑;
私人按钮搜索;
专用地址阵列适配器;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
v(Log_标记,“onCreate()已调用”);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(android.R.id.list);
edit=(EditText)findViewById(android.R.id.edit);
search_Btn=(按钮)findviewbyd(R.id.search_Btn);
适配器=新地址阵列适配器(此);
list.setAdapter(适配器);
search_Btn.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
new SearchTask().execute(新字符串[]{edit.getText().toString()});
}
});
如果(!Geocoder.isPresent()){
搜索设置已启用(错误);
Toast.makeText(这是“Geocoder方法getFromLocation和getFromLocationName未实现”,Toast.LENGTH_LONG.show();
}
}
私有类SearchTask扩展了AsyncTask{
@凌驾
受保护列表doInBackground(字符串…参数){
字符串locationName=params[0];
Log.v(Log_标记,String.format(“search()调用:locationName=[%s]”,locationName));
列表地址=空;
Geocoder Geocoder=新的Geocoder(getApplicationContext(),Locale.getDefault());
试一试{
地址=地理编码器。getFromLocationName(locationName,5);
}捕获(IOE异常){
Log.w(Log_标签,e);
}
返回地址;
}
@凌驾
受保护的void onPostExecute(列表结果){
if(result!=null&&!result.isEmpty()){
adapter.setData(结果);
}
}
}
私有类地址ArrayAdapter扩展了ArrayAdapter{
私人停车场平面布置;
公共地址ArrayAdapter(上下文){
super(context,android.R.layout.simple\u list\u item\u 2);
mLayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
}
公共无效设置数据(列表数据){
清除();
如果(数据!=null){
用于(地址:数据){
添加(地址);
}
}
}
私有类视窗持有者{
TextView text1;
TextView text2;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
最终持票人;
视图=转换视图;
如果(视图==null){
view=mLayoutInflater.flate(android.R.layout.simple\u list\u item\u 2,父项,false);
holder=新的ViewHolder();
holder.text1=(TextView)view.findViewById(android.R.id.text1);
holder.text2=(TextView)view.findViewById(android.R.id.text2);
视图.设置标签(支架);
}否则{
holder=(ViewHolder)view.getTag();
}
地址=获取项目(位置);
holder.text1.setText(address.getAddressLine(0));
holder.text2.setText(address.getAddressLine(1));
返回视图;
}
}
}
更改此选项:

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

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

您的函数将在此处返回物理地址: 地址=地理编码器。getFromLocationName

重新返回地址对象列表,其中指定了5个值

要返回lat long,您需要使用Address对象中的方法来获取它们:

因此,获取地址列表中的第一个地址,然后获取纬度和经度:

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


希望对您有所帮助。

log标记是您想要的任何表示日志事件的标记,大多数人使用该标记作为类名。你试过运行代码吗?发生了什么你不明白的事?