JavaGuy
JavaGuy

Reputation: 39

Recyclerview data to marker in Google Maps

SearchAdapter

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 Address Value:

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

Textview Latitude Value:

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

Textview Longitude Value

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);
            }
        });
    }

Is it possible to link the buttonMap and show marker in Google Maps based on textview address or latitude and longitude? I ask this before but can't find solution...

Upvotes: 1

Views: 1381

Answers (1)

Gowthaman M
Gowthaman M

Reputation: 8282

 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

Inside Oncreate()

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

If you want to place your address in google map then easy way to use following

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

(or) Use this below method to get lat and lng from the address then make marker into your google map...if you are not getting lat lng value means you address invalid..

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;
    }
}

Try this below code to make marker into google map

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

Upvotes: 1

Related Questions