这是布局代码
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
locationRecycler = findViewById(R.id.locations_area);
locationRecycler.setLayoutManager(layoutManager);
locationAreaAdapter = new LocationAreaAdapter(this, locations);
public void callMapPins(){
for ( markerCount=0 ; markerCount<lat.size();markerCount++){
LatLng sydney = new LatLng(Float.parseFloat(lat.get(markerCount)), Float.parseFloat(lng.get(markerCount)));
// mgoogleMap.addMarker(new MarkerOptions().position(sydney));
markerId.add( mgoogleMap.addMarker(new MarkerOptions().position(sydney)).getId()) ;
mgoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
if(markerId.contains(marker.getId())){
// Toast.makeText(insideHostelList.this, "", Toast.LENGTH_SHORT).show();
recyclerView.scrollToPosition(markerCount);
//this is not working
}
return false;
}
});
mgoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,10f));
}
}
我将标记ID存储在数组中,然后单击一个比较ID的标记,然后使用标记计数滚动到特定位置,但似乎不起作用,任何人都可以帮忙。
答案 0 :(得分:0)
将OnMarkerClickListener移出for循环。在创建标记并将其添加到地图并存储ID之后,设置markerClickListener。
public void callMapPins(){
for (markerCount=0 ; markerCount<lat.size();markerCount++){
LatLng sydney = new LatLng(Float.parseFloat(lat.get(markerCount)), Float.parseFloat(lng.get(markerCount)));
markerId.add( mgoogleMap.addMarker(new MarkerOptions().position(sydney)).getId()) ;
}
mgoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
if(markerId.contains(marker.getId())){
// Toast.makeText(insideHostelList.this, "", Toast.LENGTH_SHORT).show();
int index = markerId.indexOf(marker.getId());
recyclerView.scrollToPosition(index);
//this is not working
mgoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker.getPosition(),10f));
}
return false;
}
});
}