Recyclerview 数据以标记在谷歌地图中

Posted

技术标签:

【中文标题】Recyclerview 数据以标记在谷歌地图中【英文标题】:Recyclerview data to marker in Google Maps 【发布时间】:2018-06-12 18:42:15 【问题描述】:

搜索适配器

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 地图中显示标记?我之前问过这个,但找不到解决方案...

【问题讨论】:

你的意思是,当我点击 recylerview 按钮时,你想在 googlemap 活动上显示标记,我是对的 @Gowthaman M 是的,标记将基于 textview 地址 【参考方案1】:
 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);

(或) 使用以下方法从地址获取 lat 和 lng,然后在您的谷歌地图中制作标记...如果您没有获得 lat lng 值意味着您的地址无效..

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

【讨论】:

@JavaGuy 我希望这可以帮助你..如果你有任何 pbm 让我知道...有用不要忘记投票 感谢您的回答...我尝试了 intent.putExtra("address",holder.studentAddress..getText().toString();) 但收到错误消息说变量持有者是从内部类中访问的 @JavaGuy studentAddressList.get(position) 就像这样传递..make 是作为最终 int posti 的位置

以上是关于Recyclerview 数据以标记在谷歌地图中的主要内容,如果未能解决你的问题,请参考以下文章

以编程方式在谷歌地图颤动上选择一个标记

怎么在谷歌地图上做标记呀

在谷歌地图中点击标记添加信息窗口

如何在谷歌地图中显示不同颜色的最新标记

在谷歌地图标记上显示信息

如何使用我的 JSON 数据在谷歌地图 API 上创建标记?