0

AS3 代码,写得不好,我需要从 Google Maps Api 检索 XML 数据并从中检索节点/字符串。不会发生。我需要使用 XML 数据作为变量值,然后获取它的子节点和该子节点中的节点,然后将它们放入一个数组中(有点像我的推文 XML 数据)。这是我的输出:

RT @StonesGotStyle: Feeling terrible, darn you flu! This may be the only way to make me feel better! http://t.co/RJLEog6c8c So awesome! @OM…
Majulah Singapura
have the worst heyfever, and coming down with a flu whilst having guests round and I need to revise is the worst scenario **
London
2
TypeError: Error #1034: Type Coercion failed: cannot convert "<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
 <status>OK</status>
 <result>
  <type>country</type>
  <type>political</type>
  <formatted_address>United Kingdom</formatted_address>
  <address_component>
   <long_name>United Kingdom</long_name>
   <short_name>GB</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>55.3780510</lat>
    <lng>-3.4359730</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>49.8669688</lat>
     <lng>-8.6493572</lng>
    </southwest>
    <northeast>
     <lat>60.8565530</lat>
     <lng>1.7627096</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>34.5640831</lat>
     <lng>-8.6493572</lng>
    </southwest>
    <northeast>
     <lat>60.9113448</lat>
     <lng>33.9165549</lng>
    </northeast>
   </bounds>
  </geometry>
 </result>
</GeocodeResponse>
" to XML.
    at app::main/locate()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

我的代码:

package app
{
    import flash.xml.XMLDocument;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.display.MovieClip;

    public class main extends MovieClip
    {
        public var geoReq:URLRequest = new URLRequest;
        public var geoLoader:URLLoader = new URLLoader();
        public var geoXml:XML = new XML();
        public var geoLocList:XMLList = new XMLList();
        public var geoLoc:Array = new Array();
        public function main():void
        {
            geoLoader.addEventListener(Event.COMPLETE, locate);
            var urlReq:URLRequest = new URLRequest("http://search.twitter.com/search.atom?q=flu&rpp=3&lang=en&geocode=55.378051,-3.435973,605mi");
            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, getTweets);
            loader.load(urlReq);
        }
        function getTweets( e:Event ):void
        {
            loadGeo("UK");
            if ( e.target.data )
            {
                var tweets = new Array(); var times = new Array();
                var twitterXML:XML = new XML(e.target.data);
                var tweetList:XMLList = twitterXML.children();
                var tweetItem:String;
                var placeItem:String;
                var tweet:Array = new Array();
                for (var i:int = 0; i < tweetList.length(); i++)
                {
                    tweetItem = tweetList[i].*::title;
                    placeItem = tweetList[i].*::location;
                    if( tweetItem && placeItem != "")
                    {
                    tweet = [tweetItem, placeItem];
                    tweets.push(tweet);
                    trace(tweets[tweets.length - 1][0]);
                    trace(tweets[tweets.length - 1][1]);
                    }
                }
                trace(tweets.length);
            }
        }
        function loadGeo(loc:String):void
        {
            geoReq = new URLRequest("https://maps.googleapis.com/maps/api/geocode/xml?address=" + loc + "&sensor=false");
            geoLoader.load(geoReq);
        }
        function locate(e:Event):void
        {
            geoXml = e.target.data;
            //geoLocList = geoXml.children();
            //geoLoc = [geoLocList[0].*::lat, geoLocList[0].*::lng];
            trace("located");
            trace(e.target.data);
            //trace(geoXml);
            trace("located");
        }
    }
}

请帮助,提前谢谢,凯尔。

4

1 回答 1

0

您得到的结果是String,因此您需要基于它创建一个新的 XML 对象String

在您的locate()方法中更改此行:

geoXml = e.target.data;

至:

geoXml = new XML(e.target.data);
于 2013-06-01T03:13:00.480 回答