Windows下C#的GUI窗口程序中實(shí)現(xiàn)調(diào)用Google Map的實(shí)例
對(duì)谷歌地圖操作使用的是WebBrowser控件,通過對(duì)javascript的操作來實(shí)現(xiàn)對(duì)谷歌地圖的各種操作,所以首先要?jiǎng)?chuàng)建一個(gè)html文件,并賦給WebBrowser的URl:
<!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <title>Google Maps</title> <link rel="stylesheet" type="text/css" /> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var map; function initialize() {//初始化 var myLatlng = new google.maps.LatLng( 34.259442,108.947071); var myOptions = { zoom: 10, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); } function zoomIn(){//放大函數(shù) var zoomLevel = map.getZoom(); if(zoomLevel < 21){ zoomLevel += 1; map.setZoom(zoomLevel); } } function zoomOut(){//縮小函數(shù) var zoomLevel = map.getZoom(); if(zoomLevel > 0){ zoomLevel -= 1; map.setZoom(zoomLevel); } } function markLocation(x,y){//標(biāo)記某個(gè)位置 var myLatlng = new google.maps.LatLng(x, y); map.setCenter(myLatlng); marker = new google.maps.Marker({ map: map, position: myLatlng, draggable:true, title:"緯度:"+x+" 經(jīng)度:"+y }); } </script> </head> <body onload="initialize()"> <div id="map_canvas"></div> </body> </html>
操作地圖的簡(jiǎn)單函數(shù)都寫在javascript里
C#源文件如下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace GoogleMapDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); string url = Application.StartupPath + "/map-simple.html"; webBrowser1.Url = new Uri(url);//指定url } private void toolStripButtonStart_Click(object sender, EventArgs e) { webBrowser1.Document.InvokeScript("initialize");//執(zhí)行jiavascript } private void toolStripButtonZoomIn_Click(object sender, EventArgs e) { webBrowser1.Document.InvokeScript("zoomIn"); } private void toolStripButtonZoomOut_Click(object sender, EventArgs e) { webBrowser1.Document.InvokeScript("zoomOut"); } private void toolStripButtonMark_Click(object sender, EventArgs e) { object[] obj = { toolStripTextBox1.Text, toolStripTextBox2.Text }; webBrowser1.Document.InvokeScript("markLocation", obj); } } }
PS:如果只是想單純地調(diào)用瀏覽器打開網(wǎng)頁(yè),可以這樣:
private void lbllink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { //調(diào)用IE瀏覽器 System.Diagnostics.Process.Start("iexplore.exe", "http://www.google.cn"); //調(diào)用系統(tǒng)默認(rèn)的瀏覽器 System.Diagnostics.Process.Start( "http://www.google.cn"); } private void lbllink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { //調(diào)用IE瀏覽器 System.Diagnostics.Process.Start("iexplore.exe", "http://www.google.cn"); //調(diào)用系統(tǒng)默認(rèn)的瀏覽器 System.Diagnostics.Process.Start( "http://www.google.cn"); }
相關(guān)文章
c#語(yǔ)言使用Unity粒子系統(tǒng)制作手雷爆炸
這篇文章主要為大家介紹了Unity的粒子系統(tǒng)由粒子發(fā)射器、粒子動(dòng)畫器、粒子渲染器組成,通過使用一或兩個(gè)紋理多次繪制,創(chuàng)造一個(gè)混沌的效果,通過復(fù)習(xí)粒子系統(tǒng)做一個(gè)手雷和實(shí)彈投擲現(xiàn)場(chǎng)2022-04-04基于使用BeginInvoke,EndInvoke異步調(diào)用委托的實(shí)現(xiàn)代碼
本篇文章是對(duì)使用BeginInvoke,EndInvoke異步調(diào)用委托的實(shí)現(xiàn)代碼進(jìn)行了分析介紹,需要的朋友參考下2013-05-05gridview的buttonfield獲取該行的索引值(實(shí)例講解)
本篇文章主要介紹了gridview的buttonfield獲取該行的索引值(實(shí)例講解)需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01C#中使用IFormattable實(shí)現(xiàn)自定義格式化字符串輸出示例
這篇文章主要介紹了C#中使用IFormattable實(shí)現(xiàn)自定義格式字符串輸出示例,本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-06-06C# 動(dòng)態(tài)調(diào)用WebService的示例
這篇文章主要介紹了C# 動(dòng)態(tài)調(diào)用WebService的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11無法從 int? 轉(zhuǎn)換為 int 運(yùn)行時(shí)出現(xiàn)錯(cuò)誤
無法從"int?"轉(zhuǎn)換為"int" ,在運(yùn)行時(shí)會(huì)出現(xiàn)錯(cuò)誤,通過強(qiáng)制類型轉(zhuǎn)換(int)便可解決2014-05-05