[C# WinFrom 使用 Google Map] 在地图上画轨迹线

 

google map 上画 多条线段是使用 google.maps.Polyline 来实现的.

javascript 代码:

   var polyline;   
//画多边形 google.maps.Polyline function drawPolyline(latLngArray) { var path = new Array(); var latLngs = latLngArray.toString().split(";"); for(index in latLngs) { var latLng = latLngs[index].split(","); if(latLng.length==2) { path.push((new google.maps.LatLng(latLng[0], latLng[1]))); } } polyline = new google.maps.Polyline( { map: map, path: path, geodesic: true, strokeColor: "#FF0000", strokeOpacity: 0.6, strokeWeight: 2 }); } function clearPolyLine() { if(polyline!=null) { polyline.setMap(null); } }

C#调用代码:

        public static string DrawPolyline(WebBrowser wb, List<PointF> ptList)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in ptList)
            {
                sb.AppendFormat("{0},{1};", item.Y, item.X);
            }

            Object[] objArray = new Object[1] { sb.ToString().TrimEnd(';') };
   

            string optName = "drawPolyline";
            return DoneGoogleMapOp(wb, optName, objArray);
        }


// 执行脚本命令 private static string DoneGoogleMapOp(WebBrowser wb, string optName, Object[] objArray) { string retStr = ""; try { Object objRet = wb.Document.InvokeScript(optName, objArray); if (objRet != null) retStr = objRet.ToString(); } catch { } return retStr; } //画轨迹数据 public static string DrawPolyline(WebBrowser wb, List<LatLng> trackList) { List<PointF> ptList = new List<PointF>(); trackList.ForEach(item => ptList.Add(new PointF((float)item.Lng, (float)item.Lat))); return DrawPolyline(wb, ptList); } // clearPolyLine public static string ClearPolyLine(WebBrowser wb) { return DoneGoogleMapOp(wb, "clearPolyLine", null); }

 使用实际参数调用:

   DrawPolyline(wbGoogleMap, trackList);

  

posted @ 2012-06-04 11:58  沧海小小粟  阅读(4730)  评论(0编辑  收藏  举报