Windows下C#的GUI窗口法式中完成挪用Google Map的實例。本站提示廣大學習愛好者:(Windows下C#的GUI窗口法式中完成挪用Google Map的實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Windows下C#的GUI窗口法式中完成挪用Google Map的實例正文
對谷歌地圖操作應用的是WebBrowser控件,經由過程對javascript的操作來完成對谷歌地圖的各類操作,所以起首要創立一個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 href="http://code.谷歌.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="http://maps.谷歌.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> var map; function initialize() {//初始化 var myLatlng = new 谷歌.maps.LatLng( 34.259442,108.947071); var myOptions = { zoom: 10, center: myLatlng, mapTypeId: 谷歌.maps.MapTypeId.ROADMAP } map = new 谷歌.maps.Map(document.getElementById("map_canvas"), myOptions); } function zoomIn(){//縮小函數 var zoomLevel = map.getZoom(); if(zoomLevel < 21){ zoomLevel += 1; map.setZoom(zoomLevel); } } function zoomOut(){//減少函數 var zoomLevel = map.getZoom(); if(zoomLevel > 0){ zoomLevel -= 1; map.setZoom(zoomLevel); } } function markLocation(x,y){//標志某個地位 var myLatlng = new 谷歌.maps.LatLng(x, y); map.setCenter(myLatlng); marker = new 谷歌.maps.Marker({ map: map, position: myLatlng, draggable:true, title:"緯度:"+x+" 經度:"+y }); } </script> </head> <body onload="initialize()"> <div id="map_canvas"></div> </body> </html>
操作地圖的簡略函數都寫在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");//履行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:假如只是想純真地挪用閱讀器翻開網頁,可以如許:
private void lbllink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { //挪用IE閱讀器 System.Diagnostics.Process.Start("iexplore.exe", "http://www.谷歌.cn"); //挪用體系默許的閱讀器 System.Diagnostics.Process.Start( "http://www.谷歌.cn"); } private void lbllink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { //挪用IE閱讀器 System.Diagnostics.Process.Start("iexplore.exe", "http://www.谷歌.cn"); //挪用體系默許的閱讀器 System.Diagnostics.Process.Start( "http://www.谷歌.cn"); }