2017-08-10 10 views
1

マップ上のマーカーをクリックすると、RecycleView内の位置に移動されます。 私はすでにマップ上にマーカーを持っていますが、それぞれマーカーはFirebaseから取り出され、MarkerOptionsのタイトルとして配置されています。 "alpha"タイトルが開いているブックマークをクリックし、RecycleViewの中にある "alpha"タイトルがある位置を表示するにはどうすればよいですか?マップ上のマーカーをクリックし、RecycleView内の位置を指示します

コード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(); 
      } 
     }); 

    } 
} 

コード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?? 
    }}} 

答えて

0

あなたは「場所ピッカーを実現しないより良い方法である、Google Place pickerを使用することができます"、しかし、彼らのライセンスによると、あなたは緯度や経度ではなく、その場所のIDだけを保存することができますその他のまた、GmapsやApiを使用しているときは、「made by Google logo」を置く必要があります。

+0

別のオプション? – FranciscoM

0

はあなたがそれぞれのマーカーのためのArrayListにアップロードモデルクラスオブジェクトの位置を取得する必要に応じてその前に

  1. をコードを変更します。

  2. オブジェクトの位置をインテントで渡し、ShowImagesActivityアクティビティで処理します。

  3. 使用recyclerview.getLayoutManager().scrollToPosition(youPositionInTheAdapter).

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { intent = new Intent(this, ShowImagesActivity.class); intent.putExtra(ITEM_POSITION, Adapterposition); } }

+0

上記のコードを更新しました。アップロードモデルクラスのオブジェクト位置を取得する方法を知る必要があります。MapActivityとShowImagesActivityの両方で同じデータセットを維持する場合は、 – FranciscoM

+0

をクリックして、マーカーのオブジェクト位置を取得できます。その位置をインテントを通して次のアクティビティに渡すことができます。 – Logo

関連する問題