ArcGIS Javascript查询数据库并添加到地图上

将数据存放到数据库中,动态的调取比较灵活,数据变动后不需要改变图层的属性表。

此处采用的方法是通过jquery查询数据库,并将数据库的结果生产json串返回给js,在js中动态解析json串增加点至地图上

1、jquery调取查询并处理json

 var data = [];
function getData(vipstatus){
data=[];//清空数据列表
$.get('getVIPStatus.jsp',{vipstatus:vipstatus},function(result){
//alert(result);
var obj = JSON.parse(result);//将传递过来的json串转换成json对象
$.each(obj, function(index){
data.push(this);
});
displayPoints(data);//加载点到地图上,参见另外一篇文章
});
}

说明6-9行需要对返回的json传进行一个处理,因为传过来的数据格式为

 {
"id25" : {
"XMJSNR" : "30万千瓦",
"ZRDW" : "xx市*",
"XMMC" : "海上风电场H2",
"PROJECT_ID" : "ddafb1a4-668a-4b0c-85f0-081d90778e82",
"LONGITUDE" : "125.22222",
"LATITUDE" : "33.305264"
},
"id24" : {
"XMJSNR" : "年产海洋生物营养保健品系列2万吨、抗癌系列新药1万吨",
"ZRDW" : "xx市*",
"XMMC" : "海洋生物营养保健品、抗癌新药项目",
"PROJECT_ID" : "d26da381-e7ca-4ab7-8c9c-d09c2b242c27",
"LONGITUDE" : "124.55555",
"LATITUDE" : "33.30223"
}
}

而实际需要的是个json数组,如下:(此处应该可以改进,需要继续优化)

 [{"name":"name1","x":"x1","y":"y1","img":"img1","content":"content1"}...]

2、getVIPStatus.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.marost.tool.*,java.sql.*" %>
<%@ page import="java.util.Map,java.util.HashMap" %>
<%@ page import="org.json.JSONException,org.json.JSONObject" %>
<%
JDBConnection connection = new JDBConnection();
String vipstatus = Chinese.toChinese(request.getParameter("vipstatus"));
//System.out.println(vipname);
JSONObject jsonObj = new JSONObject(); String sql = "SELECT " +
"T_PROJECT_INFO.PROJECT_ID," +
"T_PROJECT_INFO.XMMC," +
"T_PROJECT_LOCATION.LONGITUDE,"+
"T_PROJECT_LOCATION.LATITUDE," +
"T_PROJECT_INFO.XMJSNR," +
"T_PROJECT_INFO.ZRDW " +
"FROM T_PROJECT_INFO ,T_PROJECT_LOCATION " +
"WHERE T_PROJECT_INFO.XMZT = (select code from t_code where code_name='"+vipstatus+"' and SECTION_NAME='项目状态') " +
"AND T_PROJECT_INFO.PROJECT_ID = T_PROJECT_LOCATION.PROJECT_ID";
System.out.println(sql);
try {
ResultSet rs = connection.executeQuery(sql);
while (rs.next()) {
Map <String, String> ingredients = new HashMap <String, String>();
ingredients.put("PROJECT_ID", rs.getString(1));
ingredients.put("name", rs.getString(2));
ingredients.put("x", rs.getString(3));
ingredients.put("y", rs.getString(4));
ingredients.put("XMJSNR", rs.getString(5));
ingredients.put("ZRDW", rs.getString(6));
ingredients.put("img","images/map_pin_fill_20.png");
try {
jsonObj.put("id"+rs.getRow(), ingredients);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.print(deptno+"\t"+dname+"\t"+loc);
//System.out.println(rs.getRow());
}
//System.out.println(jsonObj);
} catch (SQLException e) {
e.printStackTrace();
}
connection.closeConnection();
response.getWriter().print(jsonObj);
%>

说明:生成json时用到org.json.jar

上一篇:tp框架where条件查询数据库


下一篇:PHP之ThinkPHP框架(数据库)