ajax------Json
后台代码:
String result = "";
List<DeviceSensor> devices = deviceSensorService.findAll();
JSONArray array = new JSONArray();
for (DeviceSensor deviceSensor : devices) {
try {
String apMac = deviceSensor.getApMac();
if(apMac == null) continue;
DeviceAp deviceAp = deviceApService.findByAddress(apMac);
Map<String,Object> map = new HashMap<String, Object>();
map.put("apmac", apMac);
map.put("apx", deviceAp.getX());
map.put("apy", deviceAp.getY());
map.put("sensormac", deviceSensor.getMac());
map.put("sensorx", deviceSensor.getX());
map.put("sensory", deviceSensor.getY());
array.add(map);
} catch (Exception e) {
}
}
result = array.toString();
try {
response.setContentType("application/json;charset-utf-8");
response.setContentLength(result.length());
response.setCharacterEncoding("utf-8");
response.getWriter().write(result);
response.getWriter().flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
返回Json数据格式

js代码:
refresh();
window.markers = [];
window.markerList = new Array();
window.label = new Array();
function refresh(){
$.ajax({
type : "GET",
dataType : "json",
url : "${ctx}/link/ajax",
success : function(data) {
var arr=eval(data);
for(var i = 0;i<arr.length;i++){
if(arr[i].apy == null || arr[i].apx == null) continue;
var station = new BMap.Point(arr[i].apx,arr[i].apy); // 创建标注
markerList[i] = new BMap.Marker(new BMap.Point(arr[i].sensorx,arr[i].sensory)); // 创建标注
map.addOverlay(markerList[i]);
markerList[i].setTitle(arr[i].sensormac);
markers.push(markerList[i]);
var points = [station,new BMap.Point(arr[i].sensorx,arr[i].sensory)];
var curve = new BMapLib.CurveLine(points, {strokeColor:"blue", strokeWeight:3, strokeOpacity:0.5}); //创建弧线对象
map.addOverlay(curve); //添加到地图中
curve.enableEditing(); //开启编辑功能
/*alert("apmac:"+arr[i].apmac+"\n"
+"apx:"+arr[i].apx+"\n"
+"apy:"+arr[i].apy+"\n"
+"sensormac:"+arr[i].sensormac+"\n"
+"sensorx:"+arr[i].sensorx+"\n"
+"sensory:"+arr[i].sensory+"\n")*/
}
cluster();
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
http://www.cnblogs.com/makexu/

浙公网安备 33010602011771号