c# google map画点、线

原文地址:c# google map画点、线 作者:留心驿站

winForm:

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 googleMap
{
    //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>();
        private System.Windows.Forms.Timer trackTimer = new System.Windows.Forms.Timer();

        public Form1()
        {
            InitializeComponent();  
            webBrowser1.Navigate(new Uri("file:///E:\googleMap\googleMap\bin\Debug\GoogleMapGPS.html"));
            webBrowser1.ObjectForScripting = this;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadPoints();
        }

        /// <summary>
        /// 加载坐标点
        /// </summary>
        private void LoadPoints()
        {
            string points = "113.944465218958,22.5428080702352; 113.945051582597,22.5428141763446; 113.945595019848,22.5428426626812;113.946106003403,22.5428681417555; 113.946600940856,22.5428875769784";
            //string points = "113.911976666667,22.498635; 113.931975,22.51861; 113.95199,22.5585766666667";//; 113.91198,22.4985716666667; 113.911973333333,22.498565; 113.911965,22.49856; 113.912001666667,22.49852; 113.912015,22.4985116666667";
            string[] coordXY = points.Split(";".ToCharArray());
            foreach (string pt in coordXY)
            {
                string[] xyCoord = pt.Split(",".ToCharArray());
                Latitudes.Add(double.Parse(xyCoord[1]));
                Longitudes.Add(double.Parse(xyCoord[0]));
            }
        }

        private int curPointCount=0;//当前的坐标点
        private void tracTimer_Tick(object sender, EventArgs e)
        {
            if (curPointCount < Latitudes.Count)
            {
                callJsMethod("SetLocation", Latitudes[curPointCount], Longitudes[curPointCount]);
                curPointCount++;
            }
            else
            {
                trackTimer.Enabled = false;
            }
        }

        /// <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(tracTimer_Tick);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            trackTimer.Enabled = false;
        }


    }
}

 

 

webForm:

<!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.getElementByIdx_x("google_map"));
  map.addControl(new GLargeMapControl());//增加导航控件
  map.addControl(new GScaleControl());//增加比例尺控件
  map.addControl(new GMapTypeControl());//创建可切换地图
  map.addControl(new GOverviewMapControl());//增加鹰眼控件
        map.setCenter(new GLatLng(22.531353, 113.940187), 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.clearOverlays();
   map.addOverlay(new GMarker(point));
  }
  else
  {
   map.clearOverlays();
  }
 }
 
    </script>
  </head>
  <body οnlοad="initialize()" οnunlοad="GUnload()" style="font-family: Arial;border: 0 none;">
    <div id="google_map" style="width: 400px; height: 428px"></div>
  </body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值