Not able to perform Query on FeatureLayer and GeoCode using ESRI's geocode service.

577
1
09-02-2018 03:30 AM
RavindraSingh
Occasional Contributor

I am using 

  • ArcGIS-Runtime-SDK-iOS', '100.2.1'
  • XCode 9.4.1
  • iOS 11.4 Simulator

I am trying to perform GeoCode 

using - https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer

using below Code - (code is copied from ESRI-GitHub Sample Codes )

func testGeoCode(){

        // Try to query MapService

        let locatorURL = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer"

        let locatorTask:AGSLocatorTask = AGSLocatorTask(url: URL(string: locatorURL)!)

        let geocodeParameters:AGSGeocodeParameters = AGSGeocodeParameters()

        geocodeParameters.resultAttributeNames.append(contentsOf: ["*"])

        geocodeParameters.minScore = 75

        

        //perform geocode with text

        locatorTask.geocode(withSearchText: "California", parameters: geocodeParameters, completion: { (results:[AGSGeocodeResult]?, error:Error?) -> Void in

            if let error = error {

                print("testGeoCode Error::: \(error)")

            }

            else {

                if let results = results , results.count > 0 {

                    print("GeoCode Result found::: \(results.count)")

                    //create a graphic for the first result and add to the graphics overlay

                    _ = AGSGraphic(geometry: results[0].displayLocation!, symbol: nil, attributes: results[0].attributes as [String : AnyObject]?)

                    //                    let graphic = self?.graphicForPoint(results[0].displayLocation!, attributes: results[0].attributes as [String : AnyObject]?)

                    //                    self?.graphicsOverlay.graphics.add(graphic!)

                }

                else {

                    //provide feedback in case of failure

                    print("No GeoCode Result found.......)")

                }

            }

        })

    }

but this above code never gives result. and ends up with below error:

Some Times This::

testGeoCode Error::: Error Domain=NSCocoaErrorDomain Code=3072 "User canceled error" UserInfo={NSLocalizedDescription=User canceled error}

2018-09-02 13:12:36.204621+0300 ERS[8764:217712] Task <9EC69EBF-C7BE-45AF-8564-F6B607856D45>.<185> finished with error - code: -999

2018-09-02 13:12:36.204795+0300 ERS[8764:218992] Task <A6812BF2-37CC-4470-B0D0-5C53E5BD7FFD>.<184> HTTP load failed (error code: -999 [1:89])

2018-09-02 13:12:36.205268+0300 ERS[8764:217712] Task <A6812BF2-37CC-4470-B0D0-5C53E5BD7FFD>.<184> finished with error - code: -999

Some Times:: 

2018-09-02 13:20:07.760707+0300 ERS[9134:225343] Task <3C0CE309-4E55-4A3F-B62F-AD54EB132FA6>.<203> HTTP load failed (error code: -999 [1:89])

I am also performing Query:: using below Code:: -  (code is copied from ESRI-GitHub Sample Codes- for Query )

func testQueryOnLayer(){

        let featureTable = AGSServiceFeatureTable(url: URL(string: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2")!)

        let featureLayer = AGSFeatureLayer(featureTable: featureTable)

        featureLayer.selectionWidth = 5

        

        let searchText = "California"

        

        let queryParams = AGSQueryParameters()

        queryParams.whereClause = "upper(STATE_NAME) LIKE '%\(searchText.uppercased())%'"

        

        featureTable.queryFeatures(with: queryParams, completion: { (result:AGSFeatureQueryResult?, error:Error?) -> Void in

            if let error = error {

                print("testQueryOnLayer Error::: \(error)")

            }

            else if let features = result?.featureEnumerator().allObjects {

                if features.count > 0 {

                    print("GeoCode Result found::: \(features.count)")

                    //mapView.setViewpointGeometry(features[0].geometry!, padding: 80, completion: nil)

                }

                else {

                    print("No query Result found.......)")

                }

            }

        })

    }

This above code executes, neither gives error nor gives result.

NOTE:: I have already made changes into my info.plist file. to avoid ATS as below::

<key>NSAppTransportSecurity</key>

<dict>

<key>NSExceptionDomains</key>

<dict>

<key>arcgis.com</key>

<dict>

<key>NSIncludesSubdomains</key>

<true/>

<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>

<true/>

</dict>

<key>arcgisonline.com</key>

<dict>

<key>NSIncludesSubdomains</key>

<true/>

<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>

<true/>

</dict>

<key>esri.com</key>

<dict>

<key>NSIncludesSubdomains</key>

<true/>

<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>

<true/>

</dict>

</dict>

</dict>

Any Leads please.

0 Kudos
1 Reply
NimeshJarecha
Esri Regular Contributor

You are not holding on to the objects on which you are sending requests. Hence, things are not working as expected. Please look at the modified code below. The `locatorTask` and `featureTable` are declared as class level variables and now queries are successfully returns results. 

    var locatorTask: AGSLocatorTask?

    func testGeoCode(){

        // Try to query MapService

        let locatorURL = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer"

        locatorTask = AGSLocatorTask(url: URL(string: locatorURL)!)

        let geocodeParameters:AGSGeocodeParameters = AGSGeocodeParameters()

        geocodeParameters.resultAttributeNames.append(contentsOf: ["*"])

        geocodeParameters.minScore = 75

        

        //perform geocode with text

        locatorTask?.geocode(withSearchText: "California", parameters: geocodeParameters, completion: { (results:[AGSGeocodeResult]?, error:Error?) -> Void in

            if let error = error {

                print("testGeoCode Error::: \(error)")

            }

            else {

                if let results = results , results.count > 0 {

                    print("GeoCode Result found::: \(results.count)")

                    //create a graphic for the first result and add to the graphics overlay

                    _ = AGSGraphic(geometry: results[0].displayLocation!, symbol: nil, attributes: results[0].attributes as [String : AnyObject]?)

                    //                    let graphic = self?.graphicForPoint(results[0].displayLocation!, attributes: results[0].attributes as [String : AnyObject]?)

                    //                    self?.graphicsOverlay.graphics.add(graphic!)

                }

                else {

                    //provide feedback in case of failure

                    print("No GeoCode Result found.......)")

                }

            }

        })

    }

    var featureTable: AGSServiceFeatureTable?

    func testQueryOnLayer(){

        featureTable = AGSServiceFeatureTable(url: URL(string: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2")!)

        let featureLayer = AGSFeatureLayer(featureTable: featureTable!)

        featureLayer.selectionWidth = 5

        

        let searchText = "California"

        

        let queryParams = AGSQueryParameters()

        queryParams.whereClause = "upper(STATE_NAME) LIKE '%\(searchText.uppercased())%'"

        

        featureTable?.queryFeatures(with: queryParams, completion: { (result:AGSFeatureQueryResult?, error:Error?) -> Void in

            if let error = error {

                print("testQueryOnLayer Error::: \(error)")

            }

            else if let features = result?.featureEnumerator().allObjects {

                if features.count > 0 {

                    print("GeoCode Result found::: \(features.count)")

                    //mapView.setViewpointGeometry(features[0].geometry!, padding: 80, completion: nil)

                }

                else {

                    print("No query Result found.......)")

                }

            }

        })

    }

Hope this helps!

Regards,

Nimesh