代码2:Hello Viewer示例源代码
来源:互联网 发布:adobe indesign mac 编辑:程序博客网 时间:2024/05/16 14:43
说明:该源代码包含有五个页面和一个网页布局:
1. HelloViewer.html,该页面是登录页面
2. digitizeuserarea.aspx,该页面用于从地图中获取一个点作为用户区域并提交到保存页面
3. saveuserarea.aspx,该页面用于保存用户定义的用户区域
4. openuserarea.aspx,该页面用于打开用户定义的用户区域并将地图平移至该点
5. userareamain.aspx,该页面用于做为任务主页放置在网页布局中
Library://HelloViewer/HelloViewer.WebLayout,用于展示地图的网页布局
请在使用源代码前先安装IIS和asp.net编写的web端组件。使用源代码时,请将页面拷贝至web安装路径下的mapviewernet文件夹下(开源版默认安装路径为C:/Program Files/OSGeo/MapGuide/Web/www/mapviewernet)。由于源代码涉及到向该文件夹写文件,如果请给予asp.net运行用户(IIS5下为ASPNET,IIS6和7下为IIS_IUSRS)该文件夹的写权限和创建文件的权限,否则会因为无法创建文件或写文件而报错。网页布局需通过上传的方式放置在MapGuide服务器相应位置上。
1. HelloViewer.html页面源代码如下:
1. digitizeuserarea.aspx页面源代码如下: <%-- Copyright (C) 2004-2009 by Autodesk, Inc. This library is free software; you can redistribute it and/or modify it under the terms of version 2.1 of the GNU Lesser General Public License as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA --%> <%@ Page language="c#" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Web" %> <%@ Import Namespace="OSGeo.MapGuide" %> <% string errorMessage = string.Empty; bool hasUserArea = false; bool emptySessionId = false; bool redigitize = false; try { string sessionId = Request["SESSIONID"]; bool.TryParse(Request["REDIGITIZE"], out redigitize); if (string.IsNullOrEmpty(sessionId)) emptySessionId = true; else hasUserArea = HasUserArea(GetUsername(sessionId)); } catch(Exception exc) { errorMessage = exc.Message; } %> <html> <head> <script language="javascript" type="text/javascript"> function load() { var isDigitize = true; <% if(!string.IsNullOrEmpty(errorMessage)) { errorMessage = errorMessage.Replace("//", "////") .Replace("/r", "//r") .Replace("/n", "//n") .Replace("/'", "//'"); %> var errorMsg = '<%=errorMessage%>'; alert(errorMsg); <% } else if(emptySessionId) { %> var params = new Array('SESSIONID', parent.GetMapFrame().GetSessionId(), 'DATE', new Date()); parent.GetFormFrame().Submit('digitizeuserarea.aspx', params, 'scriptFrame'); <% } else{ if(hasUserArea && !redigitize) { %> isDigitize = window.confirm( '您已创建了用户区域,是否要重新创建?'); <% } %> if(isDigitize) parent.GetMapFrame().DigitizePoint(OnDigitize); <% } %> } function OnDigitize(point){ var params = new Array('SESSIONID', parent.GetMapFrame().GetSessionId(), 'CENTERX', point.X, 'CENTERY', point.Y, 'DATE', new Date()); parent.GetFormFrame().Submit('saveuserarea.aspx', params, 'scriptFrame'); } </script> </head> <body onload="load()"> </body> </html> <script runat="server"> /// <summary> /// 获取当前登录的用户名 /// </summary> /// <param name="sessionId"></param> /// <returns></returns> static string GetUsername(string sessionId) { MgSiteConnection connection = null; MgUserInformation userInfo = null; MgSite site = null; try { connection = new MgSiteConnection(); userInfo = new MgUserInformation(sessionId); connection.Open(new MgUserInformation(sessionId)); site = connection.GetSite(); return site.GetUserForSession(); } finally { if (site != null) site.Dispose(); if (connection != null) connection.Dispose(); if (userInfo != null) userInfo.Dispose(); } } /// <summary> /// 获取该用户那一行区域信息, /// 如果没有文件,或者该用户没有区域信息,会返回空字符串 /// </summary> /// <param name="username"></param> /// <returns></returns> string FetchUserAreaData(string username) { string dataFile = Server.MapPath("data/userArea.dat"); if (!File.Exists(dataFile)) return string.Empty; string[] lines; try { lines = File.ReadAllLines(dataFile); } catch (IOException exc) { throw new ArgumentException("无法读取数据文件", exc); } foreach (string line in lines) { string[] parts = line.Split('|'); if (parts.Length != 0 && parts[0].Equals(username, StringComparison.InvariantCultureIgnoreCase)) return line; } return string.Empty; } /// <summary> /// 是否存在该用户的区域信息 /// </summary> /// <param name="username"></param> /// <returns></returns> bool HasUserArea(string username) { return !string.IsNullOrEmpty(FetchUserAreaData(username)); } </script>
2. saveuserarea.aspx页面的源代码
<%--
Copyright (C) 2004-2009 by Autodesk, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of version 2.1 of the GNU Lesser
General Public License as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--%>
<%@ Page language="c#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="OSGeo.MapGuide" %>
<%
string errorMessage = string.Empty;
try
{
string sessionId = Request["SESSIONID"];
if (string.IsNullOrEmpty(sessionId)) throw new ArgumentException("会话标识不应该为空");
double centerX, centerY;
string doubleString = Request["CENTERX"];
if(string.IsNullOrEmpty(doubleString)) throw new ArgumentException("中心点坐标不应该为空");
bool parseSuccessful = double.TryParse(doubleString, out centerX);
if (!parseSuccessful) throw new ArgumentException("无法解析中心点坐标");
doubleString = Request["CENTERY"];
if (string.IsNullOrEmpty(doubleString)) throw new ArgumentException("中心点坐标不应该为空");
parseSuccessful = double.TryParse(doubleString, out centerY);
if (!parseSuccessful) throw new ArgumentException("无法解析中心点坐标");
SaveUserArea(GetUsername(sessionId), centerX, centerY);
}
catch(Exception exc)
{
errorMessage = exc.Message;
}
errorMessage = string.IsNullOrEmpty(errorMessage) ? "保存成功!" : errorMessage.Replace("//", "////").Replace("/r", "//r").Replace("/n", "//n").Replace("/'", "//'");
%>
<html>
<head>
<script language="javascript" type="text/javascript">
function load() {
var msg = '<%=errorMessage%>';
alert(msg);
}
</script>
</head>
<body onload="load()">
</body>
</html>
<script runat="server">
/// <summary>
/// 获取当前登录的用户名
/// </summary>
/// <param name="sessionId"></param>
/// <returns></returns>
static string GetUsername(string sessionId)
{
MgSiteConnection connection = null;
MgUserInformation userInfo = null;
MgSite site = null;
try
{
connection = new MgSiteConnection();
userInfo = new MgUserInformation(sessionId);
connection.Open(new MgUserInformation(sessionId));
site = connection.GetSite();
return site.GetUserForSession();
}
finally
{
if (site != null) site.Dispose();
if (connection != null) connection.Dispose();
if (userInfo != null) userInfo.Dispose();
}
}
/// <summary>
/// 保存该用户的选取点信息
/// </summary>
/// <param name="username"></param>
/// <param name="centerX"></param>
/// <param name="centerY"></param>
void SaveUserArea(string username, double centerX, double centerY)
{
string dataFile = Server.MapPath("data/userArea.dat");
if (!File.Exists(dataFile))
{
try
{
DirectoryInfo directoryInfo = Directory.GetParent(dataFile);
if(directoryInfo == null) throw new ArgumentException("无法找到数据文件所在文件夹");
if(!directoryInfo.Exists) directoryInfo.Create();
FileStream stream = File.Create(dataFile);
stream.Close();
}
catch (IOException exc)
{
throw new ArgumentException("无法创建数据文件", exc);
}
}
string[] lines;
try
{
lines = File.ReadAllLines(dataFile);
}
catch (IOException exc)
{
throw new ArgumentException("无法读取数据文件", exc);
}
bool userAlreadyExist = false;
for (int index = 0; index < lines.Length; index++)
{
string[] parts = lines[index].Split('|');
if (parts.Length == 0 || !parts[0].Equals(username, StringComparison.InvariantCultureIgnoreCase)) continue;
userAlreadyExist = true;
lines[index] = string.Format("{0}|{1}|{2}", username, centerX, centerY);
}
if(lines.Length != 0) File.WriteAllLines(dataFile, lines);
if (!userAlreadyExist)
{
File.AppendAllText(dataFile, string.Format("{0}|{1}|{2}", username, centerX, centerY));
}
}
</script>
3. openuserarea.aspx页面的源代码
<%--
Copyright (C) 2004-2009 by Autodesk, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of version 2.1 of the GNU Lesser
General Public License as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--%>
<%@ Page language="c#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="OSGeo.MapGuide" %>
<%
string errorMessage = string.Empty;
bool emptySessionId = false;
double centerX = 0;
double centerY = 0;
try
{
string sessionId = Request["SESSIONID"];
if (string.IsNullOrEmpty(sessionId))
emptySessionId = true;
else
{
string userData = FetchUserAreaData(GetUsername(sessionId));
if (string.IsNullOrEmpty(userData)) throw new ArgumentException("该用户没有设定区域!");
string[] dataParts = userData.Split('|');
if (dataParts.Length != 3) throw new ArgumentException("用户数据错误!");
string doubleString = dataParts[1];
if (string.IsNullOrEmpty(doubleString)) throw new ArgumentException("中心点坐标不应该为空");
bool parseSuccessful = double.TryParse(doubleString, out centerX);
if (!parseSuccessful) throw new ArgumentException("无法解析中心点坐标");
doubleString = dataParts[2];
if (string.IsNullOrEmpty(doubleString)) throw new ArgumentException("中心点坐标不应该为空");
parseSuccessful = double.TryParse(doubleString, out centerY);
if (!parseSuccessful) throw new ArgumentException("无法解析中心点坐标");
}
}
catch(Exception exc)
{
errorMessage = exc.Message;
}
%>
<html>
<head>
<script language="javascript" type="text/javascript">
function load() {
<%
if(!string.IsNullOrEmpty(errorMessage))
{
errorMessage = errorMessage.Replace("//", "////").Replace("/r", "//r")
.Replace("/n", "//n").Replace("/'", "//'");
%>
var errorMsg = '<%=errorMessage%>';
alert(errorMsg);
<%
} else if(emptySessionId) {
%>
var params = new Array('SESSIONID', parent.GetMapFrame().GetSessionId(), 'DATE', new Date());
parent.GetFormFrame().Submit('openuserarea.aspx', params, 'scriptFrame');
<%
}
else{
%>
parent.ZoomToView(<%=centerX%>,<%=centerY%>, parent.GetMapFrame().GetScale(), true);
alert('用户区域载入完毕');
<%
}
%>
}
</script>
</head>
<body onload="load()">
</body>
</html>
<script runat="server">
/// <summary>
/// 获取当前登录的用户名
/// </summary>
/// <param name="sessionId"></param>
/// <returns></returns>
static string GetUsername(string sessionId)
{
MgSiteConnection connection = null;
MgUserInformation userInfo = null;
MgSite site = null;
try
{
connection = new MgSiteConnection();
userInfo = new MgUserInformation(sessionId);
connection.Open(new MgUserInformation(sessionId));
site = connection.GetSite();
return site.GetUserForSession();
}
finally
{
if (site != null) site.Dispose();
if (connection != null) connection.Dispose();
if (userInfo != null) userInfo.Dispose();
}
}
/// <summary>
/// 获取该用户那一行区域信息,如果没有文件,或者该用户没有区域信息,会返回空字符串
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
string FetchUserAreaData(string username)
{
string dataFile = Server.MapPath("data/userArea.dat");
if (!File.Exists(dataFile)) return string.Empty;
string[] lines;
try
{
lines = File.ReadAllLines(dataFile);
}
catch (IOException exc)
{
throw new ArgumentException("无法读取数据文件", exc);
}
foreach (string line in lines)
{
string[] parts = line.Split('|');
if (parts.Length != 0 && parts[0].Equals(username, StringComparison.InvariantCultureIgnoreCase)) return line;
}
return string.Empty;
}
</script>
4. userareamain.aspx页面的源代码
<%--
Copyright (C) 2004-2009 by Autodesk, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of version 2.1 of the GNU Lesser
General Public License as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--%>
<%@ Page language="c#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="OSGeo.MapGuide" %>
<%
string errorMessage = string.Empty;
bool emptySessionId = false;
bool hasUserData = false;
string username = string.Empty;
double centerX = 0;
double centerY = 0;
try
{
string sessionId = Request["SESSIONID"];
if (string.IsNullOrEmpty(sessionId))
emptySessionId = true;
else
{
username = GetUsername(sessionId);
string userData = FetchUserAreaData(username);
if (!string.IsNullOrEmpty(userData))
{
string[] dataParts = userData.Split('|');
if (dataParts.Length != 3) throw new ArgumentException("用户数据错误!");
string doubleString = dataParts[1];
if (string.IsNullOrEmpty(doubleString)) throw new ArgumentException("中心点坐标不应该为空");
bool parseSuccessful = double.TryParse(doubleString, out centerX);
if (!parseSuccessful) throw new ArgumentException("无法解析中心点坐标");
doubleString = dataParts[2];
if (string.IsNullOrEmpty(doubleString)) throw new ArgumentException("中心点坐标不应该为空");
parseSuccessful = double.TryParse(doubleString, out centerY);
if (!parseSuccessful) throw new ArgumentException("无法解析中心点坐标");
hasUserData = true;
}
}
}
catch(Exception exc)
{
errorMessage = exc.Message;
}
%>
<html>
<head>
<script language="javascript" type="text/javascript">
var waitingMapReadyInterval;
function load() {
<%
if(!string.IsNullOrEmpty(errorMessage))
{
errorMessage = errorMessage.Replace("//", "////").Replace("/r", "//r")
.Replace("/n", "//n").Replace("/'", "//'");
%>
var errorMsg = '<%=errorMessage%>';
alert(errorMsg);
<%
} else if(emptySessionId) {
%>
waitingMapReadyInterval = window.setInterval(SetSessionId, 200);
<%
}
%>
}
function SetSessionId(){
if(!parent.parent.GetMapFrame().GetSessionId) return;
window.clearInterval(waitingMapReadyInterval);
var params = new Array('SESSIONID', parent.parent.GetMapFrame().GetSessionId(), 'DATE', new Date());
parent.parent.GetFormFrame().Submit('userareamain.aspx', params, 'taskPaneFrame');
}
function DigitizeUserArea(){
var params = new Array('SESSIONID', parent.parent.GetMapFrame().GetSessionId(), 'DATE', new Date());
parent.parent.GetFormFrame().Submit('digitizeuserarea.aspx', params, 'scriptFrame');
}
function RedigitizeUserArea(){
var params = new Array('SESSIONID', parent.parent.GetMapFrame().GetSessionId(), 'REDIGITIZE', 'true', 'DATE', new Date());
parent.parent.GetFormFrame().Submit('digitizeuserarea.aspx', params, 'scriptFrame');
}
function PanToUserArea(){
var params = new Array('SESSIONID', parent.parent.GetMapFrame().GetSessionId(), 'DATE', new Date());
parent.parent.GetFormFrame().Submit('openuserarea.aspx', params, 'scriptFrame');
}
</script>
<style type="text/css">
.warningtext
{
color: #FF0000;
}
</style>
</head>
<body onload="load()">
<%
if (!emptySessionId && string.IsNullOrEmpty(errorMessage))
{
%>
<div style="text-align:center">
<table style="width:270px;">
<tr style="height:50">
<td>尊敬的用户:<%=username%>,欢迎您登录本页面</td>
</tr>
<tr style="height:50" id="noUserAreaRow">
<td>您还没有设定您的用户区域,您可以:</td>
</tr>
<tr style="height:50" id="digitizeRow">
<td> 点击<a href="javascript:DigitizeUserArea()">这里</a>设定您的用户区域</td>
</tr>
<tr style="height:50" id="digitizeWarningRow">
<td> <span class="warningtext">注意:您需要设定后重新载入本页面来反映最新情况 </span></td>
</tr>
<tr style="height:50" id="hasUserAreaRow">
<td>您已经设定了您的用户区域,区域的坐标为:<br />
X: <%=centerX %><br />
Y: <%=centerY %><br />
您可以:</td>
</tr>
<tr style="height:50" id="panUserAreaRow">
<td> 点击<a href="javascript:PanToUserArea()">这里</a>将地图平移到您设定的用户区域 </td>
</tr>
<tr style="height:50" id="redigitizeUserAreaRow">
<td> 点击<a href="javascript:RedigitizeUserArea()">这里</a>重新设定您的用户区域 </td>
</tr>
<tr style="height:50" id="redigitizeWarningRow">
<td> <span class="warningtext">注意:您需要设定后重新载入本页面来反映最新情况 </span></td>
</tr>
</table>
</div>
<script language="javascript" type="text/javascript">
<%
if (hasUserData)
{
%>
document.getElementById('noUserAreaRow').style.display = 'none';
document.getElementById('digitizeRow').style.display = 'none';
document.getElementById('digitizeWarningRow').style.display = 'none';
<%
}
else
{
%>
document.getElementById('hasUserAreaRow').style.display = 'none';
document.getElementById('panUserAreaRow').style.display = 'none';
document.getElementById('redigitizeUserAreaRow').style.display = 'none';
document.getElementById('redigitizeWarningRow').style.display = 'none';
<%
}
%>
</script>
<%
}
%>
</body>
</html>
<script runat="server">
/// <summary>
/// 获取当前登录的用户名
/// </summary>
/// <param name="sessionId"></param>
/// <returns></returns>
static string GetUsername(string sessionId)
{
MgSiteConnection connection = null;
MgUserInformation userInfo = null;
MgSite site = null;
try
{
connection = new MgSiteConnection();
userInfo = new MgUserInformation(sessionId);
connection.Open(new MgUserInformation(sessionId));
site = connection.GetSite();
return site.GetUserForSession();
}
finally
{
if (site != null) site.Dispose();
if (connection != null) connection.Dispose();
if (userInfo != null) userInfo.Dispose();
}
}
/// <summary>
/// 获取该用户那一行区域信息,如果没有文件,或者该用户没有区域信息,会返回空字符串
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
string FetchUserAreaData(string username)
{
string dataFile = Server.MapPath("data/userArea.dat");
if (!File.Exists(dataFile)) return string.Empty;
string[] lines;
try
{
lines = File.ReadAllLines(dataFile);
}
catch (IOException exc)
{
throw new ArgumentException("无法读取数据文件", exc);
}
foreach (string line in lines)
{
string[] parts = line.Split('|');
if (parts.Length != 0 && parts[0].Equals(username, StringComparison.InvariantCultureIgnoreCase)) return line;
}
return string.Empty;
}
</script>
- 代码2:Hello Viewer示例源代码
- 代码2:Hello Viewer示例源代码(第二部分)
- 2.3 开始动手吧——Hello Viewer示例
- 5、示例2:Hello Node
- Qt示例[Image Viewer Example]
- Kubernetes实践(2)--hello world 示例
- jbpm Hello World 示例
- hello akka入门示例
- Hadoop “Hello World” 示例
- RabbitMQ hello world示例
- Hello world ----------AngularJS示例
- NDK示例:hello-jni
- koa2 hello world 示例
- FFMpeg官网源代码中的示例代码(编码和解码示例)(精华)
- JFace Viewer 2
- 示例程序源代码
- Linux timer 示例代码 2
- tensorflow示例代码注释2
- svn配置中,FSFS与BDB的区别
- 代码1:第二章内置命令示例所用的网页布局文件
- std::string BSTR 互相转化
- 针对TCP协议的攻击与检测、预防方法
- 试论贝叶斯分类、决策树分类分类挖掘算法的优势与劣势,以及解决维度效应的策略
- 代码2:Hello Viewer示例源代码
- MathType--公式使用不再烦恼
- 10折交叉验证
- PPT资源、技巧与设计网站精选【转自paratop】
- 点击图标打开方式引起的桌面图标全部变成一类程序的关联的解决方法
- VC按钮加上图片方法
- Oracle CHAR,VARCHAR,VARCHAR2类型的区别与使用
- 无奈
- C# sqlserver2005 操作