美文网首页
skyline画多边形

skyline画多边形

作者: MaJiT | 来源:发表于2019-11-20 15:52 被阅读0次

    1.HTML

    <input type="button" value="绘制多边形" id="btnDrawPolygon" />
    

    2. JAVASCRIPT

    //绘制多边形
                var pPolygon = null;//多边形对象
                $("#btnDrawPolygon").bind("click", function () {
                    //注册地图事件
                    SGWorld66.Window.SetInputMode(1);//鼠标样式修改
                    SGWorld66.AttachEvent("OnLButtonDown", StartDrawPolygon);
                    SGWorld66.AttachEvent("OnFrame", DrawPolygonOnFrame);
                    SGWorld66.AttachEvent("OnRButtonUp", StopDrawPolygon)
                })
                //绘制多边形
                var tPolyLine = null;
                function StartDrawPolygon(flags, x, y) {
                    //定义渲染样式
                    var lineColor, lineWidth = 1, lineOpacity = 1, fillColor, fillOpacity = 0.5;
                    lineColor = SGWorld66.Creator.CreateColor(255, 100, 0, 125);//定义线样式
                    fillColor = SGWorld66.Creator.CreateColor(100, 255, 0, 125);//定义面样式
                    //将屏幕坐标转换为地理坐标
                    var pWorldPointInfo = SGWorld66.Window.PixelToWorld(x, y, -1);
                    if (null == pWorldPointInfo) {
                        return false;
                    }
                    try {
                        if (null == pPolygon) {
                            if (!tPolyLine) {
                                var lineGeometry = SGWorld66.Creator.GeometryCreator.CreateLineStringGeometry([pWorldPointInfo.Position, pWorldPointInfo.Position]);
                                tPolyLine = SGWorld66.Creator.CreatePolyline(lineGeometry, lineColor, 2, SGWorld66.ProjectTree.RootID, "线");
                                tPolyLine.Geometry.StartEdit();
                            }
                            else if (tPolyLine.Geometry.Points.Count < 3) {
                                tPolyLine.Geometry.Points.AddPoint(pWorldPointInfo.Position.X, pWorldPointInfo.Position.Y, pWorldPointInfo.Position.Altitude);
                            }
                            else {
                                var points = tPolyLine.Geometry.Points;
                                var ring = SGWorld66.Creator.GeometryCreator.CreateLinearRingGeometry([
                                    points.Item(0).X, points.Item(0).Y, points.Item(0).Z,
                                    points.Item(1).X, points.Item(1).Y, points.Item(1).Z,
                                    points.Item(2).X, points.Item(2).Y, points.Item(2).Z,
                                    pWorldPointInfo.Position.X, pWorldPointInfo.Position.Y, pWorldPointInfo.Position.Altitude
                                ]);
                                var polygonGeometry = SGWorld66.Creator.GeometryCreator.CreatePolygonGeometry(ring, null);
                                pPolygon = SGWorld66.Creator.CreatePolygon(polygonGeometry, lineColor, fillColor, 2, SGWorld66.ProjectTree.RootID, "多边形");
                                pPolygon.LineStyle.Width = lineWidth;
                                pPolygon.LineStyle.Color.SetAlpha(lineOpacity);
                                pPolygon.TreeItem.ShowInViewerTree = true;
                                pPolygon.Geometry.StartEdit();
                                //删除临时的线对象
                                SGWorld66.ProjectTree.DeleteItem(tPolyLine.ID);
                                tPolyLine = null;
                            }
    
                        } else {
                            pPolygon.Geometry.ExteriorRing.Points.AddPoint(pWorldPointInfo.Position.X, pWorldPointInfo.Position.Y, pWorldPointInfo.Position.Altitude);
                        }
                    } catch (ex) {
                        alert(ex.description);
                    }
                }
                function DrawPolygonOnFrame() {
                    //鼠标移动时渲染
                    var teWindow, mouseInfo, mouseInfo, point, lastPoint;
                    teWindow = SGWorld66.Window;
                    mouseInfo = teWindow.GetMouseInfo();
                    point = teWindow.PixelToWorld(mouseInfo.X, mouseInfo.Y, 0);
                    if (null != tPolyLine) {
                        lastPoint = tPolyLine.Geometry.Points.Item(tPolyLine.Geometry.Points.Count - 1);
                        lastPoint.X = point.Position.X;
                        lastPoint.Y = point.Position.Y;
                        lastPoint.Z = point.Position.Altitude;
                        return;
                    }
                    if (null == pPolygon) {
                        return;
                    }
                    lastPoint = pPolygon.Geometry.ExteriorRing.Points.Item(pPolygon.Geometry.ExteriorRing.Points.Count - 1);
                    lastPoint.X = point.Position.X;
                    lastPoint.Y = point.Position.Y;
                    lastPoint.Z = point.Position.Altitude;
    
                }
                function StopDrawPolygon(flags, x, y) {
                    //结束多边形编辑功能
                    SGWorld66.DetachEvent("OnLButtonDown", StartDrawPolygon);
                    SGWorld66.DetachEvent("OnRButtonUp", StopDrawPolygon);
                    SGWorld66.DetachEvent("OnFrame", DrawPolygonOnFrame);
                    pPolygon = null;
                    SGWorld66.Window.SetInputMode(0);//鼠标恢复效果
                }
    
    Javascript Code
    

    相关文章

      网友评论

          本文标题:skyline画多边形

          本文链接:https://www.haomeiwen.com/subject/pqioictx.html