就为了这个小问题我把baidu、google都翻遍了,最终还是在OpenLayers自带的Api doc中找到了解决办法。
废话少说,分三步来完成:1.创建Geometry对象;2.创建Feture对象。3.把Feture对象添加到Layer中。
example:
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title ></ title >
< meta content ="text/html; charset=utf-8" http-equiv ="Content-Type" />
< style type ="text/css" >
#map
{}{
border-right: black 1px solid;
border-top: black 1px solid;
border-left: black 1px solid;
width: 512px;
border-bottom: black 1px solid;
height: 512px;
}
</ style >
< script src ="OpenLayers/OpenLayers.js" type ="text/javascript" ></ script >
< script type ="text/javascript" >
var map;
function init()
{
map = new OpenLayers.Map('map');
//初始化背景图层
var baseLayer = new OpenLayers.Layer.WMS("China","http://127.0.0.1:8080/geoserver/wms?",{layers:'topp:bou2_4p'});
//创建矢量图层
var vectorLayer = new OpenLayers.Layer.Vector("vector");
//创建Geometry对象
var point = new OpenLayers.Geometry.Point(115,32);
//创建Feature对象
var fea = new OpenLayers.Feature.Vector(point);
//把Feature对象添加到图层
vectorLayer.addFeatures(fea);
map.addLayers([baseLayer,vectorLayer]);
map.setCenter(new OpenLayers.LonLat(115,32),3);
}
</ script >
< meta name ="GENERATOR" content ="MSHTML 6.00.6001.17184" />
</ head >
< body onload ="init()" >
< div id ="map" >
</ div >
</ body >
</ html >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title ></ title >
< meta content ="text/html; charset=utf-8" http-equiv ="Content-Type" />
< style type ="text/css" >
#map
{}{
border-right: black 1px solid;
border-top: black 1px solid;
border-left: black 1px solid;
width: 512px;
border-bottom: black 1px solid;
height: 512px;
}
</ style >
< script src ="OpenLayers/OpenLayers.js" type ="text/javascript" ></ script >
< script type ="text/javascript" >
var map;
function init()
{
map = new OpenLayers.Map('map');
//初始化背景图层
var baseLayer = new OpenLayers.Layer.WMS("China","http://127.0.0.1:8080/geoserver/wms?",{layers:'topp:bou2_4p'});
//创建矢量图层
var vectorLayer = new OpenLayers.Layer.Vector("vector");
//创建Geometry对象
var point = new OpenLayers.Geometry.Point(115,32);
//创建Feature对象
var fea = new OpenLayers.Feature.Vector(point);
//把Feature对象添加到图层
vectorLayer.addFeatures(fea);
map.addLayers([baseLayer,vectorLayer]);
map.setCenter(new OpenLayers.LonLat(115,32),3);
}
</ script >
< meta name ="GENERATOR" content ="MSHTML 6.00.6001.17184" />
</ head >
< body onload ="init()" >
< div id ="map" >
</ div >
</ body >
</ html >
知道了添加点的方法,添加其他图形就容易了。