cmv / cmv-app

CMV - The Configurable Map Viewer - A community supported open source mapping framework built with the Esri JavaScript API and the Dojo Toolkit
https://demo.cmv.io/
MIT License
325 stars 278 forks source link

how to add search features to Geocoder #656

Closed JudyZhou closed 7 years ago

JudyZhou commented 7 years ago

I need to add search features to Geocoder. I have done this in ESRI Search widget in web AppBuilder before. this time I added code into Geocoder.js like this, but search result return not found. Or is available a search widget including data search and geocoder? Please help!:

 sources: [{
  locator: new Locator("http://cohgims.houstontx.gov/arcgis/rest/services/AddressLocators/Locator_composite/GeocodeServer"),
  name: "HOU PWE Locator",
  singleLineFieldName: "SingleLine",
  placeholder: "Enter Address or Intersection",
  countryCode: "",
  maxResults: 30,
  searchInCurrentMapExtent: false,
  highlightSymbol: new PictureMarkerSymbol("https://js.arcgis.com/3.18/esri/dijit/Search/images/search-pointer.png", 36, 36).setOffset(9, 18)
}, {
  "layerId": "RBH_wab_76_0",
  "url": "http://pwegis.houstontx.gov/arcgis/rest/services/PROD/RBH1/MapServer/4",
  "name": "CIP Projects",
  "placeholder": "Enter Project No. or Description",
  "searchFields": ["CIP_NO", "PRO_SCOPE"],
  "displayField": "CIP_NO",
  "exactMatch": false,
  "maxResults": 300,
  "type": "query"
}];
tmcgee commented 7 years ago

@JudyZhou You can use the Esri Search widget with CMV just as you have done with the WAB. The Esri Geocoder widget has been deprecated by Esri so the Search widget is the preferred option. We have switched to the search widget in CMV.

JudyZhou commented 7 years ago

@tmcgee thank you for help! does CMV has search widget already? where can I download it?

tmcgee commented 7 years ago

@JudyZhou I provided a link in my previous response to the line in the demo viewer.js where the Esri search widget is included. It is not a CMV widget. No downloading required.

green3g commented 7 years ago

I think there is actually a bug with cmv and loading feature layer searches. Adding feature layer searches doesn't work in cmv due to the widget loader using lang.clone on the widget options.

This documentation uses the FeatureLayer constructor in the source options. But the clone function cannot clone a FeatureLayer object, for example the following throws an error:

lang.clone(new FeatureLayer('url'))

This can be reproduced in cmv using the config:


        search: {
            include: true,
            type: 'domNode',
            path: 'esri/dijit/Search',
            srcNodeRef: 'geocoderButton',
            options: {
                map: true,
                mapRightClickMenu: true,
                enableInfoWindow: true,
                enableButtonMode: true,
                expanded: false,
                sources: [{
                    featureLayer: new FeatureLayer('/arcgis/rest/services/internal/assets/MapServer/12', {
                        outFields: ['cid', 'Shape', 'OBJECTID']
                    }),
                    searchFields: ['cid'],
                    suggestionTemplate: 'Structure Number: ${cid}',
                    exactMatch: false,
                    name: 'Storm Structure ID',
                    placeholder: 'Structure ID',
                    maxResults: 6,
                    maxSuggestions: 6,
                    enableSuggestions: true,
                    minCharacters: 0,
                    localSearchOptions: {distance: 5000}
                }]
            }
        },

@tmcgee is that lang.clone needed on the widget loader?

tmcgee commented 7 years ago

@roemhildtg I don't have a problem including feature layers in the Esri Search widget and have used it often for clients. I will compare the example you provided above with one of my working versions and see if I can identify the difference.

tmcgee commented 7 years ago

@roemhildtg I think the difference in our approaches may be that I do not include the search widget's options within viewer.js but within a separate file search.js. As a by-product my approach, the feature layer(s) in the widget's options do not get cloned.

As to why lang.clone is used with the widget config, that line pre-dates my involvement with the CMV project. I think we can just remove it because it is problematic for this Esri widget and potentially others - specifically those widgets that include Esri modules within their config options. As we've seen elsewhere, some of the Esri modules don't handle cloning well.

@DavidSpriggs do you recall the original need for lang.clone in this line and any objections to not cloning the config?

green3g commented 7 years ago

I see, that would make sense since if you pass a string as the widget options, lang.clone won't be used on the actual options object.

tmcgee commented 7 years ago

Correct

green3g commented 7 years ago

I don't think the lang.clone is necessary. I've taken it out on my end and things appear to be in working order. We should add this enhancement to the widget mixin to allow feature layers in the options properties.

green3g commented 7 years ago

closed in https://github.com/cmv/cmv-app/pull/736

gisnovice commented 7 years ago

Hello, can I add my own GeocodeServer to the search widget? I am trying for the search widget to also recognize APNs, or would it only be suited for the Find widget?

            search: {
                include: true,
                type: has('phone') ? 'titlePane' : 'ui',
                path: 'esri/dijit/Search',
                placeAt: has('phone') ? null : 'top-center',
                title: i18n.viewer.widgets.search,
                iconClass: 'fa-search',
                position: 0,
                options: {
                    map: true,
                    visible: true,
                    enableInfoWindow: true,
                    enableButtonMode: has('phone') ? false : true,
                    expanded: has('phone') ? true : false,
                    expanded: true,
                    defaultSource: 'http://gis.ci.oceanside.ca.us/gis/rest/services/AddressLocator_Composite2016/GeocodeServer'
                }
            },