Searching for an address is a process known as 'geocoding'. GeoStream provides advanced geocoding functionality, wrapped up in the one-line geoCode() function.

The first argument passed to the geoCode() function is the address you'd like to search for, in 'natural' form - just as you'd write it on an envelope. The second argument of the geoCode() function is the name of another JavaScript function. This second function is responsible for processing the search results.

var address = '85 Enterprise, Aliso Viejo, CA';
GeoBase.GeoCoder.geoCode(address, 'USA', process_geocode_results);

This second function is passed an array of search results, ranked by the GeoStream server in decreasing order of precision. It’s often safe to take the first element from this array, although you may wish to present the results to the user in a combo box and allow them to select the correct result.

function process_geocode_results(resultsArray)
    if (resultsArray.length > 0)
        var loc = resultsArray[0].getLocation();
        // Pan and zoom to location:
        // map.setCenter(loc);
        // map.setZoomIndex(17);
    else alert ('No matching addresses were found.');


Published, Jun 28th 2010, 15:36

Tagged under: javascript forward geocoding