最近学习中遇到了点混乱,就是对坐标系以及相互转换、校正的问题,所以萌生了自己写一个小程序实现坐标转换以及中国地形图查询程序。
进行的过程中对Google地图的定位查询产生了兴趣,于是就想,能不能在窗体中访问Google地图呢,并且根据自己输入的经纬度坐标定位。
经过摸索,终于完成了这一想法。起初没有接触过webBrowser控件,对HTML语言也是一知半解,不过通过MSDN和网上资源还是自己弄明白了些许。
先把程序效果贴上来看看吧
好,下面来看看代码,我的编程环境为VS2008,其实代码本身很简单,我的主要思路就是“HTML代码通过 DocumentText 属性加载到 WebBrowser 控件中,而不是从单独的 HTML 文件加载。”
有关细节都以注释说明了,希望大家能够多多批评指正啊!
view plaincopy to clipboardprint?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
namespace GoogleMap
{
public partial class FormGoogleEarth : Form
{
//HTML 代码通过 DocumentText 属性加载到 WebBrowser 控件中,而不是从单独的 HTML 文件加载。--->这是本程序片段的思路!!!
public FormGoogleEarth()
{
InitializeComponent();
webBrowserMap.Dock = DockStyle.Fill;
Controls.Add(webBrowserMap);
}
private void FormGoogleEarth_Load(object sender, EventArgs e)
{
double lat = 35.06, lon = 117.09;
Map_Load(lat, lon);
textBoxLon2.Clear(); textBoxLon.Clear(); textBoxLat2.Clear(); textBoxLat.Clear();
}
public void Map_Load(double Lat, double Lon)//核心代码!将Html读入WinForm中,并传递经纬度参数!
{
webBrowserMap.DocumentText =
"<html><head><title>Google Earth定位</title></head><body>" +
"<iframe width='425' height='350' frameborder='0' scrolling='no' marginheight='0' marginwidth='0'src='http://ditu.google.cn/?ie=UTF8&hq=&hnear=%E9%99%95%E8%A5%BF%E7%9C%81%E8%A5%BF%E5%AE%89%E5%B8%82&ll=" + Lat + "," + Lon + "&spn=1.107711,2.441711&z=9&brcurrent=3,0x366379e922ac17b9:0x85d466fda794582e,1%3B5,0,1&output=embed'>" +
"</iframe><br /><small>" +
"<a href="http://ditu.google.cn/?ie=UTF8&hq=&hnear=%E9%99%95%E8%A5%BF%E7%9C%81%E8%A5%BF%E5%AE%89%E5%B8%82&ll=" + Lat + "," + Lon + "&spn=1.107711,2.441711&z=9&brcurrent=3,0x366379e922ac17b9:0x85d466fda794582e,1%3B5,0,1&source=embed" mce_href="http://ditu.google.cn/?ie=UTF8&hq=&hnear=%E9%99%95%E8%A5%BF%E7%9C%81%E8%A5%BF%E5%AE%89%E5%B8%82&ll=" + Lat + "," + Lon + "&spn=1.107711,2.441711&z=9&brcurrent=3,0x366379e922ac17b9:0x85d466fda794582e,1%3B5,0,1&source=embed" style="color:#0000FF;text-align:left" mce_style="color:#0000FF;text-align:left">" +
"查看大图</a></small>" +
"</body></html>";
webBrowserMap.Navigating += new WebBrowserNavigatingEventHandler(webBrowserMap_Navigating);
webBrowserMap.ScriptErrorsSuppressed = true;
}
private void webBrowserMap_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Windows.Forms.HtmlDocument document = this.webBrowserMap.Document;//定位!
}
private void buttonRedo_Click(object sender, EventArgs e)//重新定位代码
{
if ( (textBoxLat2.Text.Trim() !="" & textBoxLon2 .Text.Trim () !="") & (textBoxLat.Text.Trim () == "" & textBoxLon.Text.Trim () == "") )
{
double Lon = Convert.ToDouble(textBoxLon2.Text);
double Lat = Convert.ToDouble(textBoxLat2.Text);
Map_Load(Lat, Lon);
textBoxLat2.Clear(); textBoxLon2.Clear();
}//获取以度的方式输入的坐标
else if (textBoxLat.Text != "" & textBoxLon.Text != "" & textBoxLat2.Text == "" & textBoxLon2.Text == "")
{
string LonStr = textBoxLon.Text;
string LatStr = textBoxLat.Text;
double Lon = (Convert.ToDouble(LonStr.Substring(LonStr.Length - 2, 2))) / 3600 + (Convert.ToDouble(LonStr.Substring(LonStr.Length - 4, 2))) / 60 + Convert.ToDouble(LonStr.Remove(LonStr.Length - 4, 4));
double Lat = (Convert.ToDouble(LatStr.Substring(LatStr.Length - 2, 2))) / 3600 + (Convert.ToDouble(LatStr.Substring(LatStr.Length - 4, 2))) / 60 + Convert.ToDouble(LatStr.Remove(LatStr.Length - 4, 4));
Map_Load(Lat, Lon);
textBoxLon.Clear(); textBoxLat.Clear();
}//获取以度分秒输入的坐标信息
else
{
MessageBox.Show("输入格式有误!请核对输入格式!\n“度分秒”格式如1084530,位数为5~7位。\n“度”格式如35.06,位数小于16。\n北纬为正数,南纬为负数。\n东经为正数,西经为负数。", "出错啦!", MessageBoxButtons.OK);
textBoxLon2.Clear(); textBoxLon.Clear(); textBoxLat2.Clear(); textBoxLat.Clear();
}//如果输入格式有误,则清除现有文本框里的内容
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
namespace GoogleMap
{
public partial class FormGoogleEarth : Form
{
//HTML 代码通过 DocumentText 属性加载到 WebBrowser 控件中,而不是从单独的 HTML 文件加载。--->这是本程序片段的思路!!!
public FormGoogleEarth()
{
InitializeComponent();
webBrowserMap.Dock = DockStyle.Fill;
Controls.Add(webBrowserMap);
}
private void FormGoogleEarth_Load(object sender, EventArgs e)
{
double lat = 35.06, lon = 117.09;
Map_Load(lat, lon);
textBoxLon2.Clear(); textBoxLon.Clear(); textBoxLat2.Clear(); textBoxLat.Clear();
}
public void Map_Load(double Lat, double Lon)//核心代码!将Html读入WinForm中,并传递经纬度参数!
{
webBrowserMap.DocumentText =
"<html><head><title>Google Earth定位</title></head><body>" +
"<iframe width='425' height='350' frameborder='0' scrolling='no' marginheight='0' marginwidth='0'src='http://ditu.google.cn/?ie=UTF8&hq=&hnear=%E9%99%95%E8%A5%BF%E7%9C%81%E8%A5%BF%E5%AE%89%E5%B8%82&ll=" + Lat + "," + Lon + "&spn=1.107711,2.441711&z=9&brcurrent=3,0x366379e922ac17b9:0x85d466fda794582e,1%3B5,0,1&output=embed'>" +
"</iframe><br /><small>" +
"<a href="http://ditu.google.cn/?ie=UTF8&hq=&hnear=%E9%99%95%E8%A5%BF%E7%9C%81%E8%A5%BF%E5%AE%89%E5%B8%82&ll=" + Lat + "," + Lon + "&spn=1.107711,2.441711&z=9&brcurrent=3,0x366379e922ac17b9:0x85d466fda794582e,1%3B5,0,1&source=embed" mce_href="http://ditu.google.cn/?ie=UTF8&hq=&hnear=%E9%99%95%E8%A5%BF%E7%9C%81%E8%A5%BF%E5%AE%89%E5%B8%82&ll=" + Lat + "," + Lon + "&spn=1.107711,2.441711&z=9&brcurrent=3,0x366379e922ac17b9:0x85d466fda794582e,1%3B5,0,1&source=embed" style="color:#0000FF;text-align:left" mce_style="color:#0000FF;text-align:left">" +
"查看大图</a></small>" +
"</body></html>";
webBrowserMap.Navigating += new WebBrowserNavigatingEventHandler(webBrowserMap_Navigating);
webBrowserMap.ScriptErrorsSuppressed = true;
}
private void webBrowserMap_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Windows.Forms.HtmlDocument document = this.webBrowserMap.Document;//定位!
}
private void buttonRedo_Click(object sender, EventArgs e)//重新定位代码
{
if ( (textBoxLat2.Text.Trim() !="" & textBoxLon2 .Text.Trim () !="") & (textBoxLat.Text.Trim () == "" & textBoxLon.Text.Trim () == "") )
{
double Lon = Convert.ToDouble(textBoxLon2.Text);
double Lat = Convert.ToDouble(textBoxLat2.Text);
Map_Load(Lat, Lon);
textBoxLat2.Clear(); textBoxLon2.Clear();
}//获取以度的方式输入的坐标
else if (textBoxLat.Text != "" & textBoxLon.Text != "" & textBoxLat2.Text == "" & textBoxLon2.Text == "")
{
string LonStr = textBoxLon.Text;
string LatStr = textBoxLat.Text;
double Lon = (Convert.ToDouble(LonStr.Substring(LonStr.Length - 2, 2))) / 3600 + (Convert.ToDouble(LonStr.Substring(LonStr.Length - 4, 2))) / 60 + Convert.ToDouble(LonStr.Remove(LonStr.Length - 4, 4));
double Lat = (Convert.ToDouble(LatStr.Substring(LatStr.Length - 2, 2))) / 3600 + (Convert.ToDouble(LatStr.Substring(LatStr.Length - 4, 2))) / 60 + Convert.ToDouble(LatStr.Remove(LatStr.Length - 4, 4));
Map_Load(Lat, Lon);
textBoxLon.Clear(); textBoxLat.Clear();
}//获取以度分秒输入的坐标信息
else
{
MessageBox.Show("输入格式有误!请核对输入格式!\n“度分秒”格式如1084530,位数为5~7位。\n“度”格式如35.06,位数小于16。\n北纬为正数,南纬为负数。\n东经为正数,西经为负数。", "出错啦!", MessageBoxButtons.OK);
textBoxLon2.Clear(); textBoxLon.Clear(); textBoxLat2.Clear(); textBoxLat.Clear();
}//如果输入格式有误,则清除现有文本框里的内容
}
}
}