Source: site.view [edit]
Function name: parseAddress
Arguments: addrStr
Description: From a freeform address string, returns lat/long, street, city, zip using a free service from Yahoo
Page type: webl
Render function:  
Module: global

Page source:

var res = [];

addrStr = Url_Decode(addrStr);

var TextVal = fun(P, fieldname, elt)
   var E = Elem(P, fieldname) inside elt; 
   return (Str_Trim(Text(E[0])) ? "")
end;
           
        

/*           Yahoo API stopped working... 1/15
var P = GetURL("http://where.yahooapis.com/geocode?location=" + Url_Encode(addrStr),
  [. appid="wubhuboaa".]);
*/
           
// Moving to Bing Maps API instead, not as good but works           
var P = GetURL("http://dev.virtualearth.net/REST/v1/Locations", [. 
				"query"=addrStr,
                "includeNeighborhood"="1",
           		"maxResults"="10",
           		"key"="AopGSduvmKdpkUH3LpvYcEUecioYJfxRC0UrhslSWF9k6QDuvGhrzLBEmYcSkYsM"
           .]);
           
Pretty(P);
                      
var Result = Elem(P, "resources");
if Size(Result) > 0 then
  var cinfo = WubCall("countryInfo", [TextVal(P, "countryRegion", Result[0])]);
  var res = [.
     latitude=TextVal(P, "latitude", Result[0]),
     longitude=TextVal(P, "longitude", Result[0]),
     radius=TextVal(P, "radius", Result[0]),
     address=TextVal(P, "addressLine", Result[0]),
     house=TextVal(P, "house", Result[0]),
     street=TextVal(P, "street", Result[0]),
     unit=TextVal(P, "unit", Result[0]),
     postal=TextVal(P, "postalCode", Result[0]),
     city=TextVal(P, "locality", Result[0]),
     county=TextVal(P, "adminDistrict2", Result[0]),
     neighborhood=TextVal(P, "neighborhood", Result[0]),
     statecode=TextVal(P, "adminDistrict", Result[0]),
     state=TextVal(P, "adminDistrict", Result[0]),
     zip=TextVal(P, "postalCode", Result[0]),
     country=TextVal(P, "countryRegion", Result[0]),
     countrycode= (cinfo.code2 ? ""),
     markup=Markup(P)
  .];
  var Point = Elem(P, "point") inside Result[0];
  if Size(Point) > 0 then
     var Coords = Elem(P, "coordinates") inside Point[0];
     if Size(Coords) > 1 then
        res.latitude := ToReal(Str_Trim(Text(Coords[0])));
        res.longitude := ToReal(Str_Trim(Text(Coords[1])));
     end;
  end;
  var BBox = Elem(P, "bbox") inside Result[0];
  if Size(BBox) > 3 then
     var radius = ToReal(Str_Trim(Text(BBox[2]))) - ToReal(Str_Trim(Text(BBox[0])));
     var r2 = ToReal(Str_Trim(Text(BBox[3]))) - ToReal(Str_Trim(Text(BBox[1])));
     if (r2 > radius) then
        radius = r2
     end;
     res.radius := ToInt(radius * 50.0)
  end;
  res;           
else
  Markup(P)
end;