天下之事,必先处之难,而后易之。

OpenLayers访问WTMS服务及添加Googlemap

1、访问WMS服务

首先需要发布WMS服务,才能进行地图WMS服务访问。这里不说怎么发布WMS服务,直接看怎么调用,代码如下:

代码 

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>OpenLayers map preview</title>
        <style type="text/css">
        #map {
            width: 800px;
            height: 380px;
            border: 1px solid black;
        }
        </style>

        <script src="http://localhost:8080/geoserver/openlayers/OpenLayers.js"
            type="text/javascript">
        </script>    


        <script type="text/javascript">
        function setHTML(response) 
        {
            OpenLayers.Util.getElement('nodelist').innerHTML = response.responseText;
        };
        function init()
        {
            var map = new OpenLayers.Map("map",{controls:[], 'projection': 'EPSG:4326', 'units':'degrees'});
            OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
            var bounds = new OpenLayers.Bounds(114.08473735,22.5444392,114.09911765000001,22.5517168);
            tiled = new OpenLayers.Layer.WMS("topp:danwei_font_point", "http://localhost:8080/geoserver/wms",
            {height: '380',width: '800',layers: 'topp:danwei_font_point',styles: '',srs: 'EPSG:4326',format: 'image/png', tiled: 'true', tilesOrigin :"114.08473735,22.5444392"},
            {maxExtent: bounds, maxResolution: 5.617304687505209E-5, projection: "EPSG:4326", buffer: 0});
            map.addLayer(tiled);
            jiaotonggandao_region = new OpenLayers.Layer.WMS("topp:jiaotonggandao_region", "http://localhost:8080/geoserver/wms",
            {height: '392',width: '800',layers: 'topp:jiaotonggandao_region',styles: '',srs: 'EPSG:4326', transparent: "true",format: 'image/png', tiled:'true', tilesOrigin :"114.0836293,22.543578500000002"},
            {maxExtent: bounds, maxResolution: 6.49429687499814E-5, projection: "EPSG:4326", buffer: 0});
            map.addLayer(jiaotonggandao_region);
            daoluzhongxinxian_polyline = new OpenLayers.Layer.WMS("topp:daoluzhongxinxian_polyline", "http://localhost:8080/geoserver/wms",
            {height: '392',width: '800',layers: 'topp:daoluzhongxinxian_polyline',styles: '',srs: 'EPSG:4326', transparent: "true",format: 'image/png', tiled:'true', tilesOrigin : "114.0836293,22.543578500000002"},
            {maxExtent: bounds, maxResolution: 6.49429687499814E-5, projection: "EPSG:4326", buffer: 0,isBaseLayer:false});
            map.addLayer(daoluzhongxinxian_polyline);
            shangsha_font_point = new OpenLayers.Layer.WMS("topp:shangsha_font_point", "http://localhost:8080/geoserver/wms",
            {width: '800',layers: 'topp:shangsha_font_point',styles: '',srs: 'EPSG:4326',height: '381',format: 'image/png', transparent: "true",tiled: 'true', tilesOrigin : "114.0838415,22.543650900000003"},
            {maxExtent: bounds, maxResolution: 6.20898437499462E-5, projection: "EPSG:4326", buffer: 0,isBaseLayer:false});
            map.addLayer(shangsha_font_point);
            untiled = new OpenLayers.Layer.WMS.Untiled("topp:danwei_font_point", "http://localhost:8080/geoserver/wms",
            {height: '380',width: '800',layers: 'topp:danwei_font_point',styles: '',srs: 'EPSG:4326',format: 'image/png'},
            {maxExtent: bounds, maxResolution: 5.617304687505209E-5, projection: "EPSG:4326"});
            untiled.ratio=1;
            untiled.setVisibility(false, false);
            map.addControl(new OpenLayers.Control.PanZoomBar({div:$('nav')}));
            map.addControl(new OpenLayers.Control.MouseDefaults());
            map.addControl(new OpenLayers.Control.Scale($('scale')));
            map.addControl(new OpenLayers.Control.MousePosition({element: $('position')}));
            map.addControl(new OpenLayers.Control.LayerSwitcher());
            map.addControl(new OpenLayers.Control.OverviewMap());
            map.zoomToExtent(bounds);
            map.events.register('click', map,
            function (e)
            {
                OpenLayers.Util.getElement('nodelist').innerHTML = "Loading… please wait..." + map.layers[0].name;
                var url =  map.layers[0].getFullRequestString({
                REQUEST: "GetFeatureInfo",
                EXCEPTIONS: "application/vnd.ogc.se_xml",
                BBOX: map.getExtent().toBBOX(),
                X: e.xy.x,
                Y: e.xy.y,
                INFO_FORMAT: 'text/html',
                QUERY_LAYERS: map.layers[0].params.LAYERS,
                FEATURE_COUNT: 50,
                layers: 'topp:danwei_font_point',
                styles: '',srs: 'EPSG:4326',WIDTH: map.size.w,HEIGHT: map.size.h},
                "http://localhost:8080/geoserver/wms");
                OpenLayers.loadURL(url, '', this, setHTML, setHTML);
                Event.stop(e);
            });
            
        }
</script>
    </head>

    <body onload="init()">
        <table>
            <tr>
                <td style="width: 40px" valign="middle" rowspan="3">
                    <div id="nav"></div>
                </td>
                <td colspan="3" align="right">
                    <a id="untiledLink" href="#"
                        onclick="map.removeLayer(tiled);map.addLayer(untiled);">Untiled</a>
                    <a id="tiledLink" href="#"
                        onclick="map.removeLayer(untiled);map.addLayer(tiled);">Tiled</a>
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <div id="map"></div>
                </td>
            </tr>
            <tr>
                <td>
                    <div id="scale"></div>
                </td>
                <td align="right">
                    <div id="position"></div>
                </td>
            </tr>
        </table>

        <div id="nodelist">
            Click on the map to get feature
        </div>
    </body>
</html>
View Code

2、添加Googlemap图层

1)、申请Google 地图key:

     申请Google 地图 API 的keyhttp://www.google.com/intl/zh-CN/apis/maps/signup.html
  申请的url填写为: http://yourhost/:8080/

2)、在HTml中添加地图引用:

<script charset="utf-8" src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAB6A1_oyBce6PP1YjsfO0_hQNFBmrp5F93wWCsYm0Hw_cwNHkjhT-j-A3DS2sOTKDXHL3iAgKKdglFQ" type="text/javascript">
</script> 

3)、在init方法中加入如下代码:

//Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->//加载google地图
var googlesatellite = new OpenLayers.Layer.Google("Google Satellite", {type:G_SATELLITE_MAP, 'maxZoomLevel':18} );
map.addLayers([googlesatellite]);
var googlebybrid = new OpenLayers.Layer.Google("Google Hybrid", {type:G_HYBRID_MAP});
map.addLayers([googlebybrid]);
var GMapsStreets = new OpenLayers.Layer.Google("Google Streets", {type:G_NORMAL_MAP, 'maxZoomLevel':18} );
map.addLayers([GMapsStreets]);

 

 

posted @ 2014-06-20 16:10  boonya  阅读(658)  评论(0)    收藏  举报
我有佳人隔窗而居,今有伊人明月之畔。
轻歌柔情冰壶之浣,涓涓清流梦入云端。
美人如娇温雅悠婉,目遇赏阅适而自欣。
百草层叠疏而有致,此情此思怀彼佳人。
念所思之唯心叩之,踽踽彳亍寤寐思之。
行云如风逝而复归,佳人一去莫知可回?
深闺冷瘦独自徘徊,处处明灯影还如只。
推窗见月疑是归人,阑珊灯火托手思忖。
庐居闲客而好品茗,斟茶徐徐漫漫生烟。

我有佳人在水之畔,瓮载渔舟浣纱归还。
明月相照月色还低,浅近芦苇深深如钿。
庐山秋月如美人衣,画堂春阁香气靡靡。
秋意幽笃残粉摇曳,轻轻如诉画中蝴蝶。
泾水潺潺取尔浇园,暮色黄昏如沐佳人。
青丝撩弄长裙翩翩,彩蝶飞舞执子手腕。
香带丝缕缓缓在肩,柔美体肤寸寸爱怜。
如水之殇美玉成欢,我有佳人清新如兰。
伊人在水我在一边,远远相望不可亵玩。