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

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

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

1、jquery调取查询并处理json

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

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

 1 {
 2     "id25" : {
 3         "XMJSNR" : "30万千瓦",
 4         "ZRDW" : "xx市政府",
 5         "XMMC" : "海上风电场H2",
 6         "PROJECT_ID" : "ddafb1a4-668a-4b0c-85f0-081d90778e82",
 7         "LONGITUDE" : "125.22222",
 8         "LATITUDE" : "33.305264"
 9     },
10     "id24" : {
11         "XMJSNR" : "年产海洋生物营养保健品系列2万吨、抗癌系列新药1万吨",
12         "ZRDW" : "xx市政府",
13         "XMMC" : "海洋生物营养保健品、抗癌新药项目",
14         "PROJECT_ID" : "d26da381-e7ca-4ab7-8c9c-d09c2b242c27",
15         "LONGITUDE" : "124.55555",
16         "LATITUDE" : "33.30223"
17     }
18 }

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

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

2、getVIPStatus.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <%@ page import="com.marost.tool.*,java.sql.*" %> 
 3 <%@ page import="java.util.Map,java.util.HashMap" %>
 4 <%@ page import="org.json.JSONException,org.json.JSONObject" %>
 5 <%
 6 JDBConnection connection = new JDBConnection();
 7 String vipstatus = Chinese.toChinese(request.getParameter("vipstatus"));
 8 //System.out.println(vipname);
 9 JSONObject jsonObj = new JSONObject(); 
10 
11 String sql = "SELECT " +
12         "T_PROJECT_INFO.PROJECT_ID," +
13         "T_PROJECT_INFO.XMMC," +
14         "T_PROJECT_LOCATION.LONGITUDE,"+
15         "T_PROJECT_LOCATION.LATITUDE," +
16         "T_PROJECT_INFO.XMJSNR," +
17         "T_PROJECT_INFO.ZRDW " +
18         "FROM T_PROJECT_INFO ,T_PROJECT_LOCATION " +
19         "WHERE T_PROJECT_INFO.XMZT = (select code from t_code where code_name='"+vipstatus+"' and SECTION_NAME='项目状态') " +
20         "AND T_PROJECT_INFO.PROJECT_ID = T_PROJECT_LOCATION.PROJECT_ID";
21 System.out.println(sql);
22 try {
23     ResultSet rs = connection.executeQuery(sql);
24     while (rs.next()) {
25           Map <String, String> ingredients = new HashMap <String, String>(); 
26         ingredients.put("PROJECT_ID", rs.getString(1)); 
27         ingredients.put("name", rs.getString(2)); 
28         ingredients.put("x", rs.getString(3)); 
29         ingredients.put("y", rs.getString(4));
30         ingredients.put("XMJSNR", rs.getString(5)); 
31         ingredients.put("ZRDW", rs.getString(6)); 
32         ingredients.put("img","images/map_pin_fill_20.png");
33         try {
34             jsonObj.put("id"+rs.getRow(), ingredients);
35         } catch (JSONException e) {
36             // TODO Auto-generated catch block
37             e.printStackTrace();
38         }
39         //System.out.print(deptno+"\t"+dname+"\t"+loc);
40         //System.out.println(rs.getRow());
41     }
42     //System.out.println(jsonObj);
43 } catch (SQLException e) {
44     e.printStackTrace();
45 }
46 connection.closeConnection();
47 response.getWriter().print(jsonObj);
48 %>

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

posted @ 2014-04-17 15:51  marost  阅读(725)  评论(0编辑  收藏  举报