Webmap keyword search

View on GitHub
Sample viewer app

Find webmap portal items by using a search term.

Image of search for webmap

Use case

Portals can contain many portal items and at times you may wish to query the portal to find what you're looking for. In this example, we search for webmap portal items using a text search.

How to use the sample

Enter search terms into the search bar. Once the search is complete, a list is populated with the resultant webmaps. Click on a webmap to set it to the map view. Click on the "Find more results" button to add more results to the list.

How it works

  1. Create a new Portal and load it.
  2. Create new PortalQueryParameters. Set the type to PortalItem.Type.WEBMAP and add the text you want to search for.
  3. Use portal.findItemsAsync(params) to get the first set of matching items (10 by default).
  4. Get more results with portal.findItemsAsync(portalQueryResultSet.getNextQueryParameters()).

Relevant API

  • Portal
  • PortalItem
  • PortalQueryParameters
  • PortalQueryResultSet

Tags

keyword, query, search, webmap

Sample Code

WebmapKeywordSearchController.javaWebmapKeywordSearchSample.javamain.fxml
                                                                                                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright 2017 Esri.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.esri.samples.webmap_keyword_search;

import java.util.List;

import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.portal.Portal;
import com.esri.arcgisruntime.portal.PortalItem;
import com.esri.arcgisruntime.portal.PortalQueryParameters;
import com.esri.arcgisruntime.portal.PortalQueryResultSet;

public class WebmapKeywordSearchController {

  @FXML private TextField keyword;
  @FXML private MapView mapView;
  @FXML private ListView<PortalItem> resultsList;
  @FXML private Button moreButton;

  private Portal portal;
  private PortalQueryResultSet<PortalItem> portalQueryResultSet;

  @FXML
  private void initialize() {
    // load a portal for arcgis.com
    portal = new Portal("https://arcgis.com");
    portal.loadAsync();

    resultsList.setCellFactory(c -> new PortalItemCell());

    // show the selected webmap in a map view
    resultsList.getSelectionModel().selectedItemProperty().addListener(o -> {
      PortalItem webmap = resultsList.getSelectionModel().getSelectedItem();
      if (webmap != null) {
        webmap.loadAsync();
        mapView.setMap(new ArcGISMap(webmap));
        // check if webmap supported
        mapView.getMap().addDoneLoadingListener(() -> {
          if (mapView.getMap().getLoadError() != null) {
            showMessage("Unable to load map", mapView.getMap().getLoadError().getMessage(), Alert.AlertType.ERROR);
          }
        });
      }
    });
  }

  /**
   * Searches a portal for webmaps matching query string in the keyword text field. The list view is updated with
   * the results.
   */
  @FXML
  private void search() {

    // create query parameters specifying the type WEBMAP
    PortalQueryParameters params = new PortalQueryParameters();
    params.setQuery(PortalItem.Type.WEBMAP, null, keyword.getText());

    // find matching portal items
    ListenableFuture<PortalQueryResultSet<PortalItem>> results = portal.findItemsAsync(params);
    results.addDoneListener(() -> {
      try {
        // update the results list view with matching items
        portalQueryResultSet = results.get();
        List<PortalItem> portalItems = portalQueryResultSet.getResults();
        resultsList.getItems().clear();
        resultsList.getItems().addAll(portalItems);
        moreButton.setDisable(false);
      } catch (Exception e) {
        e.printStackTrace();
      }
    });
  }

  /**
   * Adds the next set of results to the list view.
   */
  @FXML
  private void getMoreResults() {
    if (portalQueryResultSet.getNextQueryParameters() != null) {
      // find matching portal items
      ListenableFuture<PortalQueryResultSet<PortalItem>> results = portal.findItemsAsync(portalQueryResultSet.getNextQueryParameters());
      results.addDoneListener(() -> {
        try {
          // replace the result set with the current set of results
          portalQueryResultSet = results.get();
          List<PortalItem> portalItems = portalQueryResultSet.getResults();

          // add set of results to list view
          resultsList.getItems().addAll(portalItems);
        } catch (Exception e) {
          e.printStackTrace();
        }
      });
    } else {
      showMessage("End of results", "There are no more results matching this query", Alert.AlertType.INFORMATION);
      moreButton.setDisable(true);
    }
  }

  /**
   * Shows a Layer title in a ListView.
   */
  private class PortalItemCell extends ListCell<PortalItem> {

    @Override
    protected void updateItem(PortalItem portalItem, boolean empty) {
      super.updateItem(portalItem, empty);
      setText(empty ? null : portalItem.getTitle());
      setGraphic(null);
    }
  }

  /**
   * Display an alert to the user with the specified information.
   * @param title alert title
   * @param description alert content description
   * @param type alert type
   */
  private void showMessage(String title, String description, Alert.AlertType type) {

    Alert alert = new Alert(type);
    alert.setTitle(title);
    alert.setContentText(description);
    alert.show();
  }

  /**
   * Stops and releases all resources used in application.
   */
  void terminate() {

    if (mapView != null) {
      mapView.dispose();
    }
  }

}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.