OpenScales – Add Google Maps Layer

google mapsopenlayers-2openscales

I'm using OpenScales in a web mapping project, and I would like to add a Google Maps layer. I know that there's some licence problems, but I don't care for these licence issues, I'm looking to add Google Maps layer to OpenScales like it has been made on OpenLayers.

Best Answer

You must create your own TMS layer and server side proxy script with crossdomain.xml

package openscalesmap {

import org.openscales.core.layer.TMS;
import org.openscales.geometry.basetypes.Bounds;
import org.openscales.geometry.basetypes.Location;
import org.openscales.proj4as.ProjProjection;
import flash.system.Security;

public class GoogleMap extends TMS
{
    private var _serviceVersion:String = "1.0.0";       
    private var _tileOrigin:Location = null;        
    private var _format:String = "png";     
    private var _layerName:String;
    private var _type:String;
    private var _proxy_url:String="http://proxysite.com/swf/proxy.php?url=";

    public static const HYBRID:String="mt0.google.com/vt/lyrs=h@159000000&hl=ru";
    public static const SAT:String="khm0.google.ru/kh/v=90";
    public static const STANDART:String="mt0.google.com/vt/lyrs=m@159000000&hl=ru";

    public function GoogleMap(name:String, url:String, layerName:String="", type:String=SAT)
    {
        super(name, url, layerName);
        //flash.system.Security.allowDomain("mt0.google.com");
    /*  Security.loadPolicyFile("http://google.com/crossdomain.xml");
        Security.allowDomain( "*" );
        Security.allowInsecureDomain( "*" );*/
        this.projection = new ProjProjection("EPSG:900913");
        this._layerName = layerName;
        _type=type;
        this.maxExtent = new Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34);
    }
    //-------------------------------------------------------------------
    override public function getURL(bounds:Bounds):String {
        var res:Number = this.map.resolution;
        if(this._tileOrigin==null) {
            this._tileOrigin = new Location(this.maxExtent.left,this.maxExtent.bottom);
        }

        var x:Number = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileWidth));
        var y:Number = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileHeight));
        var z:Number = this.map.zoom;
        var limit:Number = Math.pow(2, z);


        x = ((x % limit) + limit) % limit;
        y = ((y % limit) + limit) % limit;
        var url:String =_proxy_url+escape(_type + "&z=" + z + "&x=" + x + "&y=" + y); 

        if (this.altUrls != null) {
            url = this.selectUrl(this.url, this.getUrls());
        }

        return url ;
    }
}

}

And change these lines for your language:

public static const HYBRID:String="mt0.google.com/vt/lyrs=h@159000000&hl=ru";
public static const SAT:String="khm0.google.ru/kh/v=90";
public static const STANDART:String="mt0.google.com/vt/lyrs=m@159000000&hl=ru";