3

its an output for this script and it shows four differnt points for one address instead of one pointI have a script which geocode an address and buffer that address by 1, 5 and 10 mile radius. The problem I am having is that the address on the map shows 4 different points not sure what's causing it. Please let me know how to fix this issue.

Find Address

<style>
  html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
  h3 { margin: 0 0 5px 0; border-bottom: 1px solid #444; }
  .shadow {
    -moz-box-shadow: 0 0 5px #888;
    -webkit-box-shadow: 0 0 5px #888;
    box-shadow: 0 0 5px #888;
  }
  #map{ margin: 0; padding: 0; }
  #feedback {
    background: #fff;
    bottom: 30px;
    color: #444;
    position: absolute;
    font-family: arial;
    height: 80px;
    left: 30px;
    margin: 5px;
    padding: 10px;
    width: 300px;
    z-index: 40;
  }
</style>
<script>var dojoConfig = { parseOnLoad: true };</script>
<script src="http://serverapi.arcgisonline.com/jsapi/arcgis/3.2/"></script>
<script>
  dojo.require("dijit.layout.BorderContainer");
  dojo.require("dijit.layout.ContentPane");
  dojo.require("dijit.form.Button");
  dojo.require("esri.arcgis.utils");
  dojo.require("esri.map");
  dojo.require("esri.tasks.locator");

  var map, locator;
  function init() {
    // why write a lot of javascript code when you can use arcgis.com to author your webmap?
    var webmapId = "0c96aea5f94b4ea3b73595daxxxxxxx";

    //create map
    var mapDeferred = esri.arcgis.utils.createMap(webmapId, "map", {
      mapOptions: { slider: false }
    });

    locator = new esri.tasks.Locator("http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer");

    dojo.connect(dijit.byId("locate"), "onClick", function() {
      map.graphics.clear();
      var address = dojo.byId("address").value;
      console.log("address: ", address);
      if ( ! address ) {
        alert("Please enter an address to geocode.");
        return;
      }
      locator.outSpatialReference = map.spatialReference;
      var geocode = locator.addressToLocations({
        address: { "SingleLine": address },
        outFields: ["*"]
      });

      geocode.then(function(results) {
        console.log("got results: ", results);
        // add the results to the map
        dojo.forEach(results, function(r) {
          map.graphics.add(new esri.Graphic(
            r.location, new esri.symbol.SimpleMarkerSymbol(), r.attributes
          ));
        });
        console.log("added graphics...now buffer the first one");
        bufferLocation(results[0]);
      }, function(error) {
        console.log("geocode failed: ", error);
      });
    });

    mapDeferred.addCallback(function (response) {
      map = response.map;
      dojo.connect(dijit.byId("map"), "resize", map, map.resize);
    });
  }

  function bufferLocation(place) {
    var bufferParams = new esri.tasks.BufferParameters();
    bufferParams.geometries = [ place.location ];
    bufferParams.distances = [ 1, 5, 10 ]; // miles
    bufferParams.unit = esri.tasks.GeometryService.UNIT_STATUTE_MILE;
bufferParams.outSpatialReference = map.spatialReference;

    var gs = new esri.tasks.GeometryService("http://networkgis.corp.sprint.com/networkags/rest/services/Utilities/Geometry/GeometryServer");
    var buffer = gs.buffer(bufferParams);
    buffer.then(function(buffers) {
      console.log("got buffers: ", buffers);
      dojo.forEach(buffers, function(b) {
        map.graphics.add(new esri.Graphic(
          b, new esri.symbol.SimpleFillSymbol()
        ));
      });
    }, function(error) {
      console.log("error doing buffer...", error);
    });
  }
  dojo.ready(init);
</script>

    <div id="feedback" class="shadow">
      <h3>Locate and buffer an address</h3>
      <div id="info">
        <input type="text" id="address" value="6360 Sprint Parkway Overland Park KS">
        <button dojotype="dijit.form.Button" id="locate">Locate</button> 
      </div>
    </div>
  </div>
</div>

1

Well, the issue of you getting 4 results back from a geocode operation is actually not necessarily a bug, that is how geocoding is designed to work. It looks like you're using the standard ESRI world geocoder. This means, if you type an address (ex: 123 Main St, City, ST, ZIP), you are likely getting at least one, if not more, results for the address itself, and then you may be getting a result for the city and/or the zip. For example, if you give it 123 Main St, but there is actually an E Main St and W Main St in that city, the results will likely return the points for both 123 E Main St and 123 W Main St, as well as the geographic center of the provided zip code. I do not know the ESRI world geocoding service specifically, but that is generally how most geocoders work.

It looks like the issue is in the following code:

    dojo.forEach(results, function(r) {
      map.graphics.add(new esri.Graphic(
        r.location, new esri.symbol.SimpleMarkerSymbol(), r.attributes
      ));
    });
    console.log("added graphics...now buffer the first one");
    bufferLocation(results[0]);

When you get the result from the geocode service, it is giving you multiple possible locations of that address and you are putting a point on your map for each of them, but then just creating the buffer based on the first result. You probably need to read up in the JavaScript API about geocode results and add some logic to your code to decide which single point you are going to use and then just put that point on the map and buffer it. The geocode service will likely provide the more accurate location first, followed by less likely results (ex: it likely searches address points, then road address ranges, then zip code and probably returns the results in that order). Similarly, there should be a geocoding score returned for each result which should have, basically, a percent confidence, which you may also want to consider. Generally though, with most geocoders, the first result in a result set for any given address is what it feels the most accurate result is based on the provided address, so you should be able to just use results[0] instead of looping through each result.

I hope that helps, but please feel free to comment if I need to clarify what I mean.

0

john is definitely right, but you can also utilize the maxLocations parameter of the Esri World Geocoding Service if you'd like to get control over how many results the service returns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.