Windows下C#的GUI窗口程序中實現(xiàn)調(diào)用Google Map的實例
對谷歌地圖操作使用的是WebBrowser控件,通過對javascript的操作來實現(xiàn)對谷歌地圖的各種操作,所以首先要創(chuàng)建一個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)記某個位置
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>
操作地圖的簡單函數(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)頁,可以這樣:
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)文章
基于使用BeginInvoke,EndInvoke異步調(diào)用委托的實現(xiàn)代碼
本篇文章是對使用BeginInvoke,EndInvoke異步調(diào)用委托的實現(xiàn)代碼進(jìn)行了分析介紹,需要的朋友參考下2013-05-05
gridview的buttonfield獲取該行的索引值(實例講解)
本篇文章主要介紹了gridview的buttonfield獲取該行的索引值(實例講解)需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
C#中使用IFormattable實現(xiàn)自定義格式化字符串輸出示例
這篇文章主要介紹了C#中使用IFormattable實現(xiàn)自定義格式字符串輸出示例,本文直接給出實例代碼,需要的朋友可以參考下2015-06-06
C# 動態(tài)調(diào)用WebService的示例
這篇文章主要介紹了C# 動態(tài)調(diào)用WebService的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-11-11
無法從 int? 轉(zhuǎn)換為 int 運行時出現(xiàn)錯誤
無法從"int?"轉(zhuǎn)換為"int" ,在運行時會出現(xiàn)錯誤,通過強制類型轉(zhuǎn)換(int)便可解決2014-05-05

