C#中Winform操作百度地圖

這里的博客實(shí)在是太不好寫了,這么用戶不友好的工具竟然來源于一個(gè)IT的專業(yè)網(wǎng)站,不可思議啊。
后面要做一個(gè)和地圖相關(guān)的應(yīng)用,先做一些準(zhǔn)備,今天申請了百度開發(fā)認(rèn)證,得到一個(gè)地圖的AK,可以應(yīng)用百度地圖了。
顯示地圖比較容易,直接得到AK時(shí),可以設(shè)置后得到百度地圖的HTML,在Winform中嵌入就行了。

創(chuàng)新互聯(lián) - 溫江服務(wù)器托管,四川服務(wù)器租用,成都服務(wù)器租用,四川網(wǎng)通托管,綿陽服務(wù)器托管,德陽服務(wù)器托管,遂寧服務(wù)器托管,綿陽服務(wù)器托管,四川云主機(jī),成都云主機(jī),西南云主機(jī),溫江服務(wù)器托管,西南服務(wù)器托管,四川/成都大帶寬,服務(wù)器機(jī)柜,四川老牌IDC服務(wù)商

<!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>百度地圖的學(xué)習(xí)應(yīng)用</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">
    //創(chuàng)建和初始化地圖函數(shù):
    function initMap(){
    createMap();//創(chuàng)建地圖
    setMapEvent();//設(shè)置地圖事件
    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); //添加默認(rèn)縮略地圖控件
    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;
    //調(diào)用Winform函數(shù)
    window.external.WinFormGetCurrentLngAndLat();

    }

    });

    </script>
    </html>

在窗體中放入WebBrowse控件,剩下的問題就是Winform怎樣與這個(gè)控件交互了。

我只做了一個(gè)簡單的應(yīng)用,比如獲取實(shí)時(shí)的經(jīng)緯度,如果這個(gè)解決了,其他的都應(yīng)該可以。

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 百度地圖應(yīng)用
{
[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 = "

}

要總結(jié)的是,Winform要調(diào)用瀏覽器的功能(一般是函數(shù)),在網(wǎng)頁寫好函數(shù),Winform直接調(diào)用即可。
比如:?webBrowser1.Document.InvokeScript("add_control");
網(wǎng)頁調(diào)用Winform的方法(一般是函數(shù)),在Winform中寫好,網(wǎng)頁里直接調(diào)用。
比如:window.external.WinFormGetCurrentLngAndLat();
這樣WinForm與瀏覽器就可以交互了,就可以做進(jìn)一步的應(yīng)用了。

新聞標(biāo)題:C#中Winform操作百度地圖
分享地址:http://muchs.cn/article24/pihice.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、微信公眾號、網(wǎng)站收錄、軟件開發(fā)App開發(fā)、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)