// // Contact hostmaster@kodafritt.se if this script causes problems for you! // OSMCaptcha = {} // A single map control OSMCaptcha.Map = function(placeholder, lon, lat) { var options = { credentials: '[hidden]', showDashboard: false, showScalebar: false, disablePanning: true, disableZooming: true, mapTypeId: Microsoft.Maps.MapTypeId.aerial, labelOverlay: Microsoft.Maps.LabelOverlay.hidden, // these are also disabled by the options above disableBirdseye: true, enableSearchLogo: false, showMapTypeSelector: false, // location zoom: 19, center: new Microsoft.Maps.Location(lat, lon), }; this._map = new Microsoft.Maps.Map(placeholder, options); this._optPoly = { strokeColor: new Microsoft.Maps.Color(0xCC, 0xFF, 0x66, 0x66), }; this._optSegment = { strokeColor: new Microsoft.Maps.Color(0xAA, 0xFF, 0x66, 0x66), }; this._optClosed = { strokeColor: new Microsoft.Maps.Color(0xFF, 0x66, 0x66, 0xFF), }; this._poly = new Microsoft.Maps.Polyline([], this._optPoly); this._map.entities.push(this._poly); this._nextsegment = new Microsoft.Maps.Polyline([], this._optSegment); this._lastpos = null; this._polyPoints = []; var self = this; Microsoft.Maps.Events.addHandler(this._map, 'click', function(e) { self._click(e); }); Microsoft.Maps.Events.addHandler(this._map, 'mousemove', function(e) { self._move(e); }); this._map.setView({ animate: false, zoom: 20 }); } OSMCaptcha.Map.prototype._click = function(ev) { var e = ev || window.event; var loc = this._posToLoc(e); if (!this._lastpos && this._polyPoints.length != 0) return; if (this._lastpos != null) { // Remove preview of line segment this._map.entities.remove(this._nextsegment); // or removeAt? // Check if near start if (this._polyPoints.length >= 3) { var pt = new Microsoft.Maps.Point(e.getX(), e.getY()); var startpt = this._map.tryLocationToPixel(this._polyPoints[0]); if (8 > this._distance(pt, startpt)) { this._polyPoints.push(this._polyPoints[0]); this._poly.setLocations(this._polyPoints); this._lastpos = null; return; } } } // Add to polyline this._polyPoints.push(loc); this._poly.setLocations(this._polyPoints); // Set start position for line preview this._lastpos = loc; this._nextsegment.setLocations([]); this._map.entities.push(this._nextsegment); } OSMCaptcha.Map.prototype._move = function(ev) { var e = ev || window.event; if (!this._lastpos) return; this._nextsegment.setLocations([this._lastpos, this._posToLoc(e)]); if (this._polyPoints.length >= 3) { var pt = new Microsoft.Maps.Point(e.getX(), e.getY()); var startpt = this._map.tryLocationToPixel(this._polyPoints[0]); if (8 > this._distance(pt, startpt)) { this._poly.setOptions(this._optClosed); this._nextsegment.setOptions(this._optClosed); this._nextsegment.setLocations([this._lastpos, this._polyPoints[0]]); } else { this._poly.setOptions(this._optPoly); this._nextsegment.setOptions(this._optSegment); } } } OSMCaptcha.Map.prototype._distance = function(a, b) { var dx = a.x - b.x; var dy = a.y - b.y; return Math.sqrt(dx*dx + dy*dy); } OSMCaptcha.Map.prototype._posToLoc = function(e) { var pt = new Microsoft.Maps.Point(e.getX(), e.getY()); return this._map.tryPixelToLocation(pt); } // A captcha control with two map controls OSMCaptcha.Captcha = function(placeholder) { this._locs = [[18.1994, 59.2238], [18.19987, 59.22365]]; this._maps = []; for (i = 0; i < 2; i++) { var wrapper = document.createElement('div'); wrapper.className = 'OSMCaptchaMap'; wrapper.setAttribute('style', 'display: inline-block; width: 390px; height: 400px; position: relative'); if (i == 0) wrapper.style.marginRight = '5px'; placeholder.appendChild(wrapper); this._maps.push(new OSMCaptcha.Map(wrapper, this._locs[i][0], this._locs[i][1])); } }