android - 在 Google map 中标记 Recyclerview 数据

标签 android google-maps android-intent android-recyclerview

搜索适配器

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> {

    Context context;
    ArrayList<String> studentNameList;
    ArrayList<String> studentMatrikList;
    ArrayList<String> studentPhoneList;
    ArrayList<String> studentAddressList;
    ArrayList<String> studentLatList;
    ArrayList<String> studentLngList;

    class SearchViewHolder extends RecyclerView.ViewHolder{

        TextView studentName, studentMatrik, studentPhone, studentAddress, studentLat, studentLng;
        CheckBox checkBox;
        Button buttonMap;



        public SearchViewHolder(View itemView) {
            super(itemView);
            studentName = (TextView) itemView.findViewById(R.id.studentName);
            studentMatrik = (TextView) itemView.findViewById(R.id.studentMatrik);
            studentPhone = (TextView) itemView.findViewById(R.id.studentPhone);
            studentAddress = (TextView) itemView.findViewById(R.id.studentAddress);
            studentLat = (TextView) itemView.findViewById(R.id.studentLat);
            studentLng = (TextView) itemView.findViewById(R.id.studentLng);
            checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
            buttonMap = (Button) itemView.findViewById(R.id.buttonMap);
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    if (isChecked) {
                        Toast.makeText(SearchAdapter.this.context,
                                "Selected student is " + studentName.getText(),Toast.LENGTH_LONG).show();
                    } else {

                    }
                }
            });
        }

    }

    public SearchAdapter(Context context, ArrayList<String> studentNameList, ArrayList<String> studentMatrikList, ArrayList<String> studentPhoneList, ArrayList<String> studentAddressList, ArrayList<String> studentMailList, ArrayList<String> studentConList) {
        this.context = context;
        this.studentNameList = studentNameList;
        this.studentMatrikList = studentMatrikList;
        this.studentPhoneList = studentPhoneList;
        this.studentAddressList = studentAddressList;
        this.studentMailList = studentMailList;
        this.studentConList = studentConList;

    }

    @Override
    public SearchAdapter.SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_layout, parent, false);
        return new SearchAdapter.SearchViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SearchViewHolder holder, int position) {
        holder.studentName.setText(studentNameList.get(position));
        holder.studentMatrik.setText(studentMatrikList.get(position));
        holder.studentPhone.setText(studentPhoneList.get(position));
        holder.studentAddress.setText(studentAddressList.get(position));
        holder.studentLat.setText(studentMailList.get(position));
        holder.studentLng.setText(studentConList.get(position));
        holder.buttonMap.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MapsActivity.class);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {

        return studentNameList.size();
    }
}

Textview 地址值:

studentAddress = (TextView) itemView.findViewById(R.id.studentAddress);

Textview 纬度值:

studentLat = (TextView) itemView.findViewById(R.id.studentLat);

Textview 经度值

studentLng = (TextView) itemView.findViewById(R.id.studentLng);



 @Override
    public void onBindViewHolder(SearchViewHolder holder, int position) {
        holder.studentName.setText(studentNameList.get(position));
        holder.studentMatrik.setText(studentMatrikList.get(position));
        holder.studentPhone.setText(studentPhoneList.get(position));
        holder.studentAddress.setText(studentAddressList.get(position));
        holder.studentLat.setText(studentMailList.get(position));
        holder.studentLng.setText(studentConList.get(position));
        holder.buttonMap.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MapsActivity.class);
                context.startActivity(intent);
            }
        });
    }

是否可以根据 TextView 地址或经纬度链接 buttonMap 并在 Google map 中显示标记?我之前问过这个但找不到解决方案...

最佳答案

 holder.buttonMap.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MapsActivity.class);
                intent.putExtra("address",holder.studentAddress..getText().toString();)
                context.startActivity(intent);
            }
        });

MapsActivity.class

内部 Oncreate()

String searchAddress = getIntent().getStringExtra("address");

如果您想将您的地址放在谷歌地图中,​​那么使用以下简单方法

Intent searchAddress = new  Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);

(或) 使用下面的方法从地址获取经纬度,然后在您的谷歌地图中做标记...如果您没有获得经纬度值,则意味着您的地址无效..

public GeoPoint getLocationFromAddress(String searchAddress){

Geocoder coder = new Geocoder(this);
List<Address> address;
GeoPoint p1 = null;

try {
    address = coder.getFromLocationName(searchAddress,5);
    if (address==null) {
       return null;
    }
    Address location=address.get(0);
    location.getLatitude();
    location.getLongitude();

    p1 = new GeoPoint((double) (location.getLatitude() * 1E6),
                      (double) (location.getLongitude() * 1E6));

    return p1;
    }
}

尝试使用下面的代码将标记制作成谷歌地图

if (latitude != null && longitude != null)  
     {  
     mGoogleMap2.addMarker(new MarkerOptions()  
        .position(new LatLng(latitude, longitude)));  
    mGoogleMap2.moveCamera(CameraUpdateFactory.newLatLngZoom(  
       new LatLng(latitude, longitude), 10));  
     } 

Reference here how to get latlng from address

关于android - 在 Google map 中标记 Recyclerview 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48071476/

相关文章:

javascript - 如何在谷歌地图中使用信息窗口更改样式

Android Intent 关注 Twitter 用户?

caching - Google 静态 map 自定义标记会缓存多长时间?

android - Intent 在ActivityResult 上返回空值

android - 接收长按音量键

android - 在 R.attr 中找到正确的样式

android - 与 View 动画作斗争

android - android中的手势是什么?

android - 当前位置的纬度和经度

google-maps - AngularUI - 谷歌地图 - map 未定义