C#中Winform操作百度地图

发布时间:2020-07-07 19:56:03 作者:dawn0919
来源:网络 阅读:549

  这里的博客实在是太不好写了,这么用户不友好的工具竟然来源于一个IT的专业网站,不可思议啊。
  后面要做一个和地图相关的应用,先做一些准备,今天申请了百度开发认证,得到一个地图的AK,可以应用百度地图了。
  显示地图比较容易,直接得到AK时,可以设置后得到百度地图的HTML,在Winform中嵌入就行了。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="百度地图,百度地图API,百度地图自定义工具,百度地图所见即所得工具" />
<meta name="description" content="百度地图API自定义地图,帮助用户在可视化操作下生成百度地图" />
<title>百度地图的学习应用</title>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=百度的AK"></script>
<style type=text/css>

  在窗体中放入WebBrowse控件,剩下的问题就是Winform怎样与这个控件交互了。

  我只做了一个简单的应用,比如获取实时的经纬度,如果这个解决了,其他的都应该可以。

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.Security.Permissions;
using System.Runtime.InteropServices.ComTypes;

namespace 百度地图应用
{
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
[PermissionSet(SecurityAction.Demand,Name ="FullTrust")]

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string sURL = "服务器的网址";
        Uri url = new Uri(sURL);
        webBrowser1.Url = url;
        //屏蔽浏览器的右键菜单
        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        //禁止弹出脚本错误的对话框
        webBrowser1.ScriptErrorsSuppressed = true;
        //为了与Windows Form交互,设置可以调用js方法
        webBrowser1.ObjectForScripting = this;
    }

    public void WinFormGetCurrentLngAndLat()
    {
        //获取当前百度地图的经纬度坐标
        string sLng = webBrowser1.Document.GetElementById("lng").InnerText;//经度
        string sLat = webBrowser1.Document.GetElementById("lat").InnerText;//纬度
        this.toolStripStatusLabel1.Text = "经度:"+sLng+","+"纬度:"+sLat;
    }

    private void button5_Click(object sender, EventArgs e)
    {
        WinFormGetCurrentLngAndLat();
    }

}

}

  要总结的是,Winform要调用浏览器的功能(一般是函数),在网页写好函数,Winform直接调用即可。
  比如: webBrowser1.Document.InvokeScript("add_control");
  网页调用Winform的方法(一般是函数),在Winform中写好,网页里直接调用。
  比如:window.external.WinFormGetCurrentLngAndLat();
  这样WinForm与浏览器就可以交互了,就可以做进一步的应用了。

推荐阅读:
  1. 在WinForm中打印DataGridView操作代码
  2. C#中WinForm如何制作异形窗体与控件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

百度地图 fo

上一篇:mongo简单操作

下一篇:C值循环语句(七)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》