地图项目的随笔(一)

 

1.在地图控件中,用c#调用.HTML文件中js的方法

   a.想用Describ调用IndexMap里的js里面Describ_Point方法,但是Describ是在主页面中btn 出来的,

传值也很重要。

 

 

    

 

 

 

b.现在Main.cs里声明Describ.cs文件和一些需要的变量

 

 

 

c.在Describ.cs中编写如下

Main.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
using System.Collections;

namespace winMap
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]  
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]   

    public partial class Main_F : Form
    {
        private string startX = string.Empty;
        private string startY = string.Empty;
        private string azimuth = string.Empty;
        private string count = string.Empty;
        private string distance = string.Empty;
        private Describ_F desceib_F;

        public Main_F()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            string str_url = Application.StartupPath + "\\IndexMap.html";  
             Uri url = new Uri(str_url);  
             webBrowser1.Url = url;  
             webBrowser1.ObjectForScripting = this;
            
        }

        private void add_control_Click(object sender, EventArgs e)
        {
            invokeControl("add_control"); 
        }

        private void delete_control_Click(object sender, EventArgs e)
        {
            invokeControl("delete_control");
        }

        private void open_GetDistance_Click(object sender, EventArgs e)
        {
            invokeControl("open_GetDistance");
        }

        private void Descript_Click(object sender, EventArgs e)
        {
            if (desceib_F == null)
            {
                desceib_F = new Describ_F();
            }
            desceib_F.ShowDialog();
            startX = desceib_F.startX;
            startY = desceib_F.startY;
            azimuth = desceib_F.azimuth;
            count = desceib_F.count;
            distance = desceib_F.distance;
            this.invokeControl("Describ_Point", new string[] { startX, startY, azimuth, count, distance });
        }
        public void invokeControl(String controlType, object[] args = null)
        {
            if (args != null && args.Length > 0)
            {
                webBrowser1.Document.InvokeScript(controlType, args);
            }
            else
            {
                webBrowser1.Document.InvokeScript(controlType);
            }

        }
        
   
      
    }
}

Describ.cs代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace winMap
{
    public partial class Describ_F : Form
    {
        
        private static int checkBoxCount = 1;

        private static int InitLocation = 13;

        private static int LocationAdd = 22;

        private int Location = 13;

        public string startX = string.Empty;
        public string startY = string.Empty;
        public string azimuth = string.Empty;
        public string count = string.Empty;
        public string distance = string.Empty;
        public Describ_F()
        {
            InitializeComponent();
        }

        private void Describ_F_Load(object sender, EventArgs e)
        {
      
      
        }
        private void View_Btn_Click(object sender, EventArgs e)
        {
            startX = this.StartPointlng_Text.Text;
            startY = this.StartPointlat_Text.Text;
            azimuth = this.Azimuth_Text.Text;
            count = this.Count_Text.Text;
            distance = this.Count_Text.Text;
            this.Hide();
        }

       

        private void Y_Btn_Click(object sender, EventArgs e)
        {
           // Describ_dataGridView.Rows.Add(this.Line_TextB.Text, this.StartPointlng_Text.Text, this.StartPointlat_Text.Text, this.Azimuth_Text.Text, this.Distance_Text.Text, this.Count_Text.Text);
            
            string checkText = this.Line_TextB.Text;
            if (HasCheckBox(checkText))
            {
                MessageBox.Show("此线号以存在。");
                return;
            }
            CheckBox newCheckBox = new CheckBox();
            newCheckBox.Text = checkText;
            DrawingCheckBox(newCheckBox);
            this.splitContainer1.Panel1.Controls.Add(newCheckBox);
            MessageBox.Show(newCheckBox.Location.ToString());

        }


        private bool HasCheckBox(string newText)
        {
            bool isExisted = false;

            foreach(Control control in this.splitContainer1.Panel1.Controls)//如果前面有值
            {
                if (control.Text.Equals(newText))
                {
                    isExisted = true;
                    break;
                }
            }


            return isExisted;
        }


        private void DrawingCheckBox(CheckBox checkbox)
        {
            
            if (checkBoxCount > 1)
            {
                Location = Location + LocationAdd;  
            }
            else 
            {
                Location = InitLocation;
                checkBoxCount++;
            }
            checkbox.Location = new System.Drawing.Point(14, Location);
            checkbox.AutoSize = true;
            checkbox.Size = new System.Drawing.Size(78, 16);
            checkbox.UseVisualStyleBackColor = true;
        }

        private void Describ_dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)//设置DataGridView行号
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                 e.RowBounds.Location.Y,
                 Describ_dataGridView.RowHeadersWidth - 4,
                 e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                Describ_dataGridView.RowHeadersDefaultCellStyle.Font,
                rectangle,
                Describ_dataGridView.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void Describ_dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            


        }

        private void Line_TextB_TextChanged(object sender, EventArgs e)
        {

        }
        

        

       
        

        
    }
}

IndexMap.html代码

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">    
            body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
        #allmap{width:100%;height:100%;}
        p{margin-left:5px; font-size:14px;}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=p9gxU4nY9XajuGDNGsktcGET"></script>
    <!--加载鼠标绘制工具-->
    <script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
    <link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />
    <!--加载检索信息窗口-->
    <script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>
    <link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css" />
   <script type="text/javascript" src="http://api.map.baidu.com/library/DistanceTool/1.2/src/DistanceTool_min.js"></script>
    <title></title>
</head>
<body>
    <div id="allmap"  style="overflow:hidden;zoom:1;position:relative;">    
        <div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;"></div>
    </div>
    <div id="result">
        <input type="button" value="获取绘制的覆盖物个数" onclick="alert(overlays.length)"/>
        <input type="button" value="清除所有覆盖物" onclick="clearAll()"/>
    </div>

    <script type="text/javascript">
    // 百度地图API功能
    var map = new BMap.Map('map');// 创建Map实例
    var poi = new BMap.Point(116.404 , 39.915);// 初始化地图设置中心点坐标
    map.centerAndZoom(poi, 15);//设置地图级别
    map.enableScrollWheelZoom();  //开启鼠标滚轮缩放
    
    //鼠标绘制点线面
     
    var overlays = [];
    var overlaycomplete = function(e){
        overlays.push(e.overlay);
    };
    var styleOptions = {
        strokeColor:"red",    //边线颜色。
        fillColor:"red",      //填充颜色。当参数为空时,圆形将没有填充效果。
        strokeWeight: 3,       //边线的宽度,以像素为单位。
        strokeOpacity: 0.8,       //边线透明度,取值范围0 - 1。
        fillOpacity: 0.6,      //填充的透明度,取值范围0 - 1。
        strokeStyle: 'solid' //边线的样式,solid或dashed。
    }
    //实例化鼠标绘制工具
    var drawingManager = new BMapLib.DrawingManager(map, {
        isOpen: false, //是否开启绘制模式
        enableDrawingTool: true, //是否显示工具栏
        drawingToolOptions: {
            anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
            offset: new BMap.Size(5, 5) //偏离值
        },
        circleOptions: styleOptions, //圆的样式
        polylineOptions: styleOptions, //线的样式
        polygonOptions: styleOptions, //多边形的样式
        rectangleOptions: styleOptions //矩形的样式
    }); 
     
     //添加鼠标绘制工具监听事件,用于获取绘制结果
    drawingManager.addEventListener('overlaycomplete', overlaycomplete);
    function clearAll() {
        for(var i = 0; i < overlays.length; i++){
            map.removeOverlay(overlays[i]);
        }
        overlays.length = 0   
    }

    //比例尺工具

    var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角,添加比例尺
    var top_left_navigation = new BMap.NavigationControl();  //左上角,添加默认缩放平移控件
    var top_right_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}); //右上角,仅包含平移和缩放按钮
    /*缩放控件type有四种类型:
    BMAP_NAVIGATION_CONTROL_SMALL:仅包含平移和缩放按钮;BMAP_NAVIGATION_CONTROL_PAN:仅包含平移按钮;BMAP_NAVIGATION_CONTROL_ZOOM:仅包含缩放按钮*/
    
    //添加控件和比例尺
    function add_control(){
        map.addControl(top_left_control);        
        map.addControl(top_left_navigation);     
        map.addControl(top_right_navigation);    
    }

    //移除控件和比例尺
    function delete_control(){
        map.removeControl(top_left_control);     
        map.removeControl(top_left_navigation);  
        map.removeControl(top_right_navigation); 
    }
          
     //鼠标测距
     function open_GetDistance() {  
         var myDis=new BMapLib.DistanceTool(map);//map为上面已经初始化好的地图实例  
         myDis.open();
    }
            
    function Describ_Point(startX, startY, azimuth, count,distance){
         startX=startX*1.000000;
         startY=startY*1.000000;
         var startPoint = new BMap.Point(startX , startY);  // 创建点坐标
         var startMarker = new BMap.Marker(startPoint);  // 创建标注
         map.addOverlay(startMarker);               // 将标注添加到地图中
         distance=distance*0.000008993216920097318;  //距离比例
         var endX;
         var endY;
         var i=0;
         var endPoint;
         var distanceT=0

         for(i=1;i<=count;i++){
             distanceT=distanceT+distance;
             endX=startX+Math.sin(azimuth)*distanceT;
             endY=startY+Math.cos(azimuth)*distanceT;
             var endPoint=new BMap.Point(endX , endY); 
             var endMarker = new BMap.Marker(endPoint);  // 创建标注
             map.addOverlay(endMarker);  
             var polyline = new BMap.Polyline([ startPoint,endPoint], {strokeColor:"red", strokeWeight:15, strokeOpacity:0.5});   //创建折线
             map.addOverlay(polyline);   //增加折线
            // var dis=(map.getDistance(startPoint,endPoint)).toFixed(6); //获取两点距离,保留小数点后两位  
            // alert(dis);
             
             startPoint=endPoint;
      }

}
</script>
</body>
</html>

 

posted @ 2015-09-18 17:15  JaneAi  阅读(289)  评论(0编辑  收藏  举报