رویدادهای کلیک را مدیریت کنید

پلتفرم مورد نظر را انتخاب کنید: اندروید، iOS، جاوا اسکریپت

ویژگی‌های داده‌ای را طوری تنظیم کنید که به رویدادهای click واکنش نشان دهند و از آنها برای تغییر ظاهر یک ویژگی بر اساس تعامل کاربر استفاده کنید.

تصویری که داده‌های چندضلعی استایل‌دهی شده را نشان می‌دهد.

نوشتن یک کنترل کننده رویداد کلیک

وقتی یک رویداد کلیک روی یک لایه ویژگی رخ می‌دهد، Maps SDK برای اندروید یک شیء FeatureClickEvent را به کنترل‌کننده رویداد ارسال می‌کند.

از متد FeatureClickEvent.getFeatures() برای دریافت لیست ویژگی‌هایی که تحت تأثیر کلیک قرار می‌گیرند، استفاده کنید.

مدیریت رویدادهای لایه ویژگی

برای مدیریت رویدادها در لایه Datasets مراحل زیر را انجام دهید. در این مثال، یک رنگ آبی برای پر کردن و حاشیه چندضلعی که نمایانگر عارضه انتخاب شده است، اعمال می‌کنید.

وقتی شما FeatureLayer.setFeatureStyle() را فراخوانی می‌کنید، تابع style factory، سبک ویژگی را روی تمام ویژگی‌های موجود در مجموعه داده تنظیم می‌کند. برای به‌روزرسانی سبک یک مجموعه داده در event handler، باید FeatureLayer.setFeatureStyle() را فراخوانی کنید تا سبک به‌روزرسانی‌شده روی تمام ویژگی‌های مجموعه داده تنظیم شود.

  1. اگر قبلاً این کار را نکرده‌اید، مراحل موجود در بخش «شروع به کار» را برای ایجاد شناسه نقشه و سبک نقشه جدید دنبال کنید. حتماً لایه ویژگی مجموعه داده‌ها را فعال کنید.

  2. مطمئن شوید که کلاس شما FeatureLayer.OnFeatureClickListener را پیاده‌سازی می‌کند.

  3. با فراخوانی FeatureLayer.addOnFeatureClickListener() یک کنترل‌کننده رویداد برای رویدادهای کلیک ویژگی ثبت کنید.

    کاتلین

    private var datasetLayer: FeatureLayer? = null
    // The globalid of the clicked dataset feature.
    var lastGlobalId: String? = null
    override fun onMapReady(googleMap: GoogleMap) { // Get the DATASET feature layer. datasetLayer = googleMap.getFeatureLayer(FeatureLayerOptions.Builder() .featureType(FeatureType.DATASET) // Specify the dataset ID. .datasetId(YOUR_DATASET_ID) .build())
    // Register the click event handler for the Datasets layer. datasetLayer?.addOnFeatureClickListener(this)
    // Apply default style to all features on load to enable clicking. styleDatasetsLayer() }
    // Define the click event handler to set lastGlobalId to globalid of selected feature. override fun onFeatureClick(event: FeatureClickEvent) { // Get the dataset feature affected by the click. val clickFeatures: MutableList<Feature> = event.features lastGlobalId = null if (clickFeatures.get(0) is DatasetFeature) { lastGlobalId = ((clickFeatures.get(0) as DatasetFeature).getDatasetAttributes().get("globalid")) // Remember to reset the Style Factory. styleDatasetsLayer() } }

    جاوا

    private FeatureLayer datasetLayer;
    // The globalid of the clicked dataset feature.
    String lastgobalid = null;
    @Override public void onMapReady(GoogleMap map) {
    // Get the DATASET feature layer. datasetLayer = map.getFeatureLayer(new FeatureLayerOptions.Builder() .featureType(FeatureType.DATASET) // Specify the dataset ID. .datasetId(YOUR_DATASET_ID) .build());
    // Register the click event handler for the Datasets layer. datasetLayer.addOnFeatureClickListener(this);
    // Apply default style to all features on load to enable clicking. styleDatasetsLayer(); }
    @Override // Define the click event handler. public void onFeatureClick(FeatureClickEvent event) { // Get the dataset feature affected by the click. List<Feature> clickFeatures = event.getFeatures(); lastgobalid = null; if (clickFeatures.get(0) instanceof DatasetFeature) { lastgobalid = ((DatasetFeature) clickFeatures.get(0)).getDatasetAttributes().get("globalid"); // Remember to reset the Style Factory. styleDatasetsLayer(); } }

  4. رنگ پر کردن را به ویژگی انتخاب شده آبی و به سایر ویژگی‌ها سبز اعمال کنید. فقط ویژگی‌های قابل مشاهده قابل کلیک هستند.

    کاتلین

    // Set fill and border for all features.
    private fun styleDatasetsLayer() {
        // Create the style factory function.
        val styleFactory = FeatureLayer.StyleFactory { feature: Feature ->
    // Check if the feature is an instance of DatasetFeature. if (feature is DatasetFeature) { val globalIDs: MutableMap<String, String> = feature.getDatasetAttributes() // Determine globalid attribute. val globalID = globalIDs!!["globalid"] // Set default colors to to green. var fillColor = 0x800000ff var strokeColor = 0xff0000ff if (globalID == lastGlobalId) { // Color selected area blue. fillColor = 0x8000ff00 strokeColor = 0xff00ff00 } return@StyleFactory FeatureStyle.Builder() .fillColor(fillColor) .strokeColor(strokeColor) .build() } return@StyleFactory null }
    // Apply the style factory function to the dataset feature layer. datasetLayer?.setFeatureStyle(styleFactory) }

    جاوا

    // Set default green fill and border for all features.
    private void styleDatasetsLayer() {
      // Create the style factory function.
      FeatureLayer.StyleFactory styleFactory = (Feature feature) -> {
    // Check if the feature is an instance of DatasetFeature. if (feature instanceof DatasetFeature) {
    // Check if "globalid" attribute of feature is the "globalid" of clicked feature. Map<String, String> globalIDs = ((DatasetFeature) feature).getDatasetAttributes(); String globalID = globalIDs.get("globalid"); // Set default colors to green. int fillColor = 0x4000ff00; int strokeColor = 0xff00ff00; if (Objects.equals(globalID, lastgobalid)) { // Color selected area blue. fillColor = 0x400000ff; strokeColor = 0xff0000ff; } return new FeatureStyle.Builder() .fillColor(fillColor) .strokeColor(strokeColor) .strokeWidth(2) .build(); } return null; };
    // Apply the style factory function to the feature layer. datasetLayer.setFeatureStyle(styleFactory); }