| |
老API (API V2,The V2 API will continue to work until May 19, 2013)地址:
https://developers.google.com/maps/documentation/javascript/v2/reference
新API(API3) 地址:https://developers.google.com/maps/documentation/javascript/reference#Map
例程:
C#部分
===============================================
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;
using System.Security.Permissions;
namespace Google_Map_Interface
{
//web访问winform时设置权限
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form1 : Form
{
private List<double> Latitudes = new List<double>();
private List<double> Longitudes = new List<double>();
public Form1()
{
InitializeComponent();
webBrowser1.Navigate(new Uri("file:///D:\\My Documents\\Visual Studio 2010\\Projects\\Google_Map_Interface\\Google_Map_Interface\\html_src\\GoogleMapGPS.html"));
webBrowser1.ObjectForScripting = this;
}
private void Form1_Load(object sender, EventArgs e)
{
LoadPoints();
}
/// <summary>
/// 加载坐标点
/// </summary>
private void LoadPoints()
{
string points = "28.338464,113.089614;28.438464,113.089614;";
string[] coordXY = points.Split(";".ToCharArray());
foreach (string pt in coordXY)
{
string[] xyCoord = pt.Split(",".ToCharArray());
Latitudes.Add(double.Parse(xyCoord[0]));
Longitudes.Add(double.Parse(xyCoord[1]));
}
}
/// <summary>
/// winForm调用JS的的方法
/// </summary>
/// <param name="jsFunction">JS函数</param>
/// <param name="jsParameter1">函数中第一个参数</param>
/// <param name="jsParameter2">函数中第二个参数</param>
private void callJsMethod(string jsFunction, double jsParameter1, double jsParameter2)
{
webBrowser1.Document.InvokeScript(jsFunction, new object[] { jsParameter1, jsParameter2 });
}
private void btnStart_Click(object sender, EventArgs e)
{
curPointCount = 0;
trackTimer.Interval = 1000;
trackTimer.Enabled = true;
trackTimer.Tick += new EventHandler(trackTimer_Tick);
}
private void btnStop_Click(object sender, EventArgs e)
{
trackTimer.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
double lati = Convert.ToDouble(textBox1.Text); //纬度
double longti = Convert.ToDouble(textBox2.Text); //经度
callJsMethod("SetLocation", lati, longti);
}
private int curPointCount = 0;//当前的坐标点
private void trackTimer_Tick(object sender, EventArgs e)
{
if (curPointCount < Latitudes.Count)
{
callJsMethod("SetLocation", Latitudes[curPointCount], Longitudes[curPointCount]);
curPointCount++;
}
else
{
trackTimer.Enabled = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
callJsMethod("DelAllLocation", 1, 1);
}
}
}
HTML部分(JS)
===============================================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta. http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps API Sample</title>
<script. src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAAsb1IPZFqurjmMaY5UTNvpBQaQ9fcjdeAB__afRoy5udx13-bRxS7ayw4JvJ-bv4PmD7koSQeuI6dfA" type="text/javascript"></script>
<script. type="text/javascript">
var map = null;
function initialize() {
if (GBrowserIsCompatible())
{
map = new GMap2(document.getElementById("google_map"));
map.addControl(new GLargeMapControl()); //增加导航控件
map.addControl(new GScaleControl()); //增加比例尺控件
map.addControl(new GMapTypeControl()); //创建可切换地图
map.addControl(new GOverviewMapControl()); //增加鹰眼控件
map.setCenter(new GLatLng(28.238464,113.089614), 15); //13表示比例尺等级
map.enableDoubleClickZoom(); //允许双击缩放
map.enableScrollWheelZoom(); //允许滚轮缩放
//var polyline = new GPolyline([new GLatLng(22.531353, 113.94018),new GLatLng(22.551353, 113.94018)], "#ff0000", 10);
//map.addOverlay(polyline);
}
}
function SetLocation(strVal1, strVal2) //播放器中自动调用该函数
{
if(strVal1!=0 && strVal2!=0)
{
//map.setCenter(new GLatLng(strVal1, strVal2));
point = new GLatLng(strVal1, strVal2);
map.addOverlay(new GMarker(point));
}
else
{
map.clearOverlays();
}
}
function DelAllLocation(strVal1, strVal2) //播放器中自动调用该函数
{
map.clearOverlays();
}
</script>
</head>
<body nload="initialize()" nunload="GUnload()" style="font-family: Arial;border: 0 none;">
<div id="google_map" style="width: 400px; height: 428px"></div>
</body>
</html>