添加clicklistener以Map标记列表并将数据发送到下一个活动?

sqxo8psd  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(334)

我在一个应用程序中工作,我需要添加超过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)));
    }
m2xkgtsf

m2xkgtsf1#

你需要使用 Marker.setTag() “用于商店” subcategoriesIds “在相应的 Marker 对象和 Marker.getTag() 把它拿回来 OnMarkerClickListener() . 类似于:

...
// wrapper class for marker custom properties
public class MarkerCategory {
    private int categoryName;
    private int categoryId;

    public MarkerCategory(String categoryName, int categoryId) {
        this.categoryName = categoryName;
        this.categoryId = categoryId;
    }

    public int getCategoryId() {
        return categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }
}
...

在这种情况下,在 createMarker() ,您应该使用 "subcategory" 属性(例如名称和id)并将其指定给要创建的标记:

protected Marker createMarker(double latitude, double longitude, String title, String snippet, int iconResID, String categoryName, int categoryId) {
        Marker markerWithTag = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .anchor(0.5f, 0.5f)
                .title(title)
                .snippet(snippet)
                 .icon(BitmapDescriptorFactory.fromResource(iconResID)));

         markerWithTag.setTag(new MarkerCategory(categoryName, categoryId));

         return markerWithTag;
}

然后,在 OnMarkerClickListener() 您可以使用 "subcategory" 属性并将其应用于活动调用:

// no need to 'for' loop to setOnMarkerClickListener
// for (int m = 0; m < subcategoriesIds.size(); m++){
//      int finalM = m;
        mMap.setOnMarkerClickListener(marker -> {

            // get object with subcategory properties
            MarkerCategory markerCategory = (MarkerCategory) marker.getTag();

            Intent mainIntent = new Intent(context , SubCategoriesDetailsActivity.class);
            Utilities.saveInt(context, "subCategoryId", markerCategory.getId());
            startActivity(mainIntent);
            return true;
        });
//    }

相关问题