From 546b6bc4e37df2e83c7cc74e4985d2d1cc99b021 Mon Sep 17 00:00:00 2001 From: Zenkie Zhu Date: Wed, 31 May 2017 12:14:44 +0800 Subject: [PATCH] =?UTF-8?q?Add=20support(point,=20polygon)=20for=20AMap(?= =?UTF-8?q?=E9=AB=98=E5=BE=B7=20from=20China)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wicket-amap.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 wicket-amap.js diff --git a/wicket-amap.js b/wicket-amap.js new file mode 100644 index 0000000..cf24e31 --- /dev/null +++ b/wicket-amap.js @@ -0,0 +1,56 @@ +(function (Wkt) { + + Wkt.Wkt.prototype.construct = { + point: function(config) { + var opt = config || {}; + opt.position = new AMap.LngLat(this.components[0].x, this.components[0].y); + return new AMap.Marker(opt); + }, + + polygon: function (config) { + var opt = config || {}; + opt.path = this.components[0].map(function(p) { + return new AMap.LngLat(p.x, p.y); + }); + opt.path.pop(); // unclosure + return new AMap.Polygon(opt); + } + }; + + Wkt.Wkt.prototype.deconstruct = deconstruct; + + function deconstruct(obj) { + if (obj.constructor === AMap.Marker) { + var p = obj.getPosition(); + return { + type: 'point', + components: [{ + x: p.getLng(), + y: p.getLat() + }] + }; + } + + if (obj.constructor === AMap.Polygon) { + var verts = obj.getPath().map(function(p) { + return { + x: p.getLng(), + y: p.getLat() + }; + }); + verts.push({ // closure + x: verts[0].x, + y: verts[0].y + }); + + return { + type: 'polygon', + components: [verts] + }; + } + + console.error('Unsupported geometry class'); + return null; + } + +}(Wkt || require('./wicket')));