MapView.cshtml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. @{
  2. ViewBag.Title = "管网地图";
  3. Layout = "~/Views/Shared/_LayoutIndex.cshtml";
  4. }
  5. <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=@System.Configuration.ConfigurationManager.AppSettings["BaiduMapAK"]"></script>
  6. <script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
  7. <link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />
  8. <style type="text/css">
  9. label {
  10. max-width:unset;
  11. }
  12. </style>
  13. <script type="text/javascript">
  14. $(document).ready(function () {
  15. initBaiduMap("@System.Configuration.ConfigurationManager.AppSettings["LngAndLat"].Split('|')[0]", "@System.Configuration.ConfigurationManager.AppSettings["LngAndLat"].Split('|')[1]",@System.Configuration.ConfigurationManager.AppSettings["MapZoon"]);
  16. //地图覆盖物 绘制
  17. //1、管线绘制
  18. drawPipeLine(@Html.Raw(ViewBag.PipeLineCoordinates));
  19. drawMeterPoint(@Html.Raw(ViewBag.MeterCoordinates));
  20. })
  21. // 初始化百度地图
  22. function initBaiduMap(lat, lgt, zoom) {
  23. map = new BMap.Map("allmap");
  24. var geoc = new BMap.Geocoder(); //地址解析对象
  25. var point = new BMap.Point(lat, lgt);
  26. map.centerAndZoom(point, zoom); // 中心点
  27. map.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
  28. var bottom_left_control = new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT });// 左上角,添加比例尺
  29. map.addControl(bottom_left_control);
  30. map.addControl(new BMap.MapTypeControl({
  31. mapTypes: [
  32. BMAP_NORMAL_MAP,
  33. BMAP_HYBRID_MAP
  34. //BMAP_PERSPECTIVE_MAP
  35. ],
  36. anchor: BMAP_ANCHOR_TOP_LEFT
  37. }));
  38. }
  39. function drawPipeLine(datas){
  40. for(var i=0;i<datas.length;i++){
  41. var Coordinates = datas[i]["Coordinates"];//管线坐标
  42. if (Coordinates.length > 0) {
  43. var points = Coordinates.split("|");
  44. var paths = [];
  45. for (var j = 0 ; j < points.length; j++) {
  46. paths.push(new BMap.Point(points[j].split(",")[0], points[j].split(",")[1]));
  47. }
  48. var polyLine = new BMap.Polyline(paths, { strokeColor: "#FF0000", strokeWeight: "2", strokeOpacity: 1, strokeStyle: "solid" });
  49. map.addOverlay(polyLine);
  50. }
  51. }
  52. }
  53. function drawMeterPoint(datas){
  54. for(var i=0;i<datas.length;i++){
  55. var points = datas[i]["LngAndLat"]==null?"":datas[i]["LngAndLat"];
  56. if(points.length>0){
  57. var lng = points.split('|')[0];
  58. var lat = points.split('|')[1];
  59. var point = new BMap.Point(lng, lat);
  60. var getDateTime = datas[i]["GetDateTime"];
  61. var icon = "" ;
  62. if(contrastTime(getDateTime)>=1){
  63. icon ="/Content/images/大圈灰.gif";
  64. }else{
  65. icon ="/Content/images/大圈绿.gif";
  66. }
  67. var myIcon = new BMap.Icon(icon, new BMap.Size(15, 15));
  68. myIcon.setImageSize(new BMap.Size(15, 15))
  69. // 创建标注对象并添加到地图
  70. var marker = new BMap.Marker(point, { icon: myIcon });
  71. map.addOverlay(marker);
  72. var label = new BMap.Label(datas[i]["MeterAssessmentName"], { "offset": new BMap.Size(20, -10) });
  73. marker.setLabel(label);
  74. label.setStyle({
  75. borderColor: "#000",
  76. color: "#000",
  77. cursor: "pointer",
  78. backgroundColor: "#aed0ea",
  79. opacity: 0.7
  80. });
  81. }
  82. }
  83. }
  84. function contrastTime(start) {
  85. start=Date.parse(start); //开始时间
  86. var now=Date.parse(new Date()); //结束时间
  87. var date3=now-start; //时间差的毫秒数
  88. //计算出相差天数
  89. return Math.floor(date3/(24*3600*1000))
  90. }
  91. </script>
  92. <div id='allmap' style='width: 100%; height: 100%;'></div>