Select to view content in your preferred language

javascript geocoder returning incorrect coordinates

1524
5
Jump to solution
11-21-2014 09:13 PM
JustinShepard
Deactivated User

I was testing the geocoder from the ArcGIS JavaScript API today and noticed some odd behavior. Simply it boils down to if I run the geocoder manually (ie. type the search criteria into the text box and then press enter) then it returns as expected with the result spatial reference and extent matching. However, if I run the geocoder by code (ie. dynamically build the search criteria from other user inputs and pass in the search criteria to the geocoder) then it returns the spatial reference in Web Mercator and the extent coordinates in WGS84. The problem with this is that when I zoom to the result from a find result coming from a code approach the map zooms to the wrong location.

Do I need to do something different to the geocoder when passing in search criteria by code?

after creating a geocoder I set up a handler for the "find-results"

on(geocoder, "find-results", function (geocodeResults) {

                    console.log(geocoder);

                    console.log("geocode results");

                    console.log(geocodeResults);

                    console.log("geocode results - first extent");

                    geocoder.select(geocodeResults.results.results[0]);

                    console.log("map");

                    console.log(map);

                });

for the find by code approach here's how it is handled

geocoder.value = 'California, United States';

geocoder.find();

using the same search criteria here are the results from the code and manual approaches:

Results from code

extent: Object

     spatialReference: Object

          latestWkid: 3857

          wkid: 102100

     __proto__: Object

     type: "extent"

     xmax: -114.131258

     xmin: -125.371258

     ymax: 42.87022

     ymin: 31.63022

Results from manual

extent: Object

     spatialReference: Object

          latestWkid: 3857

          wkid: 102100

     __proto__: Object

     type: "extent"

     xmax: -12705033.52415573

     xmin: -13956264.600672128

     ymax: 5292238.823753822

     ymin: 3714868.5338891055

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Notable Contributor

Justin,

The issue is that the map probably isn't loaded when you set the value for the geocoder so it doesn't know the map SR.  Waiting for the geocoder to load should resolve the issue because the geocoder handles checking for hte map load. Here's a bit of code that shows this workflow.

Also note that instead of setting the value using geocoder.value we use set instead.


        geocoder = new Geocoder({ 
          map: map 
        }, "search");
        geocoder.startup();
        on(geocoder, "load", function(){
          geocoder.set("value","California, United States");
          geocoder.find();
          on(geocoder, "find-results", function(result){
            console.log(result)
          });
        });

View solution in original post

0 Kudos
5 Replies
RahulMetangale2
Emerging Contributor

Justin,

Have you tried setting outSpatialreference property on geocoder to map.spatialreference?

Sent from my Windows Phone

0 Kudos
JustinShepard
Deactivated User

The API doesn't have anywhere to set the output spatial reference for the geocoder's find() method, geocoder-amd | API Reference | ArcGIS API for JavaScript. If you know where I could set the output spatial reference for the geocoder that would be great.

0 Kudos
KellyHutchins
Esri Notable Contributor

Justin,

The issue is that the map probably isn't loaded when you set the value for the geocoder so it doesn't know the map SR.  Waiting for the geocoder to load should resolve the issue because the geocoder handles checking for hte map load. Here's a bit of code that shows this workflow.

Also note that instead of setting the value using geocoder.value we use set instead.


        geocoder = new Geocoder({ 
          map: map 
        }, "search");
        geocoder.startup();
        on(geocoder, "load", function(){
          geocoder.set("value","California, United States");
          geocoder.find();
          on(geocoder, "find-results", function(result){
            console.log(result)
          });
        });
0 Kudos
JustinShepard
Deactivated User

Thanks. I finally stumbled upon that today. I was still surprised that the spatial reference and extent values didn't align. I was expecting it to throw an error instead of returning unmatched values.

What is the advantage to using geocoder.set instead of geocoder.value?

0 Kudos
KellyHutchins
Esri Notable Contributor

Justin,

Using set instead of directly setting the value is a cleaner more consistent form of interacting with the widget's properties.  There's a bit more info on using set/get in the following tutorial from dojo. Look at the note section in the 'Getters and Setters' portion of the doc.

Understanding _WidgetBase - The Dojo Toolkit

0 Kudos