单击地图上的标记并在RecycleView中指向一个位置

时间:2017-08-10 00:59:25

标签: android google-maps firebase android-recyclerview

我希望通过点击地图上的标记,它将我引导到RecycleView中的一个位置。 我已经在地图上有标记,每个标记都有一个从Firebase检索到的名称,并在MarkerOptions中作为标题放置。 点击带有" alpha"的书签需要什么?标题打开并显示一个位置,其中还有" alpha" RecycleView里面的标题?

Code RecycleView:

public class ShowImagesActivity extends AppCompatActivity {
    //recyclerview object
    private RecyclerView recyclerView;

    //adapter object
    private RecyclerView.Adapter adapter;

    //database reference
    private DatabaseReference mDatabase;

    //progress dialog
    private ProgressDialog progressDialog;

    //list to hold all the uploaded images
    private List<Upload> uploads;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_images);


        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        progressDialog = new ProgressDialog(this);

        uploads = new ArrayList<>();

        //displaying progress dialog while fetching images
        progressDialog.setMessage("Please wait...");
        progressDialog.show();
        mDatabase = FirebaseDatabase.getInstance().getReference(Constants.DATABASE_PATH_UPLOADS);

        //adding an event listener to fetch values
        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                //dismissing the progress dialog
                progressDialog.dismiss();

                //iterating through all the values in database
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    Upload upload = postSnapshot.getValue(Upload.class);
                    uploads.add(upload);
                }
                //creating adapter
                adapter = new MyAdapter(getApplicationContext(), uploads);

                //adding adapter to recyclerview
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                progressDialog.dismiss();
            }
        });

    }
}

Code Upload.class:

public class Upload{

    public String name;
    public String description;
    public String local;
    public String url;
    public String latitude;
    public String longitude;

    // Default constructor required for calls to
    // DataSnapshot.getValue(User.class)
    public Upload() {
    }

    public Upload(String name, String description,String local,String latitude, String longitude, String url) {
        this.name = name;
        this.description = description;
        this.local = local;
        this.url= url;
        this.latitude = latitude;
        this.longitude= longitude;
    }

    public String getName() {
        return name;
    }
    public String getDescription() {
        return description;
    }
    public String getLocal() {
        return local;
    }
    public String getLongitude() {
        return longitude;
    }
    public String getLatitude() {
        return latitude;
    }

    public String getUrl() {
        return url;
    }
}

代码图:

ref.child("uploads").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                Iterator<DataSnapshot> dataSnapshotsChat =  dataSnapshot.getChildren().iterator();

                while (dataSnapshotsChat.hasNext()) {
                    DataSnapshot dataSnapshotChild = dataSnapshotsChat.next();
                    String latitudeL = dataSnapshotChild.child("latitude").getValue().toString();
                    String longitudeL = dataSnapshotChild.child("longitude").getValue().toString();
                    double latitude1 = Double.parseDouble((latitudeL));
                    double longitude1 = Double.parseDouble(longitudeL);
                    LatLng local = new LatLng(latitude1, longitude1);
                    final String title = dataSnapshotChild.child("name").getValue().toString();
                    final String url = dataSnapshotChild.child("url").getValue().toString();

                    mMap.addMarker(new MarkerOptions().position(local).title(title));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(local, 10));

                    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(Marker marker) {

                              //What I put here??
    }}}

1 个答案:

答案 0 :(得分:0)

您可以使用Google Place picker,这是更好的方式来实现&#34;放置选择器&#34;但请记住,根据他们的许可证,您只能保存该地点的ID,而不是lat或lon或者是其他东西。此外,您必须使用Google徽标制作&#34;&#34;当您使用Gmaps或放置Api时。