Select to view content in your preferred language

Adding a Marker to the Map after Geocoding

3025
14
10-30-2013 12:35 AM
NumaGremling
Deactivated User
Hi everyone,

I�??ve been struggling with this for hours so I need to ask here. I am trying to search an address in my geocoder and then, after zooming and centering, also add a marker to that place. This seemingly simple task seems to be pretty complicated for a beginner.
It seems like all samples that are doing what I want in AMD are all using the Locator, but not the Geolocator. So, I ended up translating this code, but I get stuck.

First, here is the legacy code, where I took out everything that I do not need (I do not need a pop up, etc.)

        // add a graphics layer for geocoding results
        map.addLayer(new esri.layers.GraphicsLayer({
          id: "results"
        }));

        // create the geocoder
        geocoder = new esri.dijit.Geocoder({ 
          autoNavigate: false, // do not zoom to best result
          maxLocations: 20, // increase number of results returned
          map: map,
          arcgisGeocoder: {
            url: "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer",
            name: "Esri World Geocoder",
            placeholder: "Find a place",
            sourceCountry: "USA" // limit search to the United States
          }
        }, "search");
        geocoder.startup();

        var symbol = new esri.symbol.PictureMarkerSymbol({
          "angle":0,
          "xoffset":0,
          "yoffset":10,
          "type":"esriPMS",
          "url":"http://static.arcgis.com/images/Symbols/Shapes/BluePin1LargeB.png",
          "contentType":"image/png",
          "width":24,
          "height":24
        });


        dojo.connect(geocoder, "onFindResults", function(response) {
          var l = map.getLayer("results");
          l.clear();
          map.infoWindow.hide();
          dojo.forEach(response.results, function(r) {
            r.feature.setSymbol(symbol);
            l.add(r.feature);
          });
        });



And here is my AMD translation:

  // add graphics layer for result
  mainMap.addLayer(new GraphicsLayer({
          id: "results"
        }));  
  
  // create geocoder
  var geocoder = new Geocoder({
   map: mainMap,
   autoComplete: true,
   arcgisGeocoder: {
    suffix: " Long Beach, CA",
    name: "Esri World Geocoder",
    placeholder: "find address within Long Beach"
    },
  }, "addressFindDIV");  // name of the div it is referring to
  geocoder.startup();
  

  // create new PictureMarkerSymbol object using a JSON object
  var symbol = new PictureMarkerSymbol({
          "angle":0,
          "xoffset":0,
          "yoffset":10,
          "type":"esriPMS",
          "url":"http://static.arcgis.com/images/Symbols/Shapes/BluePin1LargeB.png",
          "contentType":"image/png",
          "width":24,
          "height":24
        });
    
  geocoder.on("find-results", function(response){
          var l = mainMap.getLayer("results");
          l.clear();
          mainMap.infoWindow.hide();
          array.forEach(response.results, function(r) {
            r.feature.setSymbol(symbol);
            l.add(r.feature);
  
           });
        });



I am absolutely not sure about the following line:

dojo.forEach(response.results, function(r) {

         

I changed it to

array.forEach(response.results, function(r) {


But that does not work either and I am not sure if it is the right approach.

I am not tied to this sample; if any other approach is recommended or easier, please share it with me. As long as I can see a symbol (a simple point is fine) I would be happy.

Thank you!
0 Kudos
14 Replies
JamesVillanueva
Regular Contributor
I haven't used the suffix parameter in my work with the geocoder. It appears that all it does is append it to the end of the address. That being said, you shouldn't enter an address with the city name if you are using the suffix. The second thing I see is that the 77 market is throwing off the geocoder as well. When I just put in the address as 2127 E 17th St it appears to go to the correct location. Please verify you are seeing the same.
0 Kudos
NumaGremling
Deactivated User
You are right; I copied the whole address although I only entered the beginning part.

I now simply typed in 2127 E 17th St without using the dropdown to help me and it went to the right location and it added a marker (two, actually, huh?). Now, I just realized that anytime I type something in but do not use the results dropdown it will take me to a location and add marker. If I click on one of the suggestions in the results dropdown it will, in most cases, not add a marker (or add it in that one spot in the previous screenshot). Very, very strange.
0 Kudos
JamesVillanueva
Regular Contributor
In my experience with the geocoder, it was built more as Point of Interest (POI) locator. It appear to be build to find things like coffee shops where you'd expect more than one result. In my project, I just limit results to the first one that comes back. With that being said, I usually get correct results back as the first result, but not always. For instance, I was looking for the Statue of Liberty and instead the first result was the ferry terminal to get you to the Statue of Liberty. I'm glad you've got it up and running.
0 Kudos
NumaGremling
Deactivated User
I have been testing many different addresses now, and I come to the conclusion that it behaves very strangely.

Try this:
Type in: 200 belmont avenue and hit Enter. It will take you to a place and add multiple markers somewhere on that street. Now type in the same thing, but instead of hitting Enter you will choose the first suggested address (the same address, actually) and see what happens. It will zoom out and not add markers anywhere (the ones you see are from your previous successful search). Does it attach the suffix to the suggested address and get confused?

I hope I am making sense; it is very hard to describe a strangely behaving geocoder. This discussion might also have moved from adding a marker to understanding how the geocoder reacts to inputs; maybe I should start a new topic. Either way, thank you so much for your help!

Oops, and while I was writing this your replied already! So would you say that using the geocoder to find an address might not be the best approach? First of all it is hard to see what the differences between the Geocoder and the Locator are when looking at the reference. They both have the same purpose?  But the Geocoder offers more, especially styling etc.? So I'll pick that one... That would be my typical reasoning; not sure anymore how one is supposed to read the reference pages and make sense out of certain things.
0 Kudos
JamesVillanueva
Regular Contributor
I agree, the documentation describing one vs the other should be better. I think if your just looking for an address, locator makes more sense. I also understand it not as appealing choice.

I would start a new topic about understanding geocoder inputs. It was also be interesting to know why you can specify a suffix on the geocoder, but it fails to ignore it if it's in the input string.
0 Kudos