如何将我存储的多边形从数据库绘制到谷歌地图上

时间:2016-04-04 15:38:09

标签: javascript angularjs google-maps google-maps-api-3 angular-google-maps

对于角度谷歌地图,我希望我可以将存储的多边形标记数组绘制到地图上。

我使用的技术是:使用JavaScript的Angular版本1,使用angular指令和templateUrl。

这是我的html指令模板:

<ui-gmap-google-map center="config.map.center" zoom="config.map.zoom" options="config.map.options" events="config.map.events" draggable="true">

        <ui-gmap-polygon path="compatiblePolygon" stroke="polygonConfig.stroke" fill="polygonConfig.fill" fit="true" static="true" visible="polygonConfig.visible" editable="polygonConfig.editable" draggable="polygonConfig.draggable" clickable="true" events="polygonConfig.events">
        </ui-gmap-polygon>

        <ui-gmap-markers models="compatiblePoints" coords="'self'" idKey="'id'"
            options="pointsConfig.options"
            clickable="true">
        </ui-gmap-markers>

        <ui-gmap-drawing-manager options="config.drawing.options" static="true" control="drawingManagerControl" events="config.drawing.events"></ui-gmap-drawing-manager>

    </ui-gmap-google-map>

1 个答案:

答案 0 :(得分:2)

You need to store your polygon as an encoded string in database, instead of polygon coordinates. you can obtain encoded polygon as follows:

/* This function save latitude and longitude to the polygons[] variable after we call it. */
    function encodePolygon(polygon)
    {
        //This variable gets all bounds of polygon.
        var path = polygon.getPath();

        var encodeString = 
        google.maps.geometry.encoding.encodePath(path);

        /* store encodeString in database */
    }

Then you can redraw your polygon from encode string any time using:

function DrawPolygon(encodedString){
    var decodedPolygon =   
   google.maps.geometry.encoding.decodePath(encodedString);

            var polygon = new google.maps.Polygon({
                paths: decodedPolygon,
                editable: false,
                strokeColor: '#FFFF',
                strokeOpacity: 0.8,
                strokeWeight: 2,
                fillColor: '#FFFF',
                fillOpacity: 0.35
            });
}