UNPKG

mmg

Version:

Simple markers for Modest Maps

1 lines 4.42 kB
function mmg(){function j(b){b.coord||(b.coord=a.map.locationCoordinate(b.location));var c=a.map.coordinatePoint(b.coord),d;c.x<0?(d=new MM.Location(b.location.lat,b.location.lon),d.lon+=Math.ceil((h.lon-b.location.lon)/360)*360,c=a.map.locationPoint(d),b.coord=a.map.locationCoordinate(d)):c.x>a.map.dimensions.x&&(d=new MM.Location(b.location.lat,b.location.lon),d.lon-=Math.ceil((b.location.lon-i.lon)/360)*360,c=a.map.locationPoint(d),b.coord=a.map.locationCoordinate(d)),c.scale=1,c.width=c.height=0,MM.moveElement(b.element,c)}[].indexOf||(Array.prototype.indexOf=function(a,b,c){for(c=this.length,b=(c+~~b)%c;b<c&&(!(b in this)||this[b]!==a);b++);return b^c?b:-1});var a={},b=[],c=[],d=null,e=null,f=null,g,h=null,i=null;a.draw=function(){if(!a.map)return;h=a.map.pointLocation(new MM.Point(0,0)),i=a.map.pointLocation(new MM.Point(a.map.dimensions.x,0));for(var b=0;b<c.length;b++)j(c[b])},a.add=function(a){return!a||!a.element?null:(k.appendChild(a.element),c.push(a),a)},a.remove=function(a){return a?(k.removeChild(a.element),c.splice(c.indexOf(a),1),a):null},a.markers=function(a){if(!arguments.length)return c},a.features=function(d){if(!arguments.length)return b;while(k.hasChildNodes())k.removeChild(k.lastChild);c=[],d||(d=[]),b=d,b.sort(f);for(var g=0;g<d.length;g++)a.add({element:e(d[g]),location:new MM.Location(d[g].geometry.coordinates[1],d[g].geometry.coordinates[0]),data:d[g]});return a.map&&a.map.coordinate&&a.map.draw(),a},a.url=function(b,c){function d(b){b&&b.features&&a.features(b.features),c&&c(b.features,a)}if(!arguments.length)return g;if(typeof reqwest=="undefined")throw"reqwest is required for url loading";return typeof b=="string"&&(b=[b]),g=b,reqwest(g[0].match(/geojsonp$/)?{url:g[0]+(~g[0].indexOf("?")?"&":"?")+"callback=grid",type:"jsonp",jsonpCallback:"callback",success:d,error:d}:{url:g[0],type:"json",success:d,error:d}),a},a.extent=function(){var b=[{lat:Infinity,lon:Infinity},{lat:-Infinity,lon:-Infinity}],c=a.features();for(var d=0;d<c.length;d++){var e=c[d].geometry.coordinates;e[0]<b[0].lon&&(b[0].lon=e[0]),e[1]<b[0].lat&&(b[0].lat=e[1]),e[0]>b[1].lon&&(b[1].lon=e[0]),e[1]>b[1].lat&&(b[1].lat=e[1])}return b},a.factory=function(b){return arguments.length?(e=b,a):e},a.factory(function(a){var b=document.createElement("div");return b.className="mmg-default",b.style.position="absolute",b}),a.sort=function(b){return arguments.length?(f=b,a):f},a.sort(function(a,b){return b.geometry.coordinates[1]-a.geometry.coordinates[1]}),a.destroy=function(){this.parent.parentNode&&this.parent.parentNode.removeChild(this.parent)};var k=document.createElement("div");return k.style.cssText="position: absolute; top: 0px;left: 0px; width: 100%; height: 100%; margin: 0; padding: 0; z-index: 0",a.parent=k,a}function mmg_interaction(a){var b={},c=[],d=!0,e=!0,f;b.formatter=function(a){return arguments.length?(f=a,b):f},b.formatter(function(a){var b="",c=a.properties;return c.title&&(b+="<strong>"+c.title+"</strong><br />"),c.description&&(b+=c.description),b}),b.hide_on_move=function(a){return arguments.length?(e=a,b):e},b.exclusive=function(a){return arguments.length?(d=a,b):d},b.bind_marker=function(b){b.element.onclick=function(e){d&&c.length>0&&a.remove(c.pop());var g=document.createElement("div");g.className="wax-movetip";var h=g.appendChild(document.createElement("div"));h.className="wax-intip",h.innerHTML=f(b.data),document.body.appendChild(g),h.style.marginTop=-(b.element.offsetHeight*.5+g.offsetHeight+10)+"px",document.body.removeChild(g);var i={element:g,data:{},location:b.location.copy()};c.push(i),a.add(i),a.draw()}};if(a&&a.map){a.map.addCallback("panned",function(){if(e)while(c.length)a.remove(c.pop())});var g=a.markers();for(var h=0;h<g.length;h++)b.bind_marker(g[h])}else console&&console.log("mmg must be added to a map before interaction is assigned");return b}function simplestyle_factory(a){var b={small:[20,50],medium:[30,70],large:[35,90]},c=a.properties||{},d=c["marker-size"]||"medium",e=c["marker-symbol"]?"-"+c["marker-symbol"]:"",f=c["marker-color"]||"7e7e7e";f=f.replace("#","");var g=document.createElement("div");g.className="simplestyle-marker";var h=g.style;return h.position="absolute",h.width=b[d][0]+"px",h.height=b[d][1]+"px",h.marginTop=-(b[d][1]/2)+"px",h.marginLeft=-(b[d][0]/2)+"px",h.backgroundImage="url(http://a.tiles.mapbox.com/v3/marker/pin-"+d[0]+e+"+"+f+".png)",h.textIndent="-10000px",g.innerHTML=c.title||"",g}