栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

使用C#调用百度地图并实现坐标点的设置以及读取示例

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用C#调用百度地图并实现坐标点的设置以及读取示例

申请百度地图密钥以及查看百度API

网址:http://lbsyun.baidu.com/apiconsole/key#/home


网址:http://lbsyun.baidu.com/jsdemo.htm#c1_3

程序实现功能:

1、输入网址那可以调用本地的html文件,也可以访问其他网站
2、输入坐标、添加坐标按钮,可以将坐标值传入html文件中,显示在经纬度的文本框中
3、定位按钮可以将地图重新定位,定位中心是文本框内的经纬度
4、添加标注点是将文本框内的经纬度添加坐标到地图
5、删除标注按钮可以删除全部标注点
6、鼠标点击地图,可以在文本框内显示点击的坐标经纬度
7、点击开始实时显示按钮,鼠标在地图上移动,可以获得实时经纬度

最终图

利用webBrowser控件展示地图

VS创建工程,添加控件webBrowser,新建.html文件,.html文件参考百度API,将其写入文件

为了能与JS交互,首先引入using System.Security.Permissions;,然后在namespace下必须加入两行:

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

给窗体一个Load事件、、、这个是功能的主要点
然后窗体运行的代码:

 private void Form1_Load(object sender, EventArgs e)
    {
      try
      {
 //string str_url = Application.StartupPath + "../HTMLPage1.html";// 添加自己添加的html文件名,注意使用相对路径的方法 HTMLPage1.html要复制到debug目录下
 string str_url = "C:/Users/12606/Desktop/C#/map/map/HTMLPage1.html";// 添加自己添加的html文件名,注意使用相对路径的方法 HTMLPage1.html要复制到debug目录下
 Uri url = new Uri(str_url);
 webBrowser1.Url = url;     // WebBrowser控件显示的网页路径
 webBrowser1.ObjectForscripting = this;  // 将当前类设置为可由脚本访问

 textBox1.Text = str_url;

      }
      catch (Exception ex)
      {
 MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }

.html文件




  
  
  
    body, html{
      width: 100%;
      height: 100%;
      overflow: hidden;
      margin: 0;
      font-family: "微软雅黑";
    }
    #allmap {
      height: 97%;
      width: 100%;
    }

    #r-result {
      width: 100%;
      font-size: 14px;
    }
  
  
  地图展示


  
    
    经度: 
    纬度: 
    
    
    
    
  
  

  0
  0



http://lbsyun.baidu.com/jsdemo.htm#c1_3
百度官方文档给了很多Demo,可根据需求来写

Form1.cs完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Permissions;
using System.IO;


namespace map
{
	// 而为了能与JS交互,首先引入using System.Security.Permissions;,然后在namespace下必须加入两行:
  [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]//调用JS代码必要
  [System.Runtime.InteropServices.ComVisibleAttribute(true)]
  
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      try
      {
 //string str_url = Application.StartupPath + "../HTMLPage1.html";// 添加自己添加的html文件名,注意使用相对路径的方法 HTMLPage1.html要复制到debug目录下
 string str_url = "C:/Users/12606/Desktop/C#/map/map/HTMLPage1.html";// 添加自己添加的html文件名,注意使用相对路径的方法 HTMLPage1.html要复制到debug目录下
 Uri url = new Uri(str_url);
 webBrowser1.Url = url;     // WebBrowser控件显示的网页路径
 webBrowser1.ObjectForscripting = this;  // 将当前类设置为可由脚本访问

 textBox1.Text = str_url;
 

      }
      catch (Exception ex)
      {
 MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
    }

    private void button1_Click(object sender, EventArgs e)
    {
      //本地文件 MapWinFormsbinDebug 
      //string url = Application.StartupPath + "\HTMLPage1.html";
      //string url = "C:/Users/12606/Desktop/C#/map/map/HTMLPage1.html";
      //textBox1.Text = url;
      string url = textBox1.Text.ToString();

      //屏蔽js相关错误 
      webBrowser1.scriptErrorsSuppressed = true;

      //导航显示本地HTML文件 
      webBrowser1.Navigate(url);


    }

    private void timer1_Tick(object sender, EventArgs e)
    {
      try
      {
 string tag_lng = webBrowser1.document.GetElementById("mouselng").InnerText;
 string tag_lat = webBrowser1.document.GetElementById("mouselat").InnerText;
 double dou_lng, dou_lat;
 if (double.TryParse(tag_lng, out dou_lng) && double.TryParse(tag_lat, out dou_lat))
 {
   label2.Text = "当前坐标:" + dou_lng.ToString("F6") + "," + dou_lat.ToString("F6");//保留小数点后6位
 }
      }
      catch (Exception ee)
      { MessageBox.Show(ee.Message); }
    }

    private void btnGetLocation_Click(object sender, EventArgs e)
    {
      if (btnGetLocation.Text == "开启实时坐标")
      {
 timer1.Enabled = true;
 btnGetLocation.Text = "关闭实时坐标";
      }
      else
      {
 btnGetLocation.Text = "开启实时坐标";
 timer1.Enabled = false;
      }

    }


    private void btnGPS_Click(object sender, EventArgs e)
    {
      webBrowser1.document.GetElementById("longitude").InnerText = textBox_longitude.Text;
      webBrowser1.document.GetElementById("latitude").InnerText = textBox_latitude.Text;
    }


  }
}

源代码

到此这篇关于使用C#调用百度地图并实现坐标点的设置以及读取示例的文章就介绍到这了,更多相关C#百度地图坐标点读取内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/121943.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号