openlayers中利用vector实现marker的方式
2013-07-24 10:12
183 查看
项目需要一个小型的gis。openlayers,geoserver,postgres组合是比较好的选择。
openlayers的marker层好像不支持拖动操作。通过研究api发现,可以利用vector层
达到这个目的,作出标注的效果。可以定位,搜索,拖动等效果,选中的时候可以
通过修改style达到动画效果。
基本做法如下:
1:定义marker显示的样式
2:扩展vector层,利用在扩展层上添加point与style,图片显示point就出现标注的
效果
基本代码如下:
定义样式:
Java代码
$package("com.bct.map");
com.bct.map.EncoderMarkerStyle = {
'bigEncoder':{
graphicWidth:24,
graphicHeight : 24,
graphicXOffset : -12,
graphicYOffset : -24,
externalGraphic : "scripts/map/img/channel2.png"
},
'smallEncoder':{
graphicWidth:16,
graphicHeight : 16,
graphicXOffset : -8,
graphicYOffset : -16,
externalGraphic : "scripts/map/img/channel.gif"
},
'selectStyle':{
pointerEvents: "visiblePainted",
border:"border:25 outset #ff88ff",
cursor: "pointer",
graphicWidth:24,
graphicHeight : 24,
graphicXOffset : -12,
graphicYOffset : -24,
externalGraphic : "scripts/map/img/channel2.png"
},
styleMap: new OpenLayers.StyleMap({
"select": new OpenLayers.Style({pointRadius: 24})
})
}
[java] view plaincopy
$package("com.bct.map");
com.bct.map.EncoderMarkerStyle = {
'bigEncoder':{
graphicWidth:24,
graphicHeight : 24,
graphicXOffset : -12,
graphicYOffset : -24,
externalGraphic : "scripts/map/img/channel2.png"
},
'smallEncoder':{
graphicWidth:16,
graphicHeight : 16,
graphicXOffset : -8,
graphicYOffset : -16,
externalGraphic : "scripts/map/img/channel.gif"
},
'selectStyle':{
pointerEvents: "visiblePainted",
border:"border:25 outset #ff88ff",
cursor: "pointer",
graphicWidth:24,
graphicHeight : 24,
graphicXOffset : -12,
graphicYOffset : -24,
externalGraphic : "scripts/map/img/channel2.png"
},
styleMap: new OpenLayers.StyleMap({
"select": new OpenLayers.Style({pointRadius: 24})
})
}
2:扩展向量层
Java代码
$package("com.bct.map");
$import("com.bct.map.EncoderMarkerStyle");
com.bct.map.MarkerVectorLayer = OpenLayers.Class(OpenLayers.Layer.Vector,{
/**
* parameters
* attribute filer对象
*/
getFeatureByAttribute :function(attributes){
var feature = null;
for(var i=0;i<this.features.length; ++i){
var attri = this.features[i].attributes;
var find = false;
for(var j in attributes){
if(attributes[j] == attri[j]){
find = true;
}
}
if(find){
return this.features[i];
}
}
},
//添加Feature,是point显示为图片
addEncorderFeature:function(encNode,location){
if(encNode&&this.repetitiveCheck(encNode.id)){
return;
}
var attributes = OpenLayers.Util.extend({}, encNode.attributes);
var enc_point = new OpenLayers.Geometry.Point(location.lon,location.lat);
var enc_Feature = new OpenLayers.Feature.Vector(enc_point,attributes,com.bct.map.EncoderMarkerStyle['smallEncoder']);
this.addFeatures([enc_Feature]);
if(encNode.attributes['lon']&&encNode.attributes['lat']&&encNode.attributes['lon'].length>0){
return;
}
this.updateChannel(encNode.id,location.lon,location.lat);
},
repetitiveCheck:function(entity_id){
if(this.getFeatureByAttribute({id:entity_id})){
return true;
}
return false;
},
updateChannel:function(channel_id,lon,lat){
Ext.Ajax.request({
url: 'deviceVideoEncoder.do?method=updateLonlat&id='+channel_id+"&lon="+lon+"&lat="+lat
});
},
channelMarkerClick:function() {
var features = this.selectedFeatures;
if(features.length >=0&&features[0]) {
feature = features[0];
var treeNodeAttribute = feature.attributes;
//显示信息
}
},
cartoonFeature :function(feature){
this.drawFeature(feature,com.bct.map.EncoderMarkerStyle['bigEncoder']);
var runner = new Ext.util.TaskRunner(1000);
var task = {
run:this.drawFeature,
scope:this,
args:[feature,com.bct.map.EncoderMarkerStyle['smallEncoder']],
interval: 1000
}
runner.start(task);
},
removeSelectFeature:function(){
var features = this.selectedFeatures;
for(var i=features.length-1; i>=0; i--) {
feature = features[i];
this.updateChannel(feature.attributes['id'],"","");
}
this.destroyFeatures(this.selectedFeatures);
},
monitorSelectFeature:function(){
var features = this.selectedFeatures;
if(features.length >=0&&features[0]) {
feature = features[0];
var treeNodeAttribute = feature.attributes;
var objId="mapAVShow"+treeNodeAttribute['id'];
//弹出窗口
}
}
});
[java] view plaincopy
$package("com.bct.map");
$import("com.bct.map.EncoderMarkerStyle");
com.bct.map.MarkerVectorLayer = OpenLayers.Class(OpenLayers.Layer.Vector,{
/**
* parameters
* attribute filer对象
*/
getFeatureByAttribute :function(attributes){
var feature = null;
for(var i=0;i<this.features.length; ++i){
var attri = this.features[i].attributes;
var find = false;
for(var j in attributes){
if(attributes[j] == attri[j]){
find = true;
}
}
if(find){
return this.features[i];
}
}
},
//添加Feature,是point显示为图片
addEncorderFeature:function(encNode,location){
if(encNode&&this.repetitiveCheck(encNode.id)){
return;
}
var attributes = OpenLayers.Util.extend({}, encNode.attributes);
var enc_point = new OpenLayers.Geometry.Point(location.lon,location.lat);
var enc_Feature = new OpenLayers.Feature.Vector(enc_point,attributes,com.bct.map.EncoderMarkerStyle['smallEncoder']);
this.addFeatures([enc_Feature]);
if(encNode.attributes['lon']&&encNode.attributes['lat']&&encNode.attributes['lon'].length>0){
return;
}
this.updateChannel(encNode.id,location.lon,location.lat);
},
repetitiveCheck:function(entity_id){
if(this.getFeatureByAttribute({id:entity_id})){
return true;
}
return false;
},
updateChannel:function(channel_id,lon,lat){
Ext.Ajax.request({
url: 'deviceVideoEncoder.do?method=updateLonlat&id='+channel_id+"&lon="+lon+"&lat="+lat
});
},
channelMarkerClick:function() {
var features = this.selectedFeatures;
if(features.length >=0&&features[0]) {
feature = features[0];
var treeNodeAttribute = feature.attributes;
//显示信息
}
},
cartoonFeature :function(feature){
this.drawFeature(feature,com.bct.map.EncoderMarkerStyle['bigEncoder']);
var runner = new Ext.util.TaskRunner(1000);
var task = {
run:this.drawFeature,
scope:this,
args:[feature,com.bct.map.EncoderMarkerStyle['smallEncoder']],
interval: 1000
}
runner.start(task);
},
removeSelectFeature:function(){
var features = this.selectedFeatures;
for(var i=features.length-1; i>=0; i--) {
feature = features[i];
this.updateChannel(feature.attributes['id'],"","");
}
this.destroyFeatures(this.selectedFeatures);
},
monitorSelectFeature:function(){
var features = this.selectedFeatures;
if(features.length >=0&&features[0]) {
feature = features[0];
var treeNodeAttribute = feature.attributes;
var objId="mapAVShow"+treeNodeAttribute['id'];
//弹出窗口
}
}
});
addEncorderFeature是添加一个标注的地方。
利用这种方式,比原有的marker层拥有更多的功能
openlayers的marker层好像不支持拖动操作。通过研究api发现,可以利用vector层
达到这个目的,作出标注的效果。可以定位,搜索,拖动等效果,选中的时候可以
通过修改style达到动画效果。
基本做法如下:
1:定义marker显示的样式
2:扩展vector层,利用在扩展层上添加point与style,图片显示point就出现标注的
效果
基本代码如下:
定义样式:
Java代码
$package("com.bct.map");
com.bct.map.EncoderMarkerStyle = {
'bigEncoder':{
graphicWidth:24,
graphicHeight : 24,
graphicXOffset : -12,
graphicYOffset : -24,
externalGraphic : "scripts/map/img/channel2.png"
},
'smallEncoder':{
graphicWidth:16,
graphicHeight : 16,
graphicXOffset : -8,
graphicYOffset : -16,
externalGraphic : "scripts/map/img/channel.gif"
},
'selectStyle':{
pointerEvents: "visiblePainted",
border:"border:25 outset #ff88ff",
cursor: "pointer",
graphicWidth:24,
graphicHeight : 24,
graphicXOffset : -12,
graphicYOffset : -24,
externalGraphic : "scripts/map/img/channel2.png"
},
styleMap: new OpenLayers.StyleMap({
"select": new OpenLayers.Style({pointRadius: 24})
})
}
[java] view plaincopy
$package("com.bct.map");
com.bct.map.EncoderMarkerStyle = {
'bigEncoder':{
graphicWidth:24,
graphicHeight : 24,
graphicXOffset : -12,
graphicYOffset : -24,
externalGraphic : "scripts/map/img/channel2.png"
},
'smallEncoder':{
graphicWidth:16,
graphicHeight : 16,
graphicXOffset : -8,
graphicYOffset : -16,
externalGraphic : "scripts/map/img/channel.gif"
},
'selectStyle':{
pointerEvents: "visiblePainted",
border:"border:25 outset #ff88ff",
cursor: "pointer",
graphicWidth:24,
graphicHeight : 24,
graphicXOffset : -12,
graphicYOffset : -24,
externalGraphic : "scripts/map/img/channel2.png"
},
styleMap: new OpenLayers.StyleMap({
"select": new OpenLayers.Style({pointRadius: 24})
})
}
2:扩展向量层
Java代码
$package("com.bct.map");
$import("com.bct.map.EncoderMarkerStyle");
com.bct.map.MarkerVectorLayer = OpenLayers.Class(OpenLayers.Layer.Vector,{
/**
* parameters
* attribute filer对象
*/
getFeatureByAttribute :function(attributes){
var feature = null;
for(var i=0;i<this.features.length; ++i){
var attri = this.features[i].attributes;
var find = false;
for(var j in attributes){
if(attributes[j] == attri[j]){
find = true;
}
}
if(find){
return this.features[i];
}
}
},
//添加Feature,是point显示为图片
addEncorderFeature:function(encNode,location){
if(encNode&&this.repetitiveCheck(encNode.id)){
return;
}
var attributes = OpenLayers.Util.extend({}, encNode.attributes);
var enc_point = new OpenLayers.Geometry.Point(location.lon,location.lat);
var enc_Feature = new OpenLayers.Feature.Vector(enc_point,attributes,com.bct.map.EncoderMarkerStyle['smallEncoder']);
this.addFeatures([enc_Feature]);
if(encNode.attributes['lon']&&encNode.attributes['lat']&&encNode.attributes['lon'].length>0){
return;
}
this.updateChannel(encNode.id,location.lon,location.lat);
},
repetitiveCheck:function(entity_id){
if(this.getFeatureByAttribute({id:entity_id})){
return true;
}
return false;
},
updateChannel:function(channel_id,lon,lat){
Ext.Ajax.request({
url: 'deviceVideoEncoder.do?method=updateLonlat&id='+channel_id+"&lon="+lon+"&lat="+lat
});
},
channelMarkerClick:function() {
var features = this.selectedFeatures;
if(features.length >=0&&features[0]) {
feature = features[0];
var treeNodeAttribute = feature.attributes;
//显示信息
}
},
cartoonFeature :function(feature){
this.drawFeature(feature,com.bct.map.EncoderMarkerStyle['bigEncoder']);
var runner = new Ext.util.TaskRunner(1000);
var task = {
run:this.drawFeature,
scope:this,
args:[feature,com.bct.map.EncoderMarkerStyle['smallEncoder']],
interval: 1000
}
runner.start(task);
},
removeSelectFeature:function(){
var features = this.selectedFeatures;
for(var i=features.length-1; i>=0; i--) {
feature = features[i];
this.updateChannel(feature.attributes['id'],"","");
}
this.destroyFeatures(this.selectedFeatures);
},
monitorSelectFeature:function(){
var features = this.selectedFeatures;
if(features.length >=0&&features[0]) {
feature = features[0];
var treeNodeAttribute = feature.attributes;
var objId="mapAVShow"+treeNodeAttribute['id'];
//弹出窗口
}
}
});
[java] view plaincopy
$package("com.bct.map");
$import("com.bct.map.EncoderMarkerStyle");
com.bct.map.MarkerVectorLayer = OpenLayers.Class(OpenLayers.Layer.Vector,{
/**
* parameters
* attribute filer对象
*/
getFeatureByAttribute :function(attributes){
var feature = null;
for(var i=0;i<this.features.length; ++i){
var attri = this.features[i].attributes;
var find = false;
for(var j in attributes){
if(attributes[j] == attri[j]){
find = true;
}
}
if(find){
return this.features[i];
}
}
},
//添加Feature,是point显示为图片
addEncorderFeature:function(encNode,location){
if(encNode&&this.repetitiveCheck(encNode.id)){
return;
}
var attributes = OpenLayers.Util.extend({}, encNode.attributes);
var enc_point = new OpenLayers.Geometry.Point(location.lon,location.lat);
var enc_Feature = new OpenLayers.Feature.Vector(enc_point,attributes,com.bct.map.EncoderMarkerStyle['smallEncoder']);
this.addFeatures([enc_Feature]);
if(encNode.attributes['lon']&&encNode.attributes['lat']&&encNode.attributes['lon'].length>0){
return;
}
this.updateChannel(encNode.id,location.lon,location.lat);
},
repetitiveCheck:function(entity_id){
if(this.getFeatureByAttribute({id:entity_id})){
return true;
}
return false;
},
updateChannel:function(channel_id,lon,lat){
Ext.Ajax.request({
url: 'deviceVideoEncoder.do?method=updateLonlat&id='+channel_id+"&lon="+lon+"&lat="+lat
});
},
channelMarkerClick:function() {
var features = this.selectedFeatures;
if(features.length >=0&&features[0]) {
feature = features[0];
var treeNodeAttribute = feature.attributes;
//显示信息
}
},
cartoonFeature :function(feature){
this.drawFeature(feature,com.bct.map.EncoderMarkerStyle['bigEncoder']);
var runner = new Ext.util.TaskRunner(1000);
var task = {
run:this.drawFeature,
scope:this,
args:[feature,com.bct.map.EncoderMarkerStyle['smallEncoder']],
interval: 1000
}
runner.start(task);
},
removeSelectFeature:function(){
var features = this.selectedFeatures;
for(var i=features.length-1; i>=0; i--) {
feature = features[i];
this.updateChannel(feature.attributes['id'],"","");
}
this.destroyFeatures(this.selectedFeatures);
},
monitorSelectFeature:function(){
var features = this.selectedFeatures;
if(features.length >=0&&features[0]) {
feature = features[0];
var treeNodeAttribute = feature.attributes;
var objId="mapAVShow"+treeNodeAttribute['id'];
//弹出窗口
}
}
});
addEncorderFeature是添加一个标注的地方。
利用这种方式,比原有的marker层拥有更多的功能
相关文章推荐
- openlayers中利用vector实现marker的方式
- 利用C实现泛型效果(generics)---vector
- 【iOS开发-79】利用Modal方式实现控制器之间的跳转
- spring利用注解方式实现Java读取properties属性值
- Spring连接数据库的方式1:利用Spring实现bean属性setter方式注入
- Android利用vectordrawable实现轨迹动画
- 利用window.open实现post方式的参数传递
- 利用pdfJS实现以读取文件流方式在线展示pdf文件
- 利用Ajax实现的广告流动条的两种方式
- 利用gitolite实现自动部署的简易实现方式
- SQL SERVER 2008 利用发布订阅方式实现数据库同步
- <HTTP>ASI实现的注册方法:利用http的get和post两种方式
- Java 集合:Collection,List,ArrayList,Vector,LinkedList(实现方式,对比)
- JS利用ActiveX控件的方式,即 Excel.Application,实现对word或者excel的打印
- vector实现邻接表的两种方式
- 【XML解析】(6)利用XML+DOM4j+Xpath+MD5加密+图形界面(Jtabe+Vector)实现《通讯录管理系统》
- stack vector array 三种方式实现字符的反向输出
- Android利用android:indeterminateDrawable来实现ProgressBar三种方式
- 利用SPring实现bean属性setter方式注入
- 缺少jar包导致Spring利用注解的方式来实现AOP所出现的错误