<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
var map = null;
function init()
{
//创建map对象,
map = new OpenLayers.Map("map");
map.addControl(new OpenLayers.Control.LayerSwitcher());
//创建google Map图层对象
var gphy = new OpenLayers.Layer.Google(
"Google Physical", //Google Map Physical图层
{type: G_PHYSICAL_MAP}
);
var gmap = new OpenLayers.Layer.Google(
"Google Streets", //Google Map Streets图层
{numZoomLevels: 20} //设置Google Map的zoom级别
);
var ghyb = new OpenLayers.Layer.Google(
"Google Hybrid", //Google Physical图层
{type: G_HYBRID_MAP, numZoomLevels: 20}
);
var gsat = new OpenLayers.Layer.Google(
"Google Satellite", //Google Map Satellite图层
{type: G_SATELLITE_MAP, numZoomLevels: 22}
);
// 添加图层
map.addLayers([gphy, gmap, ghyb, gsat]);
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.addControl(new OpenLayers.Control.PanZoomBar());
map.addControl(new OpenLayers.Control.NavToolbar());
/********************************************************/
var vectoruLayer = new OpenLayers.Layer.Vector("Vector Layer", {
styleMap: new OpenLayers.StyleMap({
pointRadius: 3,
strokeColor: "#ff3300",
strokeWidth: 3,
fillOpacity: 0
}) });
map.addLayer(vectoruLayer);
/*
var panel = new OpenLayers.Control.Panel(
{
displayClass: "olControlEditingToolbar"
});
//var options = {handlerOptions: {freehand: true}};//控制是否可以画任意图形
var draw = new OpenLayers.Control.DrawFeature(
vectoruLayer, OpenLayers.Handler.Polygon,
{
displayClass: "olControlDrawFeaturePath",
title: "Draw Lines",
handlerOptions: {freehand: true}
});
panel.addControls([
new OpenLayers.Control.Navigation({title: "Navigate"}),draw ]);
map.addControl(panel);*/
/********************************************************/
/**********************************************************/
var selectControl,drawControl,popup,selectedFeature;
function onFeatureSelect(feature){
selectedFeature = feature;
popup = new OpenLayers.Popup.FramedCloud("chicken",
feature.geometry.getBounds().getCenterLonLat(),
null,
"
lon "+feature.geometry.bounds.centerLonLat.lon
+",lat"+feature.geometry.bounds.centerLonLat.lat
+"
Area: " + feature.geometry.getArea()+"
null, true, clopsePopup);
feature.popup = popup;
map.addPopup(popup);
}
function onFeatureUnselect(feature){
map.removePopup(feature.popup);
feature.popup.destroy();
feature.popup = null;
}
function clopsePopup(feature){
selectControl.unselect(selectedFeature);
}
//点击事件
selectControl = new OpenLayers.Control.SelectFeature(vectoruLayer,
{
onSelect:onFeatureSelect,
onUnselect:onFeatureUnselect
});
drawControl =new OpenLayers.Control.DrawFeature(vectoruLayer,OpenLayers.Handler.Polygon)
controls ={
select:selectControl,
line:drawControl
}
for(var key in controls){//循环的只是键
map.addControl(controls[key]);
}
/**********************************************************/
// 放大到全屏
map.zoomToMaxExtent();
}
function toggleControl(element){
for(var key in controls){
var control = controls[key];
if(element.value==key && element.checked){
control.activate();//激活选择工具,否则不可用
}else{
control.deactivate();
}
}
}