alphavor_jay的个人空间 https://blog.eetop.cn/840234 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

GOOGLE MAP的开发

已有 2789 次阅读| 2012-9-6 13:43 |个人分类:上位机软件

老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&amp;v=2&amp;sensor=false&amp;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>


点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 8

    粉丝
  • 0

    好友
  • 22

    获赞
  • 51

    评论
  • 2656

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-27 05:44 , Processed in 0.030454 second(s), 14 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部