對谷歌地圖操作使用的是WebBrowser控件,通過對Javascript的操作來實現對谷歌地圖的各種操作,所以首先要創建一個Html文件,並賦給WebBrowser的URl:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55<!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.google.com/apis/maps/documentation/Javascript/examples/default.CSS"
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(){
//放大函數
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
google.maps.LatLng(x, y);
map.setCenter(myLatlng);
marker =
new
google.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.google.cn"
);
//調用系統默認的浏覽器
System.Diagnostics.Process.Start(
"http://www.google.cn"
);
}
private
void
lbllink_LinkClicked(
object
sender, LinkLabelLinkClickedEventArgs e)
{
//調用IE浏覽器
System.Diagnostics.Process.Start(
"IExplore.exe"
,
"http://www.google.cn"
);
//調用系統默認的浏覽器
System.Diagnostics.Process.Start(
"http://www.google.cn"
);
}