WMS as a single tile image in Google Maps v3

2020-07-30 01:32发布

问题:

I am following the code at http://www.gisdoctor.com/v3/mapserver.html to overlay a WMS as an image on Google Maps using API v3. The js code at the above link is as follows

"WMSGetTileUrl" : function(tile, zoom) {
    var projection = map.getProjection();
    var zpow = Math.pow(2, zoom);
    var ul = new google.maps.Point(
        tile.x * 256.0 / zpow, 
        (tile.y + 1) * 256.0 / zpow
    );
    var lr = new google.maps.Point(
        (tile.x + 1) * 256.0 / zpow, 
        tile.y * 256.0 / zpow
    );
    var ulw = projection.fromPointToLatLng(ul);
    var lrw = projection.fromPointToLatLng(lr);

    var bbox = ulw.lng() + "," + ulw.lat() + "," + lrw.lng() + "," + lrw.lat();

    return url = "http://url/to/mapserver?" + 
        "version=1.1.1&" + 
        "request=GetMap&" + 
        "Styles=default&" + 
        "SRS=EPSG:4326&" + 
        "Layers=wmsLayers&" + 
        "BBOX=" + bbox + "&" + 
        "width=256&" + 
        "height=256&" + 
        "format=image/png&" + 
        "TRANSPARENT=TRUE";
},

"addWmsLayer" : function() {

    /*
    Creating the WMS layer options. This code creates the Google 
    imagemaptype options for each wms layer. In the options the function 
    that calls the individual wms layer is set 
    */
    var wmsOptions = {
        alt: "MapServer Layers",
        getTileUrl: WMSGetTileUrl,
        isPng: false,
        maxZoom: 17,
        minZoom: 1,
        name: "MapServer Layer",
        tileSize: new google.maps.Size(256, 256)
    };

    /*
    Creating the object to create the ImageMapType that will call the WMS 
    Layer Options. 
    */
    wmsMapType = new google.maps.ImageMapType(wmsOptions);
    map.overlayMapTypes.insertAt(0, wmsMapType);
},

Everything works fine, but, of course, the WMS is returned as 256 x 256 tiles. No surprises, because that is what I requested. However, following the discussion at http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/c22837333f9a1812/d410a2a453025b38 it seems that I might be better off requesting an untiled (single) image from mapserver. This would tax my server less. In any case, I would like to experiment with a single image, but I am unable to construct a request for one properly.

Specifically, I changed the size of the tile to something large; for example, I tried 1024 x 1024 tiles. I did get fewer tiles, but the returned images didn't match the Google Maps base layer boundaries.

What I would like is to not specify the tile size at all. Instead, I should dynamically figure out the tile size to be, say, 256 pixels bigger than the current map size. That way, a single image would be returned, no matter what the map size. Also, a seamless pan would be implemented with the help of the extra 256px along the map edges.

Suggestions?

回答1:

1) it works for me - with 512 and 1024 also. You just have to make the change at all appropriate places (corner coordinate computation, tileSize setting, WMS parameters width & height setting). Any tile setting should be possible if you just handle it correctly at all places in code.

2) I think you are trying to optimize at wrong place. The 256x256 tiles have been established for a reason. Remember that google is caching the tiles, so with larger tiles the whole solution might be much slower. Also the map will be loaded more fast for the user.

3) The tile concept is a right thing and should be preserved. Things like receiving the whole map wouldn't work - imagine map panning then. DOn't try to reinvent the wheel and stick with the current google tile concept - it is optimized for this type of application. IF you need to receive the map as a single image, you can use the static google maps api.



回答2:

See this same question I just answered for someone else. It's likely to be your projection. EPSG:4326 is not the correct projection for Google Maps. Changing the projection means you need to change the way coordinates are calculated and referenced.