开发者

Host and load KML File in Flex project (IGN)

I'm look开发者_Python百科ing at a tutorial to display placemarks using a KML file on a flex application. I'm using IGN API (openscales) in flex project.

The example works perfectly (http://openscales.org/userguide/examples/srcview/source/KMLExample.mxml.

<os:KML url="http://www.parisavelo.net/velib.kml"
                proxy="http://openscales.org/proxy.php?url="
                numZoomLevels="20"
                style="{Style.getDefaultCircleStyle()}"/>

But when I'm hosting the same kml file on my server like that :

<os:KML url="http://www.cycom.org/velib.kml"
                proxy="http://openscales.org/proxy.php?url="
                numZoomLevels="20"
                style="{Style.getDefaultCircleStyle()}"/>

Placemarks don't show up on the map. I tried to host the kml file on different hosts but that doesn't change. Do you have a clue ?

Thank you.


You need to add a crossdomain.xml to the server side (in root folder) to allow the application to access your KML file. For example, if you want to allow access to your server to all IPs and domain names just use a simple wild card as below in your crossdomain.xml file:

<?xml version="1.0"?>
<cross-domain-policy>
    <allow-access-from domain="*" />
</cross-domain-policy>

For more information about using crossdomain file see: Transfering Data Accross Domains Using crossdomain.xml

If you don't have access to the server or if this solution doesn't work for you, you have to modify the KML.as class in openscales (found in org.openscales.core.layer package).

Change line 19

private var _request: XMLRequest = null;

to

private var _request :URLLoader = null;

Here is the entire modified KML class:

package org.openscales.core.layer

{
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;

import org.openscales.core.Trace;
import org.openscales.core.feature.Feature;
import org.openscales.core.format.KMLFormat;
import org.openscales.core.request.XMLRequest;
import org.openscales.geometry.basetypes.Bounds;


public class KML extends FeatureLayer
{
    private var _url :String = "";
    private var _request :URLLoader = null;
    private var _kmlFormat :KMLFormat = null;
    private var _xml :XML = null;

    public function KML ( name :String,
        url :String,
        bounds :Bounds = null )
    {
        this._url = url;
        this.maxExtent = bounds;

        super( name );
        this._kmlFormat = new KMLFormat();
    }

    override public function destroy () :void
    {
        if ( this._request )
            this._request = null;

        this.loading = false;
        super.destroy();
    }

    override public function redraw ( fullRedraw :Boolean = true ) :void
    {
        if ( !displayed )
        {
            this.clear();
            return;
        }

        if ( !this._request )
        {
            this.loading = true;
            this._request = new URLLoader();
            this._request.addEventListener( Event.COMPLETE, onSuccess );
            this._request.addEventListener( IOErrorEvent.IO_ERROR, onFailure );
            this._request.load( new URLRequest( url ));
        }
        else
        {
            this.clear();
            this.draw();
        }
    }

    public function onSuccess ( event :Event ) :void
    {
        this.loading = false;
        var loader :URLLoader = event.target as URLLoader;

        // To avoid errors if the server is dead
        try
        {
            this._xml = new XML( loader.data );

            if ( this.map.baseLayer.projection != null && this.projection != null && this.projection.srsCode != this.map.baseLayer.projection.srsCode )
            {
                this._kmlFormat.externalProj = this.projection;
                this._kmlFormat.internalProj = this.map.baseLayer.projection;
            }
            this._kmlFormat.proxy = this.proxy;
            var features :Vector.<Feature> = this._kmlFormat.read( this._xml ) as Vector.<Feature>;
            this.addFeatures( features );

            this.clear();
            this.draw();
        }
        catch ( error :Error )
        {
            Trace.error( error.message );
        }
    }

    protected function onFailure ( event :Event ) :void
    {
        this.loading = false;
        Trace.error( "Error when loading kml " + this._url );
    }

    public function get url () :String
    {
        return this._url;
    }

    public function set url ( value :String ) :void
    {
        this._url = value;
    }

    override public function getURL ( bounds :Bounds ) :String
    {
        return this._url;
    }

}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜