Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jaspervalero/1915546 to your computer and use it in GitHub Desktop.
Save jaspervalero/1915546 to your computer and use it in GitHub Desktop.
AS3 | This is a zipcode to Where On Earth ID (WOEID) converter class which allows you to query weather and map results by zipcode. Soon the Yahoo API will no longer support direct requests by zipcode, and will only accept requests by WOEID. This class will ensu
package com.jaspervalero.tools
{
/*######################################
# Yahoo Zipcode to WOEID Converter #
# Written By: Jasper Valero #
# www.jaspervalero.com #
# July 13th, 2011 #
######################################*/
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
/**
* Class to convert zipcodes into Yahoo API WOEIDs using Yahoo Query Language (YQL).
* @author jaspervalero
* @example Here is an example of this class in use:
*
* <listing version="3.0">
*
* private function convertZip():void
* {
* _zc = new ZipcodeConverter("95348");
* _zc.addEventListener(Event.COMPLETE, conversionComplete);
* }
*
* private function conversionComplete(e:Event):void
* {
* _woeid = _zc.woeid;
* }
* </listing>
*
*/
public class ZipcodeConverter
{
// Set property
private var _woeid:String;
/**
* Constructor function which passes in a zipcode to be converted.
* @param zipcode The 5-digit zipcode you want converted to a WOEID.
*/
public function ZipcodeConverter(zipcode:String)
{
super();
query(zipcode);
}
// Query zipcode from Yahoo to find associated WOEID
private function query(zipcode:String):void
{
/* Setup YQL query statement using dynamic zipcode. The statement searches geo.places
for the zipcode and returns XML which includes the WOEID. For more info about YQL go
to: http://developer.yahoo.com/yql/ */
var qry:String = "SELECT woeid FROM geo.places WHERE text=" + zipcode + " LIMIT 1";
// Generate request URI using the query statement
var ul:URLLoader = new URLLoader();
ul.load(new URLRequest("http://query.yahooapis.com/v1/public/yql?q=" + qry));
ul.addEventListener(Event.COMPLETE, onParse);
}
// Extract WOEID after XML loads
private function onParse(e:Event):void
{
var xmlData:XML = XML(e.target.data);
_woeid = xmlData..*::woeid;
/* Dispatch event complete to allow woeid to be passed outside of class only after
XML has loaded. Required due to asynchronous nature of XML loading. */
dispatchEvent(new Event(Event.COMPLETE));
}
/**
* This is the WOEID used by the Yahoo API.
* @return Returns the WOEID for the queried zipcode.
*/
public function get woeid():String
{
return _woeid;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment