Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. var map;
  2. var iconFeatures = [];
  3. var lineString;
  4. var lineArray = [];
  5. var extent;
  6. var baseLayers = [
  7. new ol.layer.Tile({
  8. source: new ol.source.TileWMS({
  9. url: 'http://localhost/geoserver/India/wms',
  10. params:
  11. {
  12. 'LAYERS': 'India:roads', //,dist_bnd,dist_hq,dist_road,roads
  13. 'TILED': true,
  14. crossOrigin: 'anonymous',
  15. 'isBaselayer': true
  16. },
  17. serverType: 'geoserver'
  18. })
  19. })
  20. ];
  21. var vectorPointSource = new ol.source.Vector({
  22. //create empty vector
  23. });
  24.  
  25. var vectorLineSource = new ol.source.Vector({
  26. });
  27.  
  28. var iconStyle = new ol.style.Style({
  29. image: new ol.style.Icon(/** @type {olx.style.IconOptions} */
  30. ({
  31. anchor: [0.5, 46],
  32. anchorXUnits: 'fraction',
  33. anchorYUnits: 'pixels',
  34. opacity: 0.75,
  35. src: 'images/marker.png'
  36. }))
  37. });
  38. var lineStyle = new ol.style.Style({
  39. stroke: new ol.style.Stroke({
  40. color: '#000000',
  41. width: 2
  42. })
  43. });
  44.  
  45.  
  46. //add the feature vector to the layer vector, and apply a style to whole layer
  47. var pointLayer = new ol.layer.Vector({
  48. source: vectorPointSource,
  49. style: iconStyle
  50. });
  51.  
  52. var lineLayer = new ol.layer.Vector({
  53. source: vectorLineSource,
  54. style: [lineStyle]
  55. });
  56.  
  57. var view = new ol.View({
  58. center: [10497145, 2369099],
  59. zoom: 4
  60. });
  61.  
  62. map = new ol.Map({
  63. controls: ol.control.defaults().extend([
  64. new ol.control.FullScreen(),
  65. new ol.control.ScaleLine()
  66.  
  67. ]),
  68. layers: baseLayers,
  69. target: document.getElementById('map'),
  70. view: view
  71. });
  72. // lineLayer.getSource().updateParams({"time": Date.now()});
  73. map.addLayer(pointLayer);
  74. map.addLayer(lineLayer);
  75. // lineLayer.getSource().updateParams({"time": Date.now()});
  76. function showMap() {
  77.  
  78. $.ajax({
  79. url: 'GetTerminalData',
  80. type: 'post',
  81. dataType: 'json',
  82. success: function(json) {
  83. alert('inside success');
  84.  
  85. $.each(json, function(idx, obj) {
  86.  
  87. var final_lat = +(obj.latitude);
  88. var final_lon = +(obj.longitude);
  89.  
  90.  
  91. lineArray.push({x: final_lon, y: final_lat});
  92. // alert('lineArray[0].x' + lineArray[0].x);
  93.  
  94. // alert("Final_lon : " + final_lon + " Final_lat: " + final_lat);
  95. var iconFeature = new ol.Feature({
  96. geometry: new
  97. ol.geom.Point(ol.proj.transform([final_lon, final_lat], 'EPSG:4326', 'EPSG:3857'))
  98. });
  99. lineArray[idx] = ol.proj.transform([lineArray[idx].x, lineArray[idx].y], 'EPSG:4326', 'EPSG:3857');
  100. var featureLine = new ol.Feature({
  101. geometry: new ol.geom.LineString(lineArray),
  102. time: Date.now()
  103. });
  104.  
  105. // pointLayer.getSource().updateParams({"time": Date.now()});
  106. // lineLayer.clear();
  107. /* var fe = vectorPointSource.getFeatures();
  108. alert('fe count ' + fe.length);
  109. for(i=0;i<fe.length;i++){
  110. vectorPointSource.removeFeatures(fe[i]);
  111. }*/
  112. // alert('fe count ' + fe.length);
  113. // vectorPointSource.removeFeatures();
  114. vectorPointSource.addFeature(iconFeature);
  115. vectorLineSource.addFeature(featureLine);
  116. // lineLayer.refresh({force:true});
  117. // lineLayer.getSource().updateParams({"time": Date.now()});
  118.  
  119. });
  120. extent = pointLayer.getSource().getExtent();
  121. map.getView().fit(extent, map.getSize());
  122.  
  123. }, complete: function() {
  124. setTimeout(showMap, 10000);
  125. }
  126.  
  127. });
  128. }
  129.  
  130. try {
  131.  
  132. ArrayList<LocationBean> list = new ArrayList<LocationBean>();
  133. Connection con = null;
  134. System.out.println("Inside servlet");
  135. Statement s = null;
  136. ResultSet rs = null;
  137. try {
  138. Class.forName("org.gjt.mm.mysql.Driver");
  139. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/VesselTracking", "user3", "shivam");
  140. s = con.createStatement();
  141.  
  142. rs = s.executeQuery("Select terminalId,`timestamp`,ST_Y(Current_Location) as Latitude,ST_X(Current_Location) as Longitude from VesselTracking.Terminal where TerminalId<1006;"); //ST_Y(Current_Location) as Latitude,ST_X(Current_Location) as Longitude
  143. while (rs.next()) {
  144.  
  145. LocationBean lb = new LocationBean(rs.getInt(1), rs.getTimestamp(2), rs.getFloat(3),rs.getFloat(4));
  146.  
  147.  
  148. list.add(lb);
  149.  
  150.  
  151. }
  152. rs.close();
  153. s.close();
  154. con.close();
  155. Gson gson = new Gson();
  156.  
  157. String json = gson.toJson(list);
  158.  
  159. System.out.println(json);
  160. response.setContentType("application/json");
  161. response.getWriter().write(json);
  162.  
  163. } catch (Exception e) {
  164. e.printStackTrace();
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement