C#中Winform操作百度地图-创新互联

这里的博客实在是太不好写了,这么用户不友好的工具竟然来源于一个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>

  • {margin:0px;padding:0px;}
    </style>

    </head>

    <body>
    <div style="width:1120px;height:433px;border:#ccc solid 1px;font-size:12px" id="map"></div>
    <div id="lng" style="display:none"></div>
    <div id="lat" style="display:none"></div>
    </body>
    <script type="text/javascript">
    //创建和初始化地图函数:
    function initMap(){
    createMap();//创建地图
    setMapEvent();//设置地图事件
    addMapControl();//向地图添加控件
    addMapOverlay();//向地图添加覆盖物
    }
    function createMap(){
    map = new BMap.Map("map");
    map.centerAndZoom(new BMap.Point(87.307622,43.996322),16);
    }
    function setMapEvent(){
    map.enableScrollWheelZoom();
    map.enableKeyboard();
    map.enableDragging();
    map.enableDoubleClickZoom()
    }
    function addClickHandler(target,window){
    target.addEventListener("click",function(){
    target.openInfoWindow(window);
    });
    }
    function addMapOverlay(){
    var labels = [
    {position:{lng:87.306616,lat:43.994921},content:"YQCY Company"}
    ];
    for(var index = 0; index < labels.length; index++){
    var opt = { position: new BMap.Point(labels[index].position.lng,labels[index].position.lat )};
    var label = new BMap.Label(labels[index].content,opt);
    map.addOverlay(label);
    };
    }
    //向地图添加控件
    function addMapControl(){
    var scaleControl = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
    scaleControl.setUnit(BMAP_UNIT_IMPERIAL);
    map.addControl(scaleControl);
    var navControl = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE});
    map.addControl(navControl);
    var overviewControl = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:true});
    map.addControl(overviewControl);
    }

    var map;
    initMap();

    var mapType1 = new BMap.MapTypeControl(
    {
    mapTypes: [BMAP_NORMAL_MAP,BMAP_HYBRID_MAP],
    anchor: BMAP_ANCHOR_TOP_LEFT
    }
    );

    var overView = new BMap.OverviewMapControl();
    var overViewOpen = new BMap.OverviewMapControl({isOpen:true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT});
    //添加地图类型和缩略图
    function add_control(){
    map.addControl(mapType1); //2D图,混合图
    map.addControl(overView); //添加默认缩略地图控件
    map.addControl(overViewOpen); //右下角,打开
    }
    //移除地图类型和缩略图
    function delete_control(){
    map.removeControl(mapType1); //移除2D图,混合图
    map.removeControl(overView);
    map.removeControl(overViewOpen);
    }

    map.addEventListener("click",function(e){
    document.getElementById("lng").innerText=e.point.lng;
    document.getElementById("lat").innerText=e.point.lat;
    window.external.WinFormGetCurrentLngAndLat();
    });

    map.addEventListener("mousemove",function(e){
    if(e.point.lng!=null){
    document.getElementById("lng").innerText=e.point.lng;
    document.getElementById("lat").innerText=e.point.lat;
    //调用Winform函数
    window.external.WinFormGetCurrentLngAndLat();

    }

    });

    </script>
    </html>

在窗体中放入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与浏览器就可以交互了,就可以做进一步的应用了。

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。

当前名称:C#中Winform操作百度地图-创新互联
本文网址:https://www.cdcxhl.com/article30/dpscpo.html

成都网站建设公司_创新互联,为您提供Google小程序开发营销型网站建设网页设计公司App开发企业网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设