我在一个应用程序中工作,我需要添加超过1针在一个时间相关的类别,相同的子类别将有相同的标记,我所做的每一件事都是完美的工作,现在我想发送类别,当我点击标记。我每次都添加了监听器,但它只对第一个索引有效,请检查我的代码,让我知道问题是什么这是显示pin的方法,我在这里添加了click监听器
private void showMarkersOnMap(SubcategoriesDataModel dataModel) {
allLocationsList = dataModel.getLocationsList();
if (markerList != null){
try {
markerList.clear();
}catch (Exception e){
Log.d("MapFragment", "RemoveMarker"+ e.toString());
}
}
for (int i = 0; i < allLocationsList.size(); i++) {
String str = allLocationsList.get(i).getLat_long();
List<String> latLong = Arrays.asList(str.split(","));
latLong.size();
int categoryId = allLocationsList.get(i).getId();
LatLng latLng = new LatLng(Double.parseDouble(latLong.get(0)),
Double.parseDouble(latLong.get(1)));
String markerTitle = allLocationsList.get(i).getName();
String snippet = allLocationsList.get(i).getAddress();
CameraPosition cameraPosition = new
CameraPosition.Builder().target(latLng).zoom(12).build();
if (mMap != null) {
switch (dataModel.getName()) {
case "Category1": {
myMarker = createMarker(latLng.latitude, latLng.longitude, markerTitle, snippet, R.drawable.category1);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
subcategoriesIds.add(categoryId);
markerList.add(myMarker);
break;
}
case "Category2": {
myMarker = createMarker(latLng.latitude, latLng.longitude, markerTitle, snippet, R.drawable.category2);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
subcategoriesIds.add(categoryId);
markerList.add(myMarker);
break;
}
case "Category3": {
myMarker = createMarker(latLng.latitude, latLng.longitude, markerTitle, snippet, R.drawable.category3);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
subcategoriesIds.add(categoryId);
markerList.add(myMarker);
break;
}
}
}
}
for (int m = 0; m < subcategoriesIds.size(); m++){
int finalM = m;
mMap.setOnMarkerClickListener(marker -> {
Intent mainIntent = new Intent(context , SubCategoriesDetailsActivity.class);
Utilities.saveInt(context, "subCategoryId", subcategoriesIds.get(finalM));
startActivity(mainIntent);
return true;
});
}
}
下面是添加标记的代码
protected Marker createMarker(double latitude, double longitude, String title, String snippet, int iconResID) {
return mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.anchor(0.5f, 0.5f)
.title(title)
.snippet(snippet)
.icon(BitmapDescriptorFactory.fromResource(iconResID)));
}
1条答案
按热度按时间m2xkgtsf1#
你需要使用
Marker.setTag()
“用于商店”subcategoriesIds
“在相应的Marker
对象和Marker.getTag()
把它拿回来OnMarkerClickListener()
. 类似于:在这种情况下,在
createMarker()
,您应该使用"subcategory"
属性(例如名称和id)并将其指定给要创建的标记:然后,在
OnMarkerClickListener()
您可以使用"subcategory"
属性并将其应用于活动调用: