leaflet-src.js 210 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909
  1. /*
  2. Leaflet, a JavaScript library for mobile-friendly interactive maps. http://leafletjs.com
  3. (c) 2010-2013, Vladimir Agafonkin
  4. (c) 2010-2011, CloudMade
  5. */
  6. (function (window, document, undefined) {
  7. var oldL = window.L,
  8. L = {};
  9. L.version = '0.6.4';
  10. // define Leaflet for Node module pattern loaders, including Browserify
  11. if (typeof module === 'object' && typeof module.exports === 'object') {
  12. module.exports = L;
  13. // define Leaflet as an AMD module
  14. } else if (typeof define === 'function' && define.amd) {
  15. define(L);
  16. }
  17. // define Leaflet as a global L variable, saving the original L to restore later if needed
  18. L.noConflict = function () {
  19. window.L = oldL;
  20. return this;
  21. };
  22. window.L = L;
  23. /*
  24. * L.Util contains various utility functions used throughout Leaflet code.
  25. */
  26. L.Util = {
  27. extend: function (dest) { // (Object[, Object, ...]) ->
  28. var sources = Array.prototype.slice.call(arguments, 1),
  29. i, j, len, src;
  30. for (j = 0, len = sources.length; j < len; j++) {
  31. src = sources[j] || {};
  32. for (i in src) {
  33. if (src.hasOwnProperty(i)) {
  34. dest[i] = src[i];
  35. }
  36. }
  37. }
  38. return dest;
  39. },
  40. bind: function (fn, obj) { // (Function, Object) -> Function
  41. var args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 2) : null;
  42. return function () {
  43. return fn.apply(obj, args || arguments);
  44. };
  45. },
  46. stamp: (function () {
  47. var lastId = 0,
  48. key = '_leaflet_id';
  49. return function (obj) {
  50. obj[key] = obj[key] || ++lastId;
  51. return obj[key];
  52. };
  53. }()),
  54. invokeEach: function (obj, method, context) {
  55. var i, args;
  56. if (typeof obj === 'object') {
  57. args = Array.prototype.slice.call(arguments, 3);
  58. for (i in obj) {
  59. method.apply(context, [i, obj[i]].concat(args));
  60. }
  61. return true;
  62. }
  63. return false;
  64. },
  65. limitExecByInterval: function (fn, time, context) {
  66. var lock, execOnUnlock;
  67. return function wrapperFn() {
  68. var args = arguments;
  69. if (lock) {
  70. execOnUnlock = true;
  71. return;
  72. }
  73. lock = true;
  74. setTimeout(function () {
  75. lock = false;
  76. if (execOnUnlock) {
  77. wrapperFn.apply(context, args);
  78. execOnUnlock = false;
  79. }
  80. }, time);
  81. fn.apply(context, args);
  82. };
  83. },
  84. falseFn: function () {
  85. return false;
  86. },
  87. formatNum: function (num, digits) {
  88. var pow = Math.pow(10, digits || 5);
  89. return Math.round(num * pow) / pow;
  90. },
  91. trim: function (str) {
  92. return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '');
  93. },
  94. splitWords: function (str) {
  95. return L.Util.trim(str).split(/\s+/);
  96. },
  97. setOptions: function (obj, options) {
  98. obj.options = L.extend({}, obj.options, options);
  99. return obj.options;
  100. },
  101. getParamString: function (obj, existingUrl, uppercase) {
  102. var params = [];
  103. for (var i in obj) {
  104. params.push(encodeURIComponent(uppercase ? i.toUpperCase() : i) + '=' + encodeURIComponent(obj[i]));
  105. }
  106. return ((!existingUrl || existingUrl.indexOf('?') === -1) ? '?' : '&') + params.join('&');
  107. },
  108. template: function (str, data) {
  109. return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
  110. var value = data[key];
  111. if (value === undefined) {
  112. throw new Error('No value provided for variable ' + str);
  113. } else if (typeof value === 'function') {
  114. value = value(data);
  115. }
  116. return value;
  117. });
  118. },
  119. isArray: function (obj) {
  120. return (Object.prototype.toString.call(obj) === '[object Array]');
  121. },
  122. emptyImageUrl: 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
  123. };
  124. (function () {
  125. // inspired by http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  126. function getPrefixed(name) {
  127. var i, fn,
  128. prefixes = ['webkit', 'moz', 'o', 'ms'];
  129. for (i = 0; i < prefixes.length && !fn; i++) {
  130. fn = window[prefixes[i] + name];
  131. }
  132. return fn;
  133. }
  134. var lastTime = 0;
  135. function timeoutDefer(fn) {
  136. var time = +new Date(),
  137. timeToCall = Math.max(0, 16 - (time - lastTime));
  138. lastTime = time + timeToCall;
  139. return window.setTimeout(fn, timeToCall);
  140. }
  141. var requestFn = window.requestAnimationFrame ||
  142. getPrefixed('RequestAnimationFrame') || timeoutDefer;
  143. var cancelFn = window.cancelAnimationFrame ||
  144. getPrefixed('CancelAnimationFrame') ||
  145. getPrefixed('CancelRequestAnimationFrame') ||
  146. function (id) { window.clearTimeout(id); };
  147. L.Util.requestAnimFrame = function (fn, context, immediate, element) {
  148. fn = L.bind(fn, context);
  149. if (immediate && requestFn === timeoutDefer) {
  150. fn();
  151. } else {
  152. return requestFn.call(window, fn, element);
  153. }
  154. };
  155. L.Util.cancelAnimFrame = function (id) {
  156. if (id) {
  157. cancelFn.call(window, id);
  158. }
  159. };
  160. }());
  161. // shortcuts for most used utility functions
  162. L.extend = L.Util.extend;
  163. L.bind = L.Util.bind;
  164. L.stamp = L.Util.stamp;
  165. L.setOptions = L.Util.setOptions;
  166. /*
  167. * L.Class powers the OOP facilities of the library.
  168. * Thanks to John Resig and Dean Edwards for inspiration!
  169. */
  170. L.Class = function () {};
  171. L.Class.extend = function (props) {
  172. // extended class with the new prototype
  173. var NewClass = function () {
  174. // call the constructor
  175. if (this.initialize) {
  176. this.initialize.apply(this, arguments);
  177. }
  178. // call all constructor hooks
  179. if (this._initHooks) {
  180. this.callInitHooks();
  181. }
  182. };
  183. // instantiate class without calling constructor
  184. var F = function () {};
  185. F.prototype = this.prototype;
  186. var proto = new F();
  187. proto.constructor = NewClass;
  188. NewClass.prototype = proto;
  189. //inherit parent's statics
  190. for (var i in this) {
  191. if (this.hasOwnProperty(i) && i !== 'prototype') {
  192. NewClass[i] = this[i];
  193. }
  194. }
  195. // mix static properties into the class
  196. if (props.statics) {
  197. L.extend(NewClass, props.statics);
  198. delete props.statics;
  199. }
  200. // mix includes into the prototype
  201. if (props.includes) {
  202. L.Util.extend.apply(null, [proto].concat(props.includes));
  203. delete props.includes;
  204. }
  205. // merge options
  206. if (props.options && proto.options) {
  207. props.options = L.extend({}, proto.options, props.options);
  208. }
  209. // mix given properties into the prototype
  210. L.extend(proto, props);
  211. proto._initHooks = [];
  212. var parent = this;
  213. // jshint camelcase: false
  214. NewClass.__super__ = parent.prototype;
  215. // add method for calling all hooks
  216. proto.callInitHooks = function () {
  217. if (this._initHooksCalled) { return; }
  218. if (parent.prototype.callInitHooks) {
  219. parent.prototype.callInitHooks.call(this);
  220. }
  221. this._initHooksCalled = true;
  222. for (var i = 0, len = proto._initHooks.length; i < len; i++) {
  223. proto._initHooks[i].call(this);
  224. }
  225. };
  226. return NewClass;
  227. };
  228. // method for adding properties to prototype
  229. L.Class.include = function (props) {
  230. L.extend(this.prototype, props);
  231. };
  232. // merge new default options to the Class
  233. L.Class.mergeOptions = function (options) {
  234. L.extend(this.prototype.options, options);
  235. };
  236. // add a constructor hook
  237. L.Class.addInitHook = function (fn) { // (Function) || (String, args...)
  238. var args = Array.prototype.slice.call(arguments, 1);
  239. var init = typeof fn === 'function' ? fn : function () {
  240. this[fn].apply(this, args);
  241. };
  242. this.prototype._initHooks = this.prototype._initHooks || [];
  243. this.prototype._initHooks.push(init);
  244. };
  245. /*
  246. * L.Mixin.Events is used to add custom events functionality to Leaflet classes.
  247. */
  248. var eventsKey = '_leaflet_events';
  249. L.Mixin = {};
  250. L.Mixin.Events = {
  251. addEventListener: function (types, fn, context) { // (String, Function[, Object]) or (Object[, Object])
  252. // types can be a map of types/handlers
  253. if (L.Util.invokeEach(types, this.addEventListener, this, fn, context)) { return this; }
  254. var events = this[eventsKey] = this[eventsKey] || {},
  255. contextId = context && L.stamp(context),
  256. i, len, event, type, indexKey, indexLenKey, typeIndex;
  257. // types can be a string of space-separated words
  258. types = L.Util.splitWords(types);
  259. for (i = 0, len = types.length; i < len; i++) {
  260. event = {
  261. action: fn,
  262. context: context || this
  263. };
  264. type = types[i];
  265. if (context) {
  266. // store listeners of a particular context in a separate hash (if it has an id)
  267. // gives a major performance boost when removing thousands of map layers
  268. indexKey = type + '_idx';
  269. indexLenKey = indexKey + '_len';
  270. typeIndex = events[indexKey] = events[indexKey] || {};
  271. if (!typeIndex[contextId]) {
  272. typeIndex[contextId] = [];
  273. // keep track of the number of keys in the index to quickly check if it's empty
  274. events[indexLenKey] = (events[indexLenKey] || 0) + 1;
  275. }
  276. typeIndex[contextId].push(event);
  277. } else {
  278. events[type] = events[type] || [];
  279. events[type].push(event);
  280. }
  281. }
  282. return this;
  283. },
  284. hasEventListeners: function (type) { // (String) -> Boolean
  285. var events = this[eventsKey];
  286. return !!events && ((type in events && events[type].length > 0) ||
  287. (type + '_idx' in events && events[type + '_idx_len'] > 0));
  288. },
  289. removeEventListener: function (types, fn, context) { // ([String, Function, Object]) or (Object[, Object])
  290. if (!this[eventsKey]) {
  291. return this;
  292. }
  293. if (!types) {
  294. return this.clearAllEventListeners();
  295. }
  296. if (L.Util.invokeEach(types, this.removeEventListener, this, fn, context)) { return this; }
  297. var events = this[eventsKey],
  298. contextId = context && L.stamp(context),
  299. i, len, type, listeners, j, indexKey, indexLenKey, typeIndex, removed;
  300. types = L.Util.splitWords(types);
  301. for (i = 0, len = types.length; i < len; i++) {
  302. type = types[i];
  303. indexKey = type + '_idx';
  304. indexLenKey = indexKey + '_len';
  305. typeIndex = events[indexKey];
  306. if (!fn) {
  307. // clear all listeners for a type if function isn't specified
  308. delete events[type];
  309. delete events[indexKey];
  310. } else {
  311. listeners = context && typeIndex ? typeIndex[contextId] : events[type];
  312. if (listeners) {
  313. for (j = listeners.length - 1; j >= 0; j--) {
  314. if ((listeners[j].action === fn) && (!context || (listeners[j].context === context))) {
  315. removed = listeners.splice(j, 1);
  316. // set the old action to a no-op, because it is possible
  317. // that the listener is being iterated over as part of a dispatch
  318. removed[0].action = L.Util.falseFn;
  319. }
  320. }
  321. if (context && typeIndex && (listeners.length === 0)) {
  322. delete typeIndex[contextId];
  323. events[indexLenKey]--;
  324. }
  325. }
  326. }
  327. }
  328. return this;
  329. },
  330. clearAllEventListeners: function () {
  331. delete this[eventsKey];
  332. return this;
  333. },
  334. fireEvent: function (type, data) { // (String[, Object])
  335. if (!this.hasEventListeners(type)) {
  336. return this;
  337. }
  338. var event = L.Util.extend({}, data, { type: type, target: this });
  339. var events = this[eventsKey],
  340. listeners, i, len, typeIndex, contextId;
  341. if (events[type]) {
  342. // make sure adding/removing listeners inside other listeners won't cause infinite loop
  343. listeners = events[type].slice();
  344. for (i = 0, len = listeners.length; i < len; i++) {
  345. listeners[i].action.call(listeners[i].context || this, event);
  346. }
  347. }
  348. // fire event for the context-indexed listeners as well
  349. typeIndex = events[type + '_idx'];
  350. for (contextId in typeIndex) {
  351. listeners = typeIndex[contextId].slice();
  352. if (listeners) {
  353. for (i = 0, len = listeners.length; i < len; i++) {
  354. listeners[i].action.call(listeners[i].context || this, event);
  355. }
  356. }
  357. }
  358. return this;
  359. },
  360. addOneTimeEventListener: function (types, fn, context) {
  361. if (L.Util.invokeEach(types, this.addOneTimeEventListener, this, fn, context)) { return this; }
  362. var handler = L.bind(function () {
  363. this
  364. .removeEventListener(types, fn, context)
  365. .removeEventListener(types, handler, context);
  366. }, this);
  367. return this
  368. .addEventListener(types, fn, context)
  369. .addEventListener(types, handler, context);
  370. }
  371. };
  372. L.Mixin.Events.on = L.Mixin.Events.addEventListener;
  373. L.Mixin.Events.off = L.Mixin.Events.removeEventListener;
  374. L.Mixin.Events.once = L.Mixin.Events.addOneTimeEventListener;
  375. L.Mixin.Events.fire = L.Mixin.Events.fireEvent;
  376. /*
  377. * L.Browser handles different browser and feature detections for internal Leaflet use.
  378. */
  379. (function () {
  380. var ie = !!window.ActiveXObject,
  381. ie6 = ie && !window.XMLHttpRequest,
  382. ie7 = ie && !document.querySelector,
  383. ielt9 = ie && !document.addEventListener,
  384. // terrible browser detection to work around Safari / iOS / Android browser bugs
  385. ua = navigator.userAgent.toLowerCase(),
  386. webkit = ua.indexOf('webkit') !== -1,
  387. chrome = ua.indexOf('chrome') !== -1,
  388. phantomjs = ua.indexOf('phantom') !== -1,
  389. android = ua.indexOf('android') !== -1,
  390. android23 = ua.search('android [23]') !== -1,
  391. mobile = typeof orientation !== undefined + '',
  392. msTouch = window.navigator && window.navigator.msPointerEnabled &&
  393. window.navigator.msMaxTouchPoints,
  394. retina = ('devicePixelRatio' in window && window.devicePixelRatio > 1) ||
  395. ('matchMedia' in window && window.matchMedia('(min-resolution:144dpi)') &&
  396. window.matchMedia('(min-resolution:144dpi)').matches),
  397. doc = document.documentElement,
  398. ie3d = ie && ('transition' in doc.style),
  399. webkit3d = ('WebKitCSSMatrix' in window) && ('m11' in new window.WebKitCSSMatrix()),
  400. gecko3d = 'MozPerspective' in doc.style,
  401. opera3d = 'OTransition' in doc.style,
  402. any3d = !window.L_DISABLE_3D && (ie3d || webkit3d || gecko3d || opera3d) && !phantomjs;
  403. // PhantomJS has 'ontouchstart' in document.documentElement, but doesn't actually support touch.
  404. // https://github.com/Leaflet/Leaflet/pull/1434#issuecomment-13843151
  405. var touch = !window.L_NO_TOUCH && !phantomjs && (function () {
  406. var startName = 'ontouchstart';
  407. // IE10+ (We simulate these into touch* events in L.DomEvent and L.DomEvent.MsTouch) or WebKit, etc.
  408. if (msTouch || (startName in doc)) {
  409. return true;
  410. }
  411. // Firefox/Gecko
  412. var div = document.createElement('div'),
  413. supported = false;
  414. if (!div.setAttribute) {
  415. return false;
  416. }
  417. div.setAttribute(startName, 'return;');
  418. if (typeof div[startName] === 'function') {
  419. supported = true;
  420. }
  421. div.removeAttribute(startName);
  422. div = null;
  423. return supported;
  424. }());
  425. L.Browser = {
  426. ie: ie,
  427. ie6: ie6,
  428. ie7: ie7,
  429. ielt9: ielt9,
  430. webkit: webkit,
  431. android: android,
  432. android23: android23,
  433. chrome: chrome,
  434. ie3d: ie3d,
  435. webkit3d: webkit3d,
  436. gecko3d: gecko3d,
  437. opera3d: opera3d,
  438. any3d: any3d,
  439. mobile: mobile,
  440. mobileWebkit: mobile && webkit,
  441. mobileWebkit3d: mobile && webkit3d,
  442. mobileOpera: mobile && window.opera,
  443. touch: touch,
  444. msTouch: msTouch,
  445. retina: retina
  446. };
  447. }());
  448. /*
  449. * L.Point represents a point with x and y coordinates.
  450. */
  451. L.Point = function (/*Number*/ x, /*Number*/ y, /*Boolean*/ round) {
  452. this.x = (round ? Math.round(x) : x);
  453. this.y = (round ? Math.round(y) : y);
  454. };
  455. L.Point.prototype = {
  456. clone: function () {
  457. return new L.Point(this.x, this.y);
  458. },
  459. // non-destructive, returns a new point
  460. add: function (point) {
  461. return this.clone()._add(L.point(point));
  462. },
  463. // destructive, used directly for performance in situations where it's safe to modify existing point
  464. _add: function (point) {
  465. this.x += point.x;
  466. this.y += point.y;
  467. return this;
  468. },
  469. subtract: function (point) {
  470. return this.clone()._subtract(L.point(point));
  471. },
  472. _subtract: function (point) {
  473. this.x -= point.x;
  474. this.y -= point.y;
  475. return this;
  476. },
  477. divideBy: function (num) {
  478. return this.clone()._divideBy(num);
  479. },
  480. _divideBy: function (num) {
  481. this.x /= num;
  482. this.y /= num;
  483. return this;
  484. },
  485. multiplyBy: function (num) {
  486. return this.clone()._multiplyBy(num);
  487. },
  488. _multiplyBy: function (num) {
  489. this.x *= num;
  490. this.y *= num;
  491. return this;
  492. },
  493. round: function () {
  494. return this.clone()._round();
  495. },
  496. _round: function () {
  497. this.x = Math.round(this.x);
  498. this.y = Math.round(this.y);
  499. return this;
  500. },
  501. floor: function () {
  502. return this.clone()._floor();
  503. },
  504. _floor: function () {
  505. this.x = Math.floor(this.x);
  506. this.y = Math.floor(this.y);
  507. return this;
  508. },
  509. distanceTo: function (point) {
  510. point = L.point(point);
  511. var x = point.x - this.x,
  512. y = point.y - this.y;
  513. return Math.sqrt(x * x + y * y);
  514. },
  515. equals: function (point) {
  516. point = L.point(point);
  517. return point.x === this.x &&
  518. point.y === this.y;
  519. },
  520. contains: function (point) {
  521. point = L.point(point);
  522. return Math.abs(point.x) <= Math.abs(this.x) &&
  523. Math.abs(point.y) <= Math.abs(this.y);
  524. },
  525. toString: function () {
  526. return 'Point(' +
  527. L.Util.formatNum(this.x) + ', ' +
  528. L.Util.formatNum(this.y) + ')';
  529. }
  530. };
  531. L.point = function (x, y, round) {
  532. if (x instanceof L.Point) {
  533. return x;
  534. }
  535. if (L.Util.isArray(x)) {
  536. return new L.Point(x[0], x[1]);
  537. }
  538. if (x === undefined || x === null) {
  539. return x;
  540. }
  541. return new L.Point(x, y, round);
  542. };
  543. /*
  544. * L.Bounds represents a rectangular area on the screen in pixel coordinates.
  545. */
  546. L.Bounds = function (a, b) { //(Point, Point) or Point[]
  547. if (!a) { return; }
  548. var points = b ? [a, b] : a;
  549. for (var i = 0, len = points.length; i < len; i++) {
  550. this.extend(points[i]);
  551. }
  552. };
  553. L.Bounds.prototype = {
  554. // extend the bounds to contain the given point
  555. extend: function (point) { // (Point)
  556. point = L.point(point);
  557. if (!this.min && !this.max) {
  558. this.min = point.clone();
  559. this.max = point.clone();
  560. } else {
  561. this.min.x = Math.min(point.x, this.min.x);
  562. this.max.x = Math.max(point.x, this.max.x);
  563. this.min.y = Math.min(point.y, this.min.y);
  564. this.max.y = Math.max(point.y, this.max.y);
  565. }
  566. return this;
  567. },
  568. getCenter: function (round) { // (Boolean) -> Point
  569. return new L.Point(
  570. (this.min.x + this.max.x) / 2,
  571. (this.min.y + this.max.y) / 2, round);
  572. },
  573. getBottomLeft: function () { // -> Point
  574. return new L.Point(this.min.x, this.max.y);
  575. },
  576. getTopRight: function () { // -> Point
  577. return new L.Point(this.max.x, this.min.y);
  578. },
  579. getSize: function () {
  580. return this.max.subtract(this.min);
  581. },
  582. contains: function (obj) { // (Bounds) or (Point) -> Boolean
  583. var min, max;
  584. if (typeof obj[0] === 'number' || obj instanceof L.Point) {
  585. obj = L.point(obj);
  586. } else {
  587. obj = L.bounds(obj);
  588. }
  589. if (obj instanceof L.Bounds) {
  590. min = obj.min;
  591. max = obj.max;
  592. } else {
  593. min = max = obj;
  594. }
  595. return (min.x >= this.min.x) &&
  596. (max.x <= this.max.x) &&
  597. (min.y >= this.min.y) &&
  598. (max.y <= this.max.y);
  599. },
  600. intersects: function (bounds) { // (Bounds) -> Boolean
  601. bounds = L.bounds(bounds);
  602. var min = this.min,
  603. max = this.max,
  604. min2 = bounds.min,
  605. max2 = bounds.max,
  606. xIntersects = (max2.x >= min.x) && (min2.x <= max.x),
  607. yIntersects = (max2.y >= min.y) && (min2.y <= max.y);
  608. return xIntersects && yIntersects;
  609. },
  610. isValid: function () {
  611. return !!(this.min && this.max);
  612. }
  613. };
  614. L.bounds = function (a, b) { // (Bounds) or (Point, Point) or (Point[])
  615. if (!a || a instanceof L.Bounds) {
  616. return a;
  617. }
  618. return new L.Bounds(a, b);
  619. };
  620. /*
  621. * L.Transformation is an utility class to perform simple point transformations through a 2d-matrix.
  622. */
  623. L.Transformation = function (a, b, c, d) {
  624. this._a = a;
  625. this._b = b;
  626. this._c = c;
  627. this._d = d;
  628. };
  629. L.Transformation.prototype = {
  630. transform: function (point, scale) { // (Point, Number) -> Point
  631. return this._transform(point.clone(), scale);
  632. },
  633. // destructive transform (faster)
  634. _transform: function (point, scale) {
  635. scale = scale || 1;
  636. point.x = scale * (this._a * point.x + this._b);
  637. point.y = scale * (this._c * point.y + this._d);
  638. return point;
  639. },
  640. untransform: function (point, scale) {
  641. scale = scale || 1;
  642. return new L.Point(
  643. (point.x / scale - this._b) / this._a,
  644. (point.y / scale - this._d) / this._c);
  645. }
  646. };
  647. /*
  648. * L.DomUtil contains various utility functions for working with DOM.
  649. */
  650. L.DomUtil = {
  651. get: function (id) {
  652. return (typeof id === 'string' ? document.getElementById(id) : id);
  653. },
  654. getStyle: function (el, style) {
  655. var value = el.style[style];
  656. if (!value && el.currentStyle) {
  657. value = el.currentStyle[style];
  658. }
  659. if ((!value || value === 'auto') && document.defaultView) {
  660. var css = document.defaultView.getComputedStyle(el, null);
  661. value = css ? css[style] : null;
  662. }
  663. return value === 'auto' ? null : value;
  664. },
  665. getViewportOffset: function (element) {
  666. var top = 0,
  667. left = 0,
  668. el = element,
  669. docBody = document.body,
  670. docEl = document.documentElement,
  671. pos,
  672. ie7 = L.Browser.ie7;
  673. do {
  674. top += el.offsetTop || 0;
  675. left += el.offsetLeft || 0;
  676. //add borders
  677. top += parseInt(L.DomUtil.getStyle(el, 'borderTopWidth'), 10) || 0;
  678. left += parseInt(L.DomUtil.getStyle(el, 'borderLeftWidth'), 10) || 0;
  679. pos = L.DomUtil.getStyle(el, 'position');
  680. if (el.offsetParent === docBody && pos === 'absolute') { break; }
  681. if (pos === 'fixed') {
  682. top += docBody.scrollTop || docEl.scrollTop || 0;
  683. left += docBody.scrollLeft || docEl.scrollLeft || 0;
  684. break;
  685. }
  686. if (pos === 'relative' && !el.offsetLeft) {
  687. var width = L.DomUtil.getStyle(el, 'width'),
  688. maxWidth = L.DomUtil.getStyle(el, 'max-width'),
  689. r = el.getBoundingClientRect();
  690. if (width !== 'none' || maxWidth !== 'none') {
  691. left += r.left + el.clientLeft;
  692. }
  693. //calculate full y offset since we're breaking out of the loop
  694. top += r.top + (docBody.scrollTop || docEl.scrollTop || 0);
  695. break;
  696. }
  697. el = el.offsetParent;
  698. } while (el);
  699. el = element;
  700. do {
  701. if (el === docBody) { break; }
  702. top -= el.scrollTop || 0;
  703. left -= el.scrollLeft || 0;
  704. // webkit (and ie <= 7) handles RTL scrollLeft different to everyone else
  705. // https://code.google.com/p/closure-library/source/browse/trunk/closure/goog/style/bidi.js
  706. if (!L.DomUtil.documentIsLtr() && (L.Browser.webkit || ie7)) {
  707. left += el.scrollWidth - el.clientWidth;
  708. // ie7 shows the scrollbar by default and provides clientWidth counting it, so we
  709. // need to add it back in if it is visible; scrollbar is on the left as we are RTL
  710. if (ie7 && L.DomUtil.getStyle(el, 'overflow-y') !== 'hidden' &&
  711. L.DomUtil.getStyle(el, 'overflow') !== 'hidden') {
  712. left += 17;
  713. }
  714. }
  715. el = el.parentNode;
  716. } while (el);
  717. return new L.Point(left, top);
  718. },
  719. documentIsLtr: function () {
  720. if (!L.DomUtil._docIsLtrCached) {
  721. L.DomUtil._docIsLtrCached = true;
  722. L.DomUtil._docIsLtr = L.DomUtil.getStyle(document.body, 'direction') === 'ltr';
  723. }
  724. return L.DomUtil._docIsLtr;
  725. },
  726. create: function (tagName, className, container) {
  727. var el = document.createElement(tagName);
  728. el.className = className;
  729. if (container) {
  730. container.appendChild(el);
  731. }
  732. return el;
  733. },
  734. hasClass: function (el, name) {
  735. return (el.className.length > 0) &&
  736. new RegExp('(^|\\s)' + name + '(\\s|$)').test(el.className);
  737. },
  738. addClass: function (el, name) {
  739. if (!L.DomUtil.hasClass(el, name)) {
  740. el.className += (el.className ? ' ' : '') + name;
  741. }
  742. },
  743. removeClass: function (el, name) {
  744. el.className = L.Util.trim((' ' + el.className + ' ').replace(' ' + name + ' ', ' '));
  745. },
  746. setOpacity: function (el, value) {
  747. if ('opacity' in el.style) {
  748. el.style.opacity = value;
  749. } else if ('filter' in el.style) {
  750. var filter = false,
  751. filterName = 'DXImageTransform.Microsoft.Alpha';
  752. // filters collection throws an error if we try to retrieve a filter that doesn't exist
  753. try {
  754. filter = el.filters.item(filterName);
  755. } catch (e) {
  756. // don't set opacity to 1 if we haven't already set an opacity,
  757. // it isn't needed and breaks transparent pngs.
  758. if (value === 1) { return; }
  759. }
  760. value = Math.round(value * 100);
  761. if (filter) {
  762. filter.Enabled = (value !== 100);
  763. filter.Opacity = value;
  764. } else {
  765. el.style.filter += ' progid:' + filterName + '(opacity=' + value + ')';
  766. }
  767. }
  768. },
  769. testProp: function (props) {
  770. var style = document.documentElement.style;
  771. for (var i = 0; i < props.length; i++) {
  772. if (props[i] in style) {
  773. return props[i];
  774. }
  775. }
  776. return false;
  777. },
  778. getTranslateString: function (point) {
  779. // on WebKit browsers (Chrome/Safari/iOS Safari/Android) using translate3d instead of translate
  780. // makes animation smoother as it ensures HW accel is used. Firefox 13 doesn't care
  781. // (same speed either way), Opera 12 doesn't support translate3d
  782. var is3d = L.Browser.webkit3d,
  783. open = 'translate' + (is3d ? '3d' : '') + '(',
  784. close = (is3d ? ',0' : '') + ')';
  785. return open + point.x + 'px,' + point.y + 'px' + close;
  786. },
  787. getScaleString: function (scale, origin) {
  788. var preTranslateStr = L.DomUtil.getTranslateString(origin.add(origin.multiplyBy(-1 * scale))),
  789. scaleStr = ' scale(' + scale + ') ';
  790. return preTranslateStr + scaleStr;
  791. },
  792. setPosition: function (el, point, disable3D) { // (HTMLElement, Point[, Boolean])
  793. // jshint camelcase: false
  794. el._leaflet_pos = point;
  795. if (!disable3D && L.Browser.any3d) {
  796. el.style[L.DomUtil.TRANSFORM] = L.DomUtil.getTranslateString(point);
  797. // workaround for Android 2/3 stability (https://github.com/CloudMade/Leaflet/issues/69)
  798. if (L.Browser.mobileWebkit3d) {
  799. el.style.WebkitBackfaceVisibility = 'hidden';
  800. }
  801. } else {
  802. el.style.left = point.x + 'px';
  803. el.style.top = point.y + 'px';
  804. }
  805. },
  806. getPosition: function (el) {
  807. // this method is only used for elements previously positioned using setPosition,
  808. // so it's safe to cache the position for performance
  809. // jshint camelcase: false
  810. return el._leaflet_pos;
  811. }
  812. };
  813. // prefix style property names
  814. L.DomUtil.TRANSFORM = L.DomUtil.testProp(
  815. ['transform', 'WebkitTransform', 'OTransform', 'MozTransform', 'msTransform']);
  816. // webkitTransition comes first because some browser versions that drop vendor prefix don't do
  817. // the same for the transitionend event, in particular the Android 4.1 stock browser
  818. L.DomUtil.TRANSITION = L.DomUtil.testProp(
  819. ['webkitTransition', 'transition', 'OTransition', 'MozTransition', 'msTransition']);
  820. L.DomUtil.TRANSITION_END =
  821. L.DomUtil.TRANSITION === 'webkitTransition' || L.DomUtil.TRANSITION === 'OTransition' ?
  822. L.DomUtil.TRANSITION + 'End' : 'transitionend';
  823. (function () {
  824. var userSelectProperty = L.DomUtil.testProp(
  825. ['userSelect', 'WebkitUserSelect', 'OUserSelect', 'MozUserSelect', 'msUserSelect']);
  826. L.extend(L.DomUtil, {
  827. disableTextSelection: function () {
  828. L.DomEvent.on(window, 'selectstart', L.DomEvent.preventDefault);
  829. if (userSelectProperty) {
  830. var style = document.documentElement.style;
  831. this._userSelect = style[userSelectProperty];
  832. style[userSelectProperty] = 'none';
  833. }
  834. },
  835. enableTextSelection: function () {
  836. L.DomEvent.off(window, 'selectstart', L.DomEvent.preventDefault);
  837. if (userSelectProperty) {
  838. document.documentElement.style[userSelectProperty] = this._userSelect;
  839. delete this._userSelect;
  840. }
  841. },
  842. disableImageDrag: function () {
  843. L.DomEvent.on(window, 'dragstart', L.DomEvent.preventDefault);
  844. },
  845. enableImageDrag: function () {
  846. L.DomEvent.off(window, 'dragstart', L.DomEvent.preventDefault);
  847. }
  848. });
  849. })();
  850. /*
  851. * L.LatLng represents a geographical point with latitude and longitude coordinates.
  852. */
  853. L.LatLng = function (rawLat, rawLng) { // (Number, Number)
  854. var lat = parseFloat(rawLat),
  855. lng = parseFloat(rawLng);
  856. if (isNaN(lat) || isNaN(lng)) {
  857. throw new Error('Invalid LatLng object: (' + rawLat + ', ' + rawLng + ')');
  858. }
  859. this.lat = lat;
  860. this.lng = lng;
  861. };
  862. L.extend(L.LatLng, {
  863. DEG_TO_RAD: Math.PI / 180,
  864. RAD_TO_DEG: 180 / Math.PI,
  865. MAX_MARGIN: 1.0E-9 // max margin of error for the "equals" check
  866. });
  867. L.LatLng.prototype = {
  868. equals: function (obj) { // (LatLng) -> Boolean
  869. if (!obj) { return false; }
  870. obj = L.latLng(obj);
  871. var margin = Math.max(
  872. Math.abs(this.lat - obj.lat),
  873. Math.abs(this.lng - obj.lng));
  874. return margin <= L.LatLng.MAX_MARGIN;
  875. },
  876. toString: function (precision) { // (Number) -> String
  877. return 'LatLng(' +
  878. L.Util.formatNum(this.lat, precision) + ', ' +
  879. L.Util.formatNum(this.lng, precision) + ')';
  880. },
  881. // Haversine distance formula, see http://en.wikipedia.org/wiki/Haversine_formula
  882. // TODO move to projection code, LatLng shouldn't know about Earth
  883. distanceTo: function (other) { // (LatLng) -> Number
  884. other = L.latLng(other);
  885. var R = 6378137, // earth radius in meters
  886. d2r = L.LatLng.DEG_TO_RAD,
  887. dLat = (other.lat - this.lat) * d2r,
  888. dLon = (other.lng - this.lng) * d2r,
  889. lat1 = this.lat * d2r,
  890. lat2 = other.lat * d2r,
  891. sin1 = Math.sin(dLat / 2),
  892. sin2 = Math.sin(dLon / 2);
  893. var a = sin1 * sin1 + sin2 * sin2 * Math.cos(lat1) * Math.cos(lat2);
  894. return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  895. },
  896. wrap: function (a, b) { // (Number, Number) -> LatLng
  897. var lng = this.lng;
  898. a = a || -180;
  899. b = b || 180;
  900. lng = (lng + b) % (b - a) + (lng < a || lng === b ? b : a);
  901. return new L.LatLng(this.lat, lng);
  902. }
  903. };
  904. L.latLng = function (a, b) { // (LatLng) or ([Number, Number]) or (Number, Number)
  905. if (a instanceof L.LatLng) {
  906. return a;
  907. }
  908. if (L.Util.isArray(a)) {
  909. return new L.LatLng(a[0], a[1]);
  910. }
  911. if (a === undefined || a === null) {
  912. return a;
  913. }
  914. if (typeof a === 'object' && 'lat' in a) {
  915. return new L.LatLng(a.lat, 'lng' in a ? a.lng : a.lon);
  916. }
  917. return new L.LatLng(a, b);
  918. };
  919. /*
  920. * L.LatLngBounds represents a rectangular area on the map in geographical coordinates.
  921. */
  922. L.LatLngBounds = function (southWest, northEast) { // (LatLng, LatLng) or (LatLng[])
  923. if (!southWest) { return; }
  924. var latlngs = northEast ? [southWest, northEast] : southWest;
  925. for (var i = 0, len = latlngs.length; i < len; i++) {
  926. this.extend(latlngs[i]);
  927. }
  928. };
  929. L.LatLngBounds.prototype = {
  930. // extend the bounds to contain the given point or bounds
  931. extend: function (obj) { // (LatLng) or (LatLngBounds)
  932. if (!obj) { return this; }
  933. if (typeof obj[0] === 'number' || typeof obj[0] === 'string' || obj instanceof L.LatLng) {
  934. obj = L.latLng(obj);
  935. } else {
  936. obj = L.latLngBounds(obj);
  937. }
  938. if (obj instanceof L.LatLng) {
  939. if (!this._southWest && !this._northEast) {
  940. this._southWest = new L.LatLng(obj.lat, obj.lng);
  941. this._northEast = new L.LatLng(obj.lat, obj.lng);
  942. } else {
  943. this._southWest.lat = Math.min(obj.lat, this._southWest.lat);
  944. this._southWest.lng = Math.min(obj.lng, this._southWest.lng);
  945. this._northEast.lat = Math.max(obj.lat, this._northEast.lat);
  946. this._northEast.lng = Math.max(obj.lng, this._northEast.lng);
  947. }
  948. } else if (obj instanceof L.LatLngBounds) {
  949. this.extend(obj._southWest);
  950. this.extend(obj._northEast);
  951. }
  952. return this;
  953. },
  954. // extend the bounds by a percentage
  955. pad: function (bufferRatio) { // (Number) -> LatLngBounds
  956. var sw = this._southWest,
  957. ne = this._northEast,
  958. heightBuffer = Math.abs(sw.lat - ne.lat) * bufferRatio,
  959. widthBuffer = Math.abs(sw.lng - ne.lng) * bufferRatio;
  960. return new L.LatLngBounds(
  961. new L.LatLng(sw.lat - heightBuffer, sw.lng - widthBuffer),
  962. new L.LatLng(ne.lat + heightBuffer, ne.lng + widthBuffer));
  963. },
  964. getCenter: function () { // -> LatLng
  965. return new L.LatLng(
  966. (this._southWest.lat + this._northEast.lat) / 2,
  967. (this._southWest.lng + this._northEast.lng) / 2);
  968. },
  969. getSouthWest: function () {
  970. return this._southWest;
  971. },
  972. getNorthEast: function () {
  973. return this._northEast;
  974. },
  975. getNorthWest: function () {
  976. return new L.LatLng(this.getNorth(), this.getWest());
  977. },
  978. getSouthEast: function () {
  979. return new L.LatLng(this.getSouth(), this.getEast());
  980. },
  981. getWest: function () {
  982. return this._southWest.lng;
  983. },
  984. getSouth: function () {
  985. return this._southWest.lat;
  986. },
  987. getEast: function () {
  988. return this._northEast.lng;
  989. },
  990. getNorth: function () {
  991. return this._northEast.lat;
  992. },
  993. contains: function (obj) { // (LatLngBounds) or (LatLng) -> Boolean
  994. if (typeof obj[0] === 'number' || obj instanceof L.LatLng) {
  995. obj = L.latLng(obj);
  996. } else {
  997. obj = L.latLngBounds(obj);
  998. }
  999. var sw = this._southWest,
  1000. ne = this._northEast,
  1001. sw2, ne2;
  1002. if (obj instanceof L.LatLngBounds) {
  1003. sw2 = obj.getSouthWest();
  1004. ne2 = obj.getNorthEast();
  1005. } else {
  1006. sw2 = ne2 = obj;
  1007. }
  1008. return (sw2.lat >= sw.lat) && (ne2.lat <= ne.lat) &&
  1009. (sw2.lng >= sw.lng) && (ne2.lng <= ne.lng);
  1010. },
  1011. intersects: function (bounds) { // (LatLngBounds)
  1012. bounds = L.latLngBounds(bounds);
  1013. var sw = this._southWest,
  1014. ne = this._northEast,
  1015. sw2 = bounds.getSouthWest(),
  1016. ne2 = bounds.getNorthEast(),
  1017. latIntersects = (ne2.lat >= sw.lat) && (sw2.lat <= ne.lat),
  1018. lngIntersects = (ne2.lng >= sw.lng) && (sw2.lng <= ne.lng);
  1019. return latIntersects && lngIntersects;
  1020. },
  1021. toBBoxString: function () {
  1022. return [this.getWest(), this.getSouth(), this.getEast(), this.getNorth()].join(',');
  1023. },
  1024. equals: function (bounds) { // (LatLngBounds)
  1025. if (!bounds) { return false; }
  1026. bounds = L.latLngBounds(bounds);
  1027. return this._southWest.equals(bounds.getSouthWest()) &&
  1028. this._northEast.equals(bounds.getNorthEast());
  1029. },
  1030. isValid: function () {
  1031. return !!(this._southWest && this._northEast);
  1032. }
  1033. };
  1034. //TODO International date line?
  1035. L.latLngBounds = function (a, b) { // (LatLngBounds) or (LatLng, LatLng)
  1036. if (!a || a instanceof L.LatLngBounds) {
  1037. return a;
  1038. }
  1039. return new L.LatLngBounds(a, b);
  1040. };
  1041. /*
  1042. * L.Projection contains various geographical projections used by CRS classes.
  1043. */
  1044. L.Projection = {};
  1045. /*
  1046. * Spherical Mercator is the most popular map projection, used by EPSG:3857 CRS used by default.
  1047. */
  1048. L.Projection.SphericalMercator = {
  1049. MAX_LATITUDE: 85.0511287798,
  1050. project: function (latlng) { // (LatLng) -> Point
  1051. var d = L.LatLng.DEG_TO_RAD,
  1052. max = this.MAX_LATITUDE,
  1053. lat = Math.max(Math.min(max, latlng.lat), -max),
  1054. x = latlng.lng * d,
  1055. y = lat * d;
  1056. y = Math.log(Math.tan((Math.PI / 4) + (y / 2)));
  1057. return new L.Point(x, y);
  1058. },
  1059. unproject: function (point) { // (Point, Boolean) -> LatLng
  1060. var d = L.LatLng.RAD_TO_DEG,
  1061. lng = point.x * d,
  1062. lat = (2 * Math.atan(Math.exp(point.y)) - (Math.PI / 2)) * d;
  1063. return new L.LatLng(lat, lng);
  1064. }
  1065. };
  1066. /*
  1067. * Simple equirectangular (Plate Carree) projection, used by CRS like EPSG:4326 and Simple.
  1068. */
  1069. L.Projection.LonLat = {
  1070. project: function (latlng) {
  1071. return new L.Point(latlng.lng, latlng.lat);
  1072. },
  1073. unproject: function (point) {
  1074. return new L.LatLng(point.y, point.x);
  1075. }
  1076. };
  1077. /*
  1078. * L.CRS is a base object for all defined CRS (Coordinate Reference Systems) in Leaflet.
  1079. */
  1080. L.CRS = {
  1081. latLngToPoint: function (latlng, zoom) { // (LatLng, Number) -> Point
  1082. var projectedPoint = this.projection.project(latlng),
  1083. scale = this.scale(zoom);
  1084. return this.transformation._transform(projectedPoint, scale);
  1085. },
  1086. pointToLatLng: function (point, zoom) { // (Point, Number[, Boolean]) -> LatLng
  1087. var scale = this.scale(zoom),
  1088. untransformedPoint = this.transformation.untransform(point, scale);
  1089. return this.projection.unproject(untransformedPoint);
  1090. },
  1091. project: function (latlng) {
  1092. return this.projection.project(latlng);
  1093. },
  1094. scale: function (zoom) {
  1095. return 256 * Math.pow(2, zoom);
  1096. }
  1097. };
  1098. /*
  1099. * A simple CRS that can be used for flat non-Earth maps like panoramas or game maps.
  1100. */
  1101. L.CRS.Simple = L.extend({}, L.CRS, {
  1102. projection: L.Projection.LonLat,
  1103. transformation: new L.Transformation(1, 0, -1, 0),
  1104. scale: function (zoom) {
  1105. return Math.pow(2, zoom);
  1106. }
  1107. });
  1108. /*
  1109. * L.CRS.EPSG3857 (Spherical Mercator) is the most common CRS for web mapping
  1110. * and is used by Leaflet by default.
  1111. */
  1112. L.CRS.EPSG3857 = L.extend({}, L.CRS, {
  1113. code: 'EPSG:3857',
  1114. projection: L.Projection.SphericalMercator,
  1115. transformation: new L.Transformation(0.5 / Math.PI, 0.5, -0.5 / Math.PI, 0.5),
  1116. project: function (latlng) { // (LatLng) -> Point
  1117. var projectedPoint = this.projection.project(latlng),
  1118. earthRadius = 6378137;
  1119. return projectedPoint.multiplyBy(earthRadius);
  1120. }
  1121. });
  1122. L.CRS.EPSG900913 = L.extend({}, L.CRS.EPSG3857, {
  1123. code: 'EPSG:900913'
  1124. });
  1125. /*
  1126. * L.CRS.EPSG4326 is a CRS popular among advanced GIS specialists.
  1127. */
  1128. L.CRS.EPSG4326 = L.extend({}, L.CRS, {
  1129. code: 'EPSG:4326',
  1130. projection: L.Projection.LonLat,
  1131. transformation: new L.Transformation(1 / 360, 0.5, -1 / 360, 0.5)
  1132. });
  1133. /*
  1134. * L.Map is the central class of the API - it is used to create a map.
  1135. */
  1136. L.Map = L.Class.extend({
  1137. includes: L.Mixin.Events,
  1138. options: {
  1139. crs: L.CRS.EPSG3857,
  1140. /*
  1141. center: LatLng,
  1142. zoom: Number,
  1143. layers: Array,
  1144. */
  1145. fadeAnimation: L.DomUtil.TRANSITION && !L.Browser.android23,
  1146. trackResize: true,
  1147. markerZoomAnimation: L.DomUtil.TRANSITION && L.Browser.any3d
  1148. },
  1149. initialize: function (id, options) { // (HTMLElement or String, Object)
  1150. options = L.setOptions(this, options);
  1151. this._initContainer(id);
  1152. this._initLayout();
  1153. this._initEvents();
  1154. if (options.maxBounds) {
  1155. this.setMaxBounds(options.maxBounds);
  1156. }
  1157. if (options.center && options.zoom !== undefined) {
  1158. this.setView(L.latLng(options.center), options.zoom, {reset: true});
  1159. }
  1160. this._handlers = [];
  1161. this._layers = {};
  1162. this._zoomBoundLayers = {};
  1163. this._tileLayersNum = 0;
  1164. this.callInitHooks();
  1165. this._addLayers(options.layers);
  1166. },
  1167. // public methods that modify map state
  1168. // replaced by animation-powered implementation in Map.PanAnimation.js
  1169. setView: function (center, zoom) {
  1170. this._resetView(L.latLng(center), this._limitZoom(zoom));
  1171. return this;
  1172. },
  1173. setZoom: function (zoom, options) {
  1174. return this.setView(this.getCenter(), zoom, {zoom: options});
  1175. },
  1176. zoomIn: function (delta, options) {
  1177. return this.setZoom(this._zoom + (delta || 1), options);
  1178. },
  1179. zoomOut: function (delta, options) {
  1180. return this.setZoom(this._zoom - (delta || 1), options);
  1181. },
  1182. setZoomAround: function (latlng, zoom, options) {
  1183. var scale = this.getZoomScale(zoom),
  1184. viewHalf = this.getSize().divideBy(2),
  1185. containerPoint = latlng instanceof L.Point ? latlng : this.latLngToContainerPoint(latlng),
  1186. centerOffset = containerPoint.subtract(viewHalf).multiplyBy(1 - 1 / scale),
  1187. newCenter = this.containerPointToLatLng(viewHalf.add(centerOffset));
  1188. return this.setView(newCenter, zoom, {zoom: options});
  1189. },
  1190. fitBounds: function (bounds, options) {
  1191. options = options || {};
  1192. bounds = bounds.getBounds ? bounds.getBounds() : L.latLngBounds(bounds);
  1193. var paddingTL = L.point(options.paddingTopLeft || options.padding || [0, 0]),
  1194. paddingBR = L.point(options.paddingBottomRight || options.padding || [0, 0]),
  1195. zoom = this.getBoundsZoom(bounds, false, paddingTL.add(paddingBR)),
  1196. paddingOffset = paddingBR.subtract(paddingTL).divideBy(2),
  1197. swPoint = this.project(bounds.getSouthWest(), zoom),
  1198. nePoint = this.project(bounds.getNorthEast(), zoom),
  1199. center = this.unproject(swPoint.add(nePoint).divideBy(2).add(paddingOffset), zoom);
  1200. return this.setView(center, zoom, options);
  1201. },
  1202. fitWorld: function (options) {
  1203. return this.fitBounds([[-90, -180], [90, 180]], options);
  1204. },
  1205. panTo: function (center, options) { // (LatLng)
  1206. return this.setView(center, this._zoom, {pan: options});
  1207. },
  1208. panBy: function (offset) { // (Point)
  1209. // replaced with animated panBy in Map.Animation.js
  1210. this.fire('movestart');
  1211. this._rawPanBy(L.point(offset));
  1212. this.fire('move');
  1213. return this.fire('moveend');
  1214. },
  1215. setMaxBounds: function (bounds, options) {
  1216. bounds = L.latLngBounds(bounds);
  1217. this.options.maxBounds = bounds;
  1218. if (!bounds) {
  1219. this._boundsMinZoom = null;
  1220. this.off('moveend', this._panInsideMaxBounds, this);
  1221. return this;
  1222. }
  1223. var minZoom = this.getBoundsZoom(bounds, true);
  1224. this._boundsMinZoom = minZoom;
  1225. if (this._loaded) {
  1226. if (this._zoom < minZoom) {
  1227. this.setView(bounds.getCenter(), minZoom, options);
  1228. } else {
  1229. this.panInsideBounds(bounds);
  1230. }
  1231. }
  1232. this.on('moveend', this._panInsideMaxBounds, this);
  1233. return this;
  1234. },
  1235. panInsideBounds: function (bounds) {
  1236. bounds = L.latLngBounds(bounds);
  1237. var viewBounds = this.getPixelBounds(),
  1238. viewSw = viewBounds.getBottomLeft(),
  1239. viewNe = viewBounds.getTopRight(),
  1240. sw = this.project(bounds.getSouthWest()),
  1241. ne = this.project(bounds.getNorthEast()),
  1242. dx = 0,
  1243. dy = 0;
  1244. if (viewNe.y < ne.y) { // north
  1245. dy = Math.ceil(ne.y - viewNe.y);
  1246. }
  1247. if (viewNe.x > ne.x) { // east
  1248. dx = Math.floor(ne.x - viewNe.x);
  1249. }
  1250. if (viewSw.y > sw.y) { // south
  1251. dy = Math.floor(sw.y - viewSw.y);
  1252. }
  1253. if (viewSw.x < sw.x) { // west
  1254. dx = Math.ceil(sw.x - viewSw.x);
  1255. }
  1256. if (dx || dy) {
  1257. return this.panBy([dx, dy]);
  1258. }
  1259. return this;
  1260. },
  1261. addLayer: function (layer) {
  1262. // TODO method is too big, refactor
  1263. var id = L.stamp(layer);
  1264. if (this._layers[id]) { return this; }
  1265. this._layers[id] = layer;
  1266. // TODO getMaxZoom, getMinZoom in ILayer (instead of options)
  1267. if (layer.options && (!isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom))) {
  1268. this._zoomBoundLayers[id] = layer;
  1269. this._updateZoomLevels();
  1270. }
  1271. // TODO looks ugly, refactor!!!
  1272. if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
  1273. this._tileLayersNum++;
  1274. this._tileLayersToLoad++;
  1275. layer.on('load', this._onTileLayerLoad, this);
  1276. }
  1277. if (this._loaded) {
  1278. this._layerAdd(layer);
  1279. }
  1280. return this;
  1281. },
  1282. removeLayer: function (layer) {
  1283. var id = L.stamp(layer);
  1284. if (!this._layers[id]) { return; }
  1285. if (this._loaded) {
  1286. layer.onRemove(this);
  1287. }
  1288. delete this._layers[id];
  1289. if (this._loaded) {
  1290. this.fire('layerremove', {layer: layer});
  1291. }
  1292. if (this._zoomBoundLayers[id]) {
  1293. delete this._zoomBoundLayers[id];
  1294. this._updateZoomLevels();
  1295. }
  1296. // TODO looks ugly, refactor
  1297. if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
  1298. this._tileLayersNum--;
  1299. this._tileLayersToLoad--;
  1300. layer.off('load', this._onTileLayerLoad, this);
  1301. }
  1302. return this;
  1303. },
  1304. hasLayer: function (layer) {
  1305. if (!layer) { return false; }
  1306. return (L.stamp(layer) in this._layers);
  1307. },
  1308. eachLayer: function (method, context) {
  1309. for (var i in this._layers) {
  1310. method.call(context, this._layers[i]);
  1311. }
  1312. return this;
  1313. },
  1314. invalidateSize: function (options) {
  1315. options = L.extend({
  1316. animate: false,
  1317. pan: true
  1318. }, options === true ? {animate: true} : options);
  1319. var oldSize = this.getSize();
  1320. this._sizeChanged = true;
  1321. if (this.options.maxBounds) {
  1322. this.setMaxBounds(this.options.maxBounds);
  1323. }
  1324. if (!this._loaded) { return this; }
  1325. var newSize = this.getSize(),
  1326. offset = oldSize.subtract(newSize).divideBy(2).round();
  1327. if (!offset.x && !offset.y) { return this; }
  1328. if (options.animate && options.pan) {
  1329. this.panBy(offset);
  1330. } else {
  1331. if (options.pan) {
  1332. this._rawPanBy(offset);
  1333. }
  1334. this.fire('move');
  1335. // make sure moveend is not fired too often on resize
  1336. clearTimeout(this._sizeTimer);
  1337. this._sizeTimer = setTimeout(L.bind(this.fire, this, 'moveend'), 200);
  1338. }
  1339. return this.fire('resize', {
  1340. oldSize: oldSize,
  1341. newSize: newSize
  1342. });
  1343. },
  1344. // TODO handler.addTo
  1345. addHandler: function (name, HandlerClass) {
  1346. if (!HandlerClass) { return; }
  1347. var handler = this[name] = new HandlerClass(this);
  1348. this._handlers.push(handler);
  1349. if (this.options[name]) {
  1350. handler.enable();
  1351. }
  1352. return this;
  1353. },
  1354. remove: function () {
  1355. if (this._loaded) {
  1356. this.fire('unload');
  1357. }
  1358. this._initEvents('off');
  1359. delete this._container._leaflet;
  1360. this._clearPanes();
  1361. if (this._clearControlPos) {
  1362. this._clearControlPos();
  1363. }
  1364. this._clearHandlers();
  1365. return this;
  1366. },
  1367. // public methods for getting map state
  1368. getCenter: function () { // (Boolean) -> LatLng
  1369. this._checkIfLoaded();
  1370. if (!this._moved()) {
  1371. return this._initialCenter;
  1372. }
  1373. return this.layerPointToLatLng(this._getCenterLayerPoint());
  1374. },
  1375. getZoom: function () {
  1376. return this._zoom;
  1377. },
  1378. getBounds: function () {
  1379. var bounds = this.getPixelBounds(),
  1380. sw = this.unproject(bounds.getBottomLeft()),
  1381. ne = this.unproject(bounds.getTopRight());
  1382. return new L.LatLngBounds(sw, ne);
  1383. },
  1384. getMinZoom: function () {
  1385. var z1 = this._layersMinZoom === undefined ? 0 : this._layersMinZoom,
  1386. z2 = this._boundsMinZoom === undefined ? 0 : this._boundsMinZoom;
  1387. return this.options.minZoom === undefined ? Math.max(z1, z2) : this.options.minZoom;
  1388. },
  1389. getMaxZoom: function () {
  1390. return this.options.maxZoom === undefined ?
  1391. (this._layersMaxZoom === undefined ? Infinity : this._layersMaxZoom) :
  1392. this.options.maxZoom;
  1393. },
  1394. getBoundsZoom: function (bounds, inside, padding) { // (LatLngBounds[, Boolean, Point]) -> Number
  1395. bounds = L.latLngBounds(bounds);
  1396. var zoom = this.getMinZoom() - (inside ? 1 : 0),
  1397. maxZoom = this.getMaxZoom(),
  1398. size = this.getSize(),
  1399. nw = bounds.getNorthWest(),
  1400. se = bounds.getSouthEast(),
  1401. zoomNotFound = true,
  1402. boundsSize;
  1403. padding = L.point(padding || [0, 0]);
  1404. do {
  1405. zoom++;
  1406. boundsSize = this.project(se, zoom).subtract(this.project(nw, zoom)).add(padding);
  1407. zoomNotFound = !inside ? size.contains(boundsSize) : boundsSize.x < size.x || boundsSize.y < size.y;
  1408. } while (zoomNotFound && zoom <= maxZoom);
  1409. if (zoomNotFound && inside) {
  1410. return null;
  1411. }
  1412. return inside ? zoom : zoom - 1;
  1413. },
  1414. getSize: function () {
  1415. if (!this._size || this._sizeChanged) {
  1416. this._size = new L.Point(
  1417. this._container.clientWidth,
  1418. this._container.clientHeight);
  1419. this._sizeChanged = false;
  1420. }
  1421. return this._size.clone();
  1422. },
  1423. getPixelBounds: function () {
  1424. var topLeftPoint = this._getTopLeftPoint();
  1425. return new L.Bounds(topLeftPoint, topLeftPoint.add(this.getSize()));
  1426. },
  1427. getPixelOrigin: function () {
  1428. this._checkIfLoaded();
  1429. return this._initialTopLeftPoint;
  1430. },
  1431. getPanes: function () {
  1432. return this._panes;
  1433. },
  1434. getContainer: function () {
  1435. return this._container;
  1436. },
  1437. // TODO replace with universal implementation after refactoring projections
  1438. getZoomScale: function (toZoom) {
  1439. var crs = this.options.crs;
  1440. return crs.scale(toZoom) / crs.scale(this._zoom);
  1441. },
  1442. getScaleZoom: function (scale) {
  1443. return this._zoom + (Math.log(scale) / Math.LN2);
  1444. },
  1445. // conversion methods
  1446. project: function (latlng, zoom) { // (LatLng[, Number]) -> Point
  1447. zoom = zoom === undefined ? this._zoom : zoom;
  1448. return this.options.crs.latLngToPoint(L.latLng(latlng), zoom);
  1449. },
  1450. unproject: function (point, zoom) { // (Point[, Number]) -> LatLng
  1451. zoom = zoom === undefined ? this._zoom : zoom;
  1452. return this.options.crs.pointToLatLng(L.point(point), zoom);
  1453. },
  1454. layerPointToLatLng: function (point) { // (Point)
  1455. var projectedPoint = L.point(point).add(this.getPixelOrigin());
  1456. return this.unproject(projectedPoint);
  1457. },
  1458. latLngToLayerPoint: function (latlng) { // (LatLng)
  1459. var projectedPoint = this.project(L.latLng(latlng))._round();
  1460. return projectedPoint._subtract(this.getPixelOrigin());
  1461. },
  1462. containerPointToLayerPoint: function (point) { // (Point)
  1463. return L.point(point).subtract(this._getMapPanePos());
  1464. },
  1465. layerPointToContainerPoint: function (point) { // (Point)
  1466. return L.point(point).add(this._getMapPanePos());
  1467. },
  1468. containerPointToLatLng: function (point) {
  1469. var layerPoint = this.containerPointToLayerPoint(L.point(point));
  1470. return this.layerPointToLatLng(layerPoint);
  1471. },
  1472. latLngToContainerPoint: function (latlng) {
  1473. return this.layerPointToContainerPoint(this.latLngToLayerPoint(L.latLng(latlng)));
  1474. },
  1475. mouseEventToContainerPoint: function (e) { // (MouseEvent)
  1476. return L.DomEvent.getMousePosition(e, this._container);
  1477. },
  1478. mouseEventToLayerPoint: function (e) { // (MouseEvent)
  1479. return this.containerPointToLayerPoint(this.mouseEventToContainerPoint(e));
  1480. },
  1481. mouseEventToLatLng: function (e) { // (MouseEvent)
  1482. return this.layerPointToLatLng(this.mouseEventToLayerPoint(e));
  1483. },
  1484. // map initialization methods
  1485. _initContainer: function (id) {
  1486. var container = this._container = L.DomUtil.get(id);
  1487. if (!container) {
  1488. throw new Error('Map container not found.');
  1489. } else if (container._leaflet) {
  1490. throw new Error('Map container is already initialized.');
  1491. }
  1492. container._leaflet = true;
  1493. },
  1494. _initLayout: function () {
  1495. var container = this._container;
  1496. L.DomUtil.addClass(container, 'leaflet-container' +
  1497. (L.Browser.touch ? ' leaflet-touch' : '') +
  1498. (L.Browser.retina ? ' leaflet-retina' : '') +
  1499. (this.options.fadeAnimation ? ' leaflet-fade-anim' : ''));
  1500. var position = L.DomUtil.getStyle(container, 'position');
  1501. if (position !== 'absolute' && position !== 'relative' && position !== 'fixed') {
  1502. container.style.position = 'relative';
  1503. }
  1504. this._initPanes();
  1505. if (this._initControlPos) {
  1506. this._initControlPos();
  1507. }
  1508. },
  1509. _initPanes: function () {
  1510. var panes = this._panes = {};
  1511. this._mapPane = panes.mapPane = this._createPane('leaflet-map-pane', this._container);
  1512. this._tilePane = panes.tilePane = this._createPane('leaflet-tile-pane', this._mapPane);
  1513. panes.objectsPane = this._createPane('leaflet-objects-pane', this._mapPane);
  1514. panes.shadowPane = this._createPane('leaflet-shadow-pane');
  1515. panes.overlayPane = this._createPane('leaflet-overlay-pane');
  1516. panes.markerPane = this._createPane('leaflet-marker-pane');
  1517. panes.popupPane = this._createPane('leaflet-popup-pane');
  1518. var zoomHide = ' leaflet-zoom-hide';
  1519. if (!this.options.markerZoomAnimation) {
  1520. L.DomUtil.addClass(panes.markerPane, zoomHide);
  1521. L.DomUtil.addClass(panes.shadowPane, zoomHide);
  1522. L.DomUtil.addClass(panes.popupPane, zoomHide);
  1523. }
  1524. },
  1525. _createPane: function (className, container) {
  1526. return L.DomUtil.create('div', className, container || this._panes.objectsPane);
  1527. },
  1528. _clearPanes: function () {
  1529. this._container.removeChild(this._mapPane);
  1530. },
  1531. _addLayers: function (layers) {
  1532. layers = layers ? (L.Util.isArray(layers) ? layers : [layers]) : [];
  1533. for (var i = 0, len = layers.length; i < len; i++) {
  1534. this.addLayer(layers[i]);
  1535. }
  1536. },
  1537. // private methods that modify map state
  1538. _resetView: function (center, zoom, preserveMapOffset, afterZoomAnim) {
  1539. var zoomChanged = (this._zoom !== zoom);
  1540. if (!afterZoomAnim) {
  1541. this.fire('movestart');
  1542. if (zoomChanged) {
  1543. this.fire('zoomstart');
  1544. }
  1545. }
  1546. this._zoom = zoom;
  1547. this._initialCenter = center;
  1548. this._initialTopLeftPoint = this._getNewTopLeftPoint(center);
  1549. if (!preserveMapOffset) {
  1550. L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
  1551. } else {
  1552. this._initialTopLeftPoint._add(this._getMapPanePos());
  1553. }
  1554. this._tileLayersToLoad = this._tileLayersNum;
  1555. var loading = !this._loaded;
  1556. this._loaded = true;
  1557. if (loading) {
  1558. this.fire('load');
  1559. this.eachLayer(this._layerAdd, this);
  1560. }
  1561. this.fire('viewreset', {hard: !preserveMapOffset});
  1562. this.fire('move');
  1563. if (zoomChanged || afterZoomAnim) {
  1564. this.fire('zoomend');
  1565. }
  1566. this.fire('moveend', {hard: !preserveMapOffset});
  1567. },
  1568. _rawPanBy: function (offset) {
  1569. L.DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset));
  1570. },
  1571. _getZoomSpan: function () {
  1572. return this.getMaxZoom() - this.getMinZoom();
  1573. },
  1574. _updateZoomLevels: function () {
  1575. var i,
  1576. minZoom = Infinity,
  1577. maxZoom = -Infinity,
  1578. oldZoomSpan = this._getZoomSpan();
  1579. for (i in this._zoomBoundLayers) {
  1580. var layer = this._zoomBoundLayers[i];
  1581. if (!isNaN(layer.options.minZoom)) {
  1582. minZoom = Math.min(minZoom, layer.options.minZoom);
  1583. }
  1584. if (!isNaN(layer.options.maxZoom)) {
  1585. maxZoom = Math.max(maxZoom, layer.options.maxZoom);
  1586. }
  1587. }
  1588. if (i === undefined) { // we have no tilelayers
  1589. this._layersMaxZoom = this._layersMinZoom = undefined;
  1590. } else {
  1591. this._layersMaxZoom = maxZoom;
  1592. this._layersMinZoom = minZoom;
  1593. }
  1594. if (oldZoomSpan !== this._getZoomSpan()) {
  1595. this.fire('zoomlevelschange');
  1596. }
  1597. },
  1598. _panInsideMaxBounds: function () {
  1599. this.panInsideBounds(this.options.maxBounds);
  1600. },
  1601. _checkIfLoaded: function () {
  1602. if (!this._loaded) {
  1603. throw new Error('Set map center and zoom first.');
  1604. }
  1605. },
  1606. // map events
  1607. _initEvents: function (onOff) {
  1608. if (!L.DomEvent) { return; }
  1609. onOff = onOff || 'on';
  1610. L.DomEvent[onOff](this._container, 'click', this._onMouseClick, this);
  1611. var events = ['dblclick', 'mousedown', 'mouseup', 'mouseenter',
  1612. 'mouseleave', 'mousemove', 'contextmenu'],
  1613. i, len;
  1614. for (i = 0, len = events.length; i < len; i++) {
  1615. L.DomEvent[onOff](this._container, events[i], this._fireMouseEvent, this);
  1616. }
  1617. if (this.options.trackResize) {
  1618. L.DomEvent[onOff](window, 'resize', this._onResize, this);
  1619. }
  1620. },
  1621. _onResize: function () {
  1622. L.Util.cancelAnimFrame(this._resizeRequest);
  1623. this._resizeRequest = L.Util.requestAnimFrame(
  1624. this.invalidateSize, this, false, this._container);
  1625. },
  1626. _onMouseClick: function (e) {
  1627. if (!this._loaded || (!e._simulated && this.dragging && this.dragging.moved()) ||
  1628. L.DomEvent._skipped(e)) { return; }
  1629. this.fire('preclick');
  1630. this._fireMouseEvent(e);
  1631. },
  1632. _fireMouseEvent: function (e) {
  1633. if (!this._loaded || L.DomEvent._skipped(e)) { return; }
  1634. var type = e.type;
  1635. type = (type === 'mouseenter' ? 'mouseover' : (type === 'mouseleave' ? 'mouseout' : type));
  1636. if (!this.hasEventListeners(type)) { return; }
  1637. if (type === 'contextmenu') {
  1638. L.DomEvent.preventDefault(e);
  1639. }
  1640. var containerPoint = this.mouseEventToContainerPoint(e),
  1641. layerPoint = this.containerPointToLayerPoint(containerPoint),
  1642. latlng = this.layerPointToLatLng(layerPoint);
  1643. this.fire(type, {
  1644. latlng: latlng,
  1645. layerPoint: layerPoint,
  1646. containerPoint: containerPoint,
  1647. originalEvent: e
  1648. });
  1649. },
  1650. _onTileLayerLoad: function () {
  1651. this._tileLayersToLoad--;
  1652. if (this._tileLayersNum && !this._tileLayersToLoad) {
  1653. this.fire('tilelayersload');
  1654. }
  1655. },
  1656. _clearHandlers: function () {
  1657. for (var i = 0, len = this._handlers.length; i < len; i++) {
  1658. this._handlers[i].disable();
  1659. }
  1660. },
  1661. whenReady: function (callback, context) {
  1662. if (this._loaded) {
  1663. callback.call(context || this, this);
  1664. } else {
  1665. this.on('load', callback, context);
  1666. }
  1667. return this;
  1668. },
  1669. _layerAdd: function (layer) {
  1670. layer.onAdd(this);
  1671. this.fire('layeradd', {layer: layer});
  1672. },
  1673. // private methods for getting map state
  1674. _getMapPanePos: function () {
  1675. return L.DomUtil.getPosition(this._mapPane);
  1676. },
  1677. _moved: function () {
  1678. var pos = this._getMapPanePos();
  1679. return pos && !pos.equals([0, 0]);
  1680. },
  1681. _getTopLeftPoint: function () {
  1682. return this.getPixelOrigin().subtract(this._getMapPanePos());
  1683. },
  1684. _getNewTopLeftPoint: function (center, zoom) {
  1685. var viewHalf = this.getSize()._divideBy(2);
  1686. // TODO round on display, not calculation to increase precision?
  1687. return this.project(center, zoom)._subtract(viewHalf)._round();
  1688. },
  1689. _latLngToNewLayerPoint: function (latlng, newZoom, newCenter) {
  1690. var topLeft = this._getNewTopLeftPoint(newCenter, newZoom).add(this._getMapPanePos());
  1691. return this.project(latlng, newZoom)._subtract(topLeft);
  1692. },
  1693. // layer point of the current center
  1694. _getCenterLayerPoint: function () {
  1695. return this.containerPointToLayerPoint(this.getSize()._divideBy(2));
  1696. },
  1697. // offset of the specified place to the current center in pixels
  1698. _getCenterOffset: function (latlng) {
  1699. return this.latLngToLayerPoint(latlng).subtract(this._getCenterLayerPoint());
  1700. },
  1701. _limitZoom: function (zoom) {
  1702. var min = this.getMinZoom(),
  1703. max = this.getMaxZoom();
  1704. return Math.max(min, Math.min(max, zoom));
  1705. }
  1706. });
  1707. L.map = function (id, options) {
  1708. return new L.Map(id, options);
  1709. };
  1710. /*
  1711. * Mercator projection that takes into account that the Earth is not a perfect sphere.
  1712. * Less popular than spherical mercator; used by projections like EPSG:3395.
  1713. */
  1714. L.Projection.Mercator = {
  1715. MAX_LATITUDE: 85.0840591556,
  1716. R_MINOR: 6356752.314245179,
  1717. R_MAJOR: 6378137,
  1718. project: function (latlng) { // (LatLng) -> Point
  1719. var d = L.LatLng.DEG_TO_RAD,
  1720. max = this.MAX_LATITUDE,
  1721. lat = Math.max(Math.min(max, latlng.lat), -max),
  1722. r = this.R_MAJOR,
  1723. r2 = this.R_MINOR,
  1724. x = latlng.lng * d * r,
  1725. y = lat * d,
  1726. tmp = r2 / r,
  1727. eccent = Math.sqrt(1.0 - tmp * tmp),
  1728. con = eccent * Math.sin(y);
  1729. con = Math.pow((1 - con) / (1 + con), eccent * 0.5);
  1730. var ts = Math.tan(0.5 * ((Math.PI * 0.5) - y)) / con;
  1731. y = -r * Math.log(ts);
  1732. return new L.Point(x, y);
  1733. },
  1734. unproject: function (point) { // (Point, Boolean) -> LatLng
  1735. var d = L.LatLng.RAD_TO_DEG,
  1736. r = this.R_MAJOR,
  1737. r2 = this.R_MINOR,
  1738. lng = point.x * d / r,
  1739. tmp = r2 / r,
  1740. eccent = Math.sqrt(1 - (tmp * tmp)),
  1741. ts = Math.exp(- point.y / r),
  1742. phi = (Math.PI / 2) - 2 * Math.atan(ts),
  1743. numIter = 15,
  1744. tol = 1e-7,
  1745. i = numIter,
  1746. dphi = 0.1,
  1747. con;
  1748. while ((Math.abs(dphi) > tol) && (--i > 0)) {
  1749. con = eccent * Math.sin(phi);
  1750. dphi = (Math.PI / 2) - 2 * Math.atan(ts *
  1751. Math.pow((1.0 - con) / (1.0 + con), 0.5 * eccent)) - phi;
  1752. phi += dphi;
  1753. }
  1754. return new L.LatLng(phi * d, lng);
  1755. }
  1756. };
  1757. L.CRS.EPSG3395 = L.extend({}, L.CRS, {
  1758. code: 'EPSG:3395',
  1759. projection: L.Projection.Mercator,
  1760. transformation: (function () {
  1761. var m = L.Projection.Mercator,
  1762. r = m.R_MAJOR,
  1763. r2 = m.R_MINOR;
  1764. return new L.Transformation(0.5 / (Math.PI * r), 0.5, -0.5 / (Math.PI * r2), 0.5);
  1765. }())
  1766. });
  1767. /*
  1768. * L.TileLayer is used for standard xyz-numbered tile layers.
  1769. */
  1770. L.TileLayer = L.Class.extend({
  1771. includes: L.Mixin.Events,
  1772. options: {
  1773. minZoom: 0,
  1774. maxZoom: 18,
  1775. tileSize: 256,
  1776. subdomains: 'abc',
  1777. errorTileUrl: '',
  1778. attribution: '',
  1779. zoomOffset: 0,
  1780. opacity: 1,
  1781. /* (undefined works too)
  1782. zIndex: null,
  1783. tms: false,
  1784. continuousWorld: false,
  1785. noWrap: false,
  1786. zoomReverse: false,
  1787. detectRetina: false,
  1788. reuseTiles: false,
  1789. bounds: false,
  1790. */
  1791. unloadInvisibleTiles: L.Browser.mobile,
  1792. updateWhenIdle: L.Browser.mobile
  1793. },
  1794. initialize: function (url, options) {
  1795. options = L.setOptions(this, options);
  1796. // detecting retina displays, adjusting tileSize and zoom levels
  1797. if (options.detectRetina && L.Browser.retina && options.maxZoom > 0) {
  1798. options.tileSize = Math.floor(options.tileSize / 2);
  1799. options.zoomOffset++;
  1800. if (options.minZoom > 0) {
  1801. options.minZoom--;
  1802. }
  1803. this.options.maxZoom--;
  1804. }
  1805. if (options.bounds) {
  1806. options.bounds = L.latLngBounds(options.bounds);
  1807. }
  1808. this._url = url;
  1809. var subdomains = this.options.subdomains;
  1810. if (typeof subdomains === 'string') {
  1811. this.options.subdomains = subdomains.split('');
  1812. }
  1813. },
  1814. onAdd: function (map) {
  1815. this._map = map;
  1816. this._animated = map._zoomAnimated;
  1817. // create a container div for tiles
  1818. this._initContainer();
  1819. // create an image to clone for tiles
  1820. this._createTileProto();
  1821. // set up events
  1822. map.on({
  1823. 'viewreset': this._reset,
  1824. 'moveend': this._update
  1825. }, this);
  1826. if (this._animated) {
  1827. map.on({
  1828. 'zoomanim': this._animateZoom,
  1829. 'zoomend': this._endZoomAnim
  1830. }, this);
  1831. }
  1832. if (!this.options.updateWhenIdle) {
  1833. this._limitedUpdate = L.Util.limitExecByInterval(this._update, 150, this);
  1834. map.on('move', this._limitedUpdate, this);
  1835. }
  1836. this._reset();
  1837. this._update();
  1838. },
  1839. addTo: function (map) {
  1840. map.addLayer(this);
  1841. return this;
  1842. },
  1843. onRemove: function (map) {
  1844. this._container.parentNode.removeChild(this._container);
  1845. map.off({
  1846. 'viewreset': this._reset,
  1847. 'moveend': this._update
  1848. }, this);
  1849. if (this._animated) {
  1850. map.off({
  1851. 'zoomanim': this._animateZoom,
  1852. 'zoomend': this._endZoomAnim
  1853. }, this);
  1854. }
  1855. if (!this.options.updateWhenIdle) {
  1856. map.off('move', this._limitedUpdate, this);
  1857. }
  1858. this._container = null;
  1859. this._map = null;
  1860. },
  1861. bringToFront: function () {
  1862. var pane = this._map._panes.tilePane;
  1863. if (this._container) {
  1864. pane.appendChild(this._container);
  1865. this._setAutoZIndex(pane, Math.max);
  1866. }
  1867. return this;
  1868. },
  1869. bringToBack: function () {
  1870. var pane = this._map._panes.tilePane;
  1871. if (this._container) {
  1872. pane.insertBefore(this._container, pane.firstChild);
  1873. this._setAutoZIndex(pane, Math.min);
  1874. }
  1875. return this;
  1876. },
  1877. getAttribution: function () {
  1878. return this.options.attribution;
  1879. },
  1880. getContainer: function () {
  1881. return this._container;
  1882. },
  1883. setOpacity: function (opacity) {
  1884. this.options.opacity = opacity;
  1885. if (this._map) {
  1886. this._updateOpacity();
  1887. }
  1888. return this;
  1889. },
  1890. setZIndex: function (zIndex) {
  1891. this.options.zIndex = zIndex;
  1892. this._updateZIndex();
  1893. return this;
  1894. },
  1895. setUrl: function (url, noRedraw) {
  1896. this._url = url;
  1897. if (!noRedraw) {
  1898. this.redraw();
  1899. }
  1900. return this;
  1901. },
  1902. redraw: function () {
  1903. if (this._map) {
  1904. this._reset({hard: true});
  1905. this._update();
  1906. }
  1907. return this;
  1908. },
  1909. _updateZIndex: function () {
  1910. if (this._container && this.options.zIndex !== undefined) {
  1911. this._container.style.zIndex = this.options.zIndex;
  1912. }
  1913. },
  1914. _setAutoZIndex: function (pane, compare) {
  1915. var layers = pane.children,
  1916. edgeZIndex = -compare(Infinity, -Infinity), // -Infinity for max, Infinity for min
  1917. zIndex, i, len;
  1918. for (i = 0, len = layers.length; i < len; i++) {
  1919. if (layers[i] !== this._container) {
  1920. zIndex = parseInt(layers[i].style.zIndex, 10);
  1921. if (!isNaN(zIndex)) {
  1922. edgeZIndex = compare(edgeZIndex, zIndex);
  1923. }
  1924. }
  1925. }
  1926. this.options.zIndex = this._container.style.zIndex =
  1927. (isFinite(edgeZIndex) ? edgeZIndex : 0) + compare(1, -1);
  1928. },
  1929. _updateOpacity: function () {
  1930. var i,
  1931. tiles = this._tiles;
  1932. if (L.Browser.ielt9) {
  1933. for (i in tiles) {
  1934. L.DomUtil.setOpacity(tiles[i], this.options.opacity);
  1935. }
  1936. } else {
  1937. L.DomUtil.setOpacity(this._container, this.options.opacity);
  1938. }
  1939. },
  1940. _initContainer: function () {
  1941. var tilePane = this._map._panes.tilePane;
  1942. if (!this._container) {
  1943. this._container = L.DomUtil.create('div', 'leaflet-layer');
  1944. this._updateZIndex();
  1945. if (this._animated) {
  1946. var className = 'leaflet-tile-container leaflet-zoom-animated';
  1947. this._bgBuffer = L.DomUtil.create('div', className, this._container);
  1948. this._tileContainer = L.DomUtil.create('div', className, this._container);
  1949. } else {
  1950. this._tileContainer = this._container;
  1951. }
  1952. tilePane.appendChild(this._container);
  1953. if (this.options.opacity < 1) {
  1954. this._updateOpacity();
  1955. }
  1956. }
  1957. },
  1958. _reset: function (e) {
  1959. for (var key in this._tiles) {
  1960. this.fire('tileunload', {tile: this._tiles[key]});
  1961. }
  1962. this._tiles = {};
  1963. this._tilesToLoad = 0;
  1964. if (this.options.reuseTiles) {
  1965. this._unusedTiles = [];
  1966. }
  1967. this._tileContainer.innerHTML = '';
  1968. if (this._animated && e && e.hard) {
  1969. this._clearBgBuffer();
  1970. }
  1971. this._initContainer();
  1972. },
  1973. _update: function () {
  1974. if (!this._map) { return; }
  1975. var bounds = this._map.getPixelBounds(),
  1976. zoom = this._map.getZoom(),
  1977. tileSize = this.options.tileSize;
  1978. if (zoom > this.options.maxZoom || zoom < this.options.minZoom) {
  1979. return;
  1980. }
  1981. var tileBounds = L.bounds(
  1982. bounds.min.divideBy(tileSize)._floor(),
  1983. bounds.max.divideBy(tileSize)._floor());
  1984. this._addTilesFromCenterOut(tileBounds);
  1985. if (this.options.unloadInvisibleTiles || this.options.reuseTiles) {
  1986. this._removeOtherTiles(tileBounds);
  1987. }
  1988. },
  1989. _addTilesFromCenterOut: function (bounds) {
  1990. var queue = [],
  1991. center = bounds.getCenter();
  1992. var j, i, point;
  1993. for (j = bounds.min.y; j <= bounds.max.y; j++) {
  1994. for (i = bounds.min.x; i <= bounds.max.x; i++) {
  1995. point = new L.Point(i, j);
  1996. if (this._tileShouldBeLoaded(point)) {
  1997. queue.push(point);
  1998. }
  1999. }
  2000. }
  2001. var tilesToLoad = queue.length;
  2002. if (tilesToLoad === 0) { return; }
  2003. // load tiles in order of their distance to center
  2004. queue.sort(function (a, b) {
  2005. return a.distanceTo(center) - b.distanceTo(center);
  2006. });
  2007. var fragment = document.createDocumentFragment();
  2008. // if its the first batch of tiles to load
  2009. if (!this._tilesToLoad) {
  2010. this.fire('loading');
  2011. }
  2012. this._tilesToLoad += tilesToLoad;
  2013. for (i = 0; i < tilesToLoad; i++) {
  2014. this._addTile(queue[i], fragment);
  2015. }
  2016. this._tileContainer.appendChild(fragment);
  2017. },
  2018. _tileShouldBeLoaded: function (tilePoint) {
  2019. if ((tilePoint.x + ':' + tilePoint.y) in this._tiles) {
  2020. return false; // already loaded
  2021. }
  2022. var options = this.options;
  2023. if (!options.continuousWorld) {
  2024. var limit = this._getWrapTileNum();
  2025. // don't load if exceeds world bounds
  2026. if ((options.noWrap && (tilePoint.x < 0 || tilePoint.x >= limit)) ||
  2027. tilePoint.y < 0 || tilePoint.y >= limit) { return false; }
  2028. }
  2029. if (options.bounds) {
  2030. var tileSize = options.tileSize,
  2031. nwPoint = tilePoint.multiplyBy(tileSize),
  2032. sePoint = nwPoint.add([tileSize, tileSize]),
  2033. nw = this._map.unproject(nwPoint),
  2034. se = this._map.unproject(sePoint);
  2035. // TODO temporary hack, will be removed after refactoring projections
  2036. // https://github.com/Leaflet/Leaflet/issues/1618
  2037. if (!options.continuousWorld && !options.noWrap) {
  2038. nw = nw.wrap();
  2039. se = se.wrap();
  2040. }
  2041. if (!options.bounds.intersects([nw, se])) { return false; }
  2042. }
  2043. return true;
  2044. },
  2045. _removeOtherTiles: function (bounds) {
  2046. var kArr, x, y, key;
  2047. for (key in this._tiles) {
  2048. kArr = key.split(':');
  2049. x = parseInt(kArr[0], 10);
  2050. y = parseInt(kArr[1], 10);
  2051. // remove tile if it's out of bounds
  2052. if (x < bounds.min.x || x > bounds.max.x || y < bounds.min.y || y > bounds.max.y) {
  2053. this._removeTile(key);
  2054. }
  2055. }
  2056. },
  2057. _removeTile: function (key) {
  2058. var tile = this._tiles[key];
  2059. this.fire('tileunload', {tile: tile, url: tile.src});
  2060. if (this.options.reuseTiles) {
  2061. L.DomUtil.removeClass(tile, 'leaflet-tile-loaded');
  2062. this._unusedTiles.push(tile);
  2063. } else if (tile.parentNode === this._tileContainer) {
  2064. this._tileContainer.removeChild(tile);
  2065. }
  2066. // for https://github.com/CloudMade/Leaflet/issues/137
  2067. if (!L.Browser.android) {
  2068. tile.onload = null;
  2069. tile.src = L.Util.emptyImageUrl;
  2070. }
  2071. delete this._tiles[key];
  2072. },
  2073. _addTile: function (tilePoint, container) {
  2074. var tilePos = this._getTilePos(tilePoint);
  2075. // get unused tile - or create a new tile
  2076. var tile = this._getTile();
  2077. /*
  2078. Chrome 20 layouts much faster with top/left (verify with timeline, frames)
  2079. Android 4 browser has display issues with top/left and requires transform instead
  2080. Android 2 browser requires top/left or tiles disappear on load or first drag
  2081. (reappear after zoom) https://github.com/CloudMade/Leaflet/issues/866
  2082. (other browsers don't currently care) - see debug/hacks/jitter.html for an example
  2083. */
  2084. L.DomUtil.setPosition(tile, tilePos, L.Browser.chrome || L.Browser.android23);
  2085. this._tiles[tilePoint.x + ':' + tilePoint.y] = tile;
  2086. this._loadTile(tile, tilePoint);
  2087. if (tile.parentNode !== this._tileContainer) {
  2088. container.appendChild(tile);
  2089. }
  2090. },
  2091. _getZoomForUrl: function () {
  2092. var options = this.options,
  2093. zoom = this._map.getZoom();
  2094. if (options.zoomReverse) {
  2095. zoom = options.maxZoom - zoom;
  2096. }
  2097. return zoom + options.zoomOffset;
  2098. },
  2099. _getTilePos: function (tilePoint) {
  2100. var origin = this._map.getPixelOrigin(),
  2101. tileSize = this.options.tileSize;
  2102. return tilePoint.multiplyBy(tileSize).subtract(origin);
  2103. },
  2104. // image-specific code (override to implement e.g. Canvas or SVG tile layer)
  2105. getTileUrl: function (tilePoint) {
  2106. return L.Util.template(this._url, L.extend({
  2107. s: this._getSubdomain(tilePoint),
  2108. z: tilePoint.z,
  2109. x: tilePoint.x,
  2110. y: tilePoint.y
  2111. }, this.options));
  2112. },
  2113. _getWrapTileNum: function () {
  2114. // TODO refactor, limit is not valid for non-standard projections
  2115. return Math.pow(2, this._getZoomForUrl());
  2116. },
  2117. _adjustTilePoint: function (tilePoint) {
  2118. var limit = this._getWrapTileNum();
  2119. // wrap tile coordinates
  2120. if (!this.options.continuousWorld && !this.options.noWrap) {
  2121. tilePoint.x = ((tilePoint.x % limit) + limit) % limit;
  2122. }
  2123. if (this.options.tms) {
  2124. tilePoint.y = limit - tilePoint.y - 1;
  2125. }
  2126. tilePoint.z = this._getZoomForUrl();
  2127. },
  2128. _getSubdomain: function (tilePoint) {
  2129. var index = Math.abs(tilePoint.x + tilePoint.y) % this.options.subdomains.length;
  2130. return this.options.subdomains[index];
  2131. },
  2132. _createTileProto: function () {
  2133. var img = this._tileImg = L.DomUtil.create('img', 'leaflet-tile');
  2134. img.style.width = img.style.height = this.options.tileSize + 'px';
  2135. img.galleryimg = 'no';
  2136. },
  2137. _getTile: function () {
  2138. if (this.options.reuseTiles && this._unusedTiles.length > 0) {
  2139. var tile = this._unusedTiles.pop();
  2140. this._resetTile(tile);
  2141. return tile;
  2142. }
  2143. return this._createTile();
  2144. },
  2145. // Override if data stored on a tile needs to be cleaned up before reuse
  2146. _resetTile: function (/*tile*/) {},
  2147. _createTile: function () {
  2148. var tile = this._tileImg.cloneNode(false);
  2149. tile.onselectstart = tile.onmousemove = L.Util.falseFn;
  2150. if (L.Browser.ielt9 && this.options.opacity !== undefined) {
  2151. L.DomUtil.setOpacity(tile, this.options.opacity);
  2152. }
  2153. return tile;
  2154. },
  2155. _loadTile: function (tile, tilePoint) {
  2156. tile._layer = this;
  2157. tile.onload = this._tileOnLoad;
  2158. tile.onerror = this._tileOnError;
  2159. this._adjustTilePoint(tilePoint);
  2160. tile.src = this.getTileUrl(tilePoint);
  2161. },
  2162. _tileLoaded: function () {
  2163. this._tilesToLoad--;
  2164. if (!this._tilesToLoad) {
  2165. this.fire('load');
  2166. if (this._animated) {
  2167. // clear scaled tiles after all new tiles are loaded (for performance)
  2168. clearTimeout(this._clearBgBufferTimer);
  2169. this._clearBgBufferTimer = setTimeout(L.bind(this._clearBgBuffer, this), 500);
  2170. }
  2171. }
  2172. },
  2173. _tileOnLoad: function () {
  2174. var layer = this._layer;
  2175. //Only if we are loading an actual image
  2176. if (this.src !== L.Util.emptyImageUrl) {
  2177. L.DomUtil.addClass(this, 'leaflet-tile-loaded');
  2178. layer.fire('tileload', {
  2179. tile: this,
  2180. url: this.src
  2181. });
  2182. }
  2183. layer._tileLoaded();
  2184. },
  2185. _tileOnError: function () {
  2186. var layer = this._layer;
  2187. layer.fire('tileerror', {
  2188. tile: this,
  2189. url: this.src
  2190. });
  2191. var newUrl = layer.options.errorTileUrl;
  2192. if (newUrl) {
  2193. this.src = newUrl;
  2194. }
  2195. layer._tileLoaded();
  2196. }
  2197. });
  2198. L.tileLayer = function (url, options) {
  2199. return new L.TileLayer(url, options);
  2200. };
  2201. /*
  2202. * L.TileLayer.WMS is used for putting WMS tile layers on the map.
  2203. */
  2204. L.TileLayer.WMS = L.TileLayer.extend({
  2205. defaultWmsParams: {
  2206. service: 'WMS',
  2207. request: 'GetMap',
  2208. version: '1.1.1',
  2209. layers: '',
  2210. styles: '',
  2211. format: 'image/jpeg',
  2212. transparent: false
  2213. },
  2214. initialize: function (url, options) { // (String, Object)
  2215. this._url = url;
  2216. var wmsParams = L.extend({}, this.defaultWmsParams),
  2217. tileSize = options.tileSize || this.options.tileSize;
  2218. if (options.detectRetina && L.Browser.retina) {
  2219. wmsParams.width = wmsParams.height = tileSize * 2;
  2220. } else {
  2221. wmsParams.width = wmsParams.height = tileSize;
  2222. }
  2223. for (var i in options) {
  2224. // all keys that are not TileLayer options go to WMS params
  2225. if (!this.options.hasOwnProperty(i) && i !== 'crs') {
  2226. wmsParams[i] = options[i];
  2227. }
  2228. }
  2229. this.wmsParams = wmsParams;
  2230. L.setOptions(this, options);
  2231. },
  2232. onAdd: function (map) {
  2233. this._crs = this.options.crs || map.options.crs;
  2234. var projectionKey = parseFloat(this.wmsParams.version) >= 1.3 ? 'crs' : 'srs';
  2235. this.wmsParams[projectionKey] = this._crs.code;
  2236. L.TileLayer.prototype.onAdd.call(this, map);
  2237. },
  2238. getTileUrl: function (tilePoint, zoom) { // (Point, Number) -> String
  2239. var map = this._map,
  2240. tileSize = this.options.tileSize,
  2241. nwPoint = tilePoint.multiplyBy(tileSize),
  2242. sePoint = nwPoint.add([tileSize, tileSize]),
  2243. nw = this._crs.project(map.unproject(nwPoint, zoom)),
  2244. se = this._crs.project(map.unproject(sePoint, zoom)),
  2245. bbox = [nw.x, se.y, se.x, nw.y].join(','),
  2246. url = L.Util.template(this._url, {s: this._getSubdomain(tilePoint)});
  2247. return url + L.Util.getParamString(this.wmsParams, url, true) + '&BBOX=' + bbox;
  2248. },
  2249. setParams: function (params, noRedraw) {
  2250. L.extend(this.wmsParams, params);
  2251. if (!noRedraw) {
  2252. this.redraw();
  2253. }
  2254. return this;
  2255. }
  2256. });
  2257. L.tileLayer.wms = function (url, options) {
  2258. return new L.TileLayer.WMS(url, options);
  2259. };
  2260. /*
  2261. * L.TileLayer.Canvas is a class that you can use as a base for creating
  2262. * dynamically drawn Canvas-based tile layers.
  2263. */
  2264. L.TileLayer.Canvas = L.TileLayer.extend({
  2265. options: {
  2266. async: false
  2267. },
  2268. initialize: function (options) {
  2269. L.setOptions(this, options);
  2270. },
  2271. redraw: function () {
  2272. if (this._map) {
  2273. this._reset({hard: true});
  2274. this._update();
  2275. }
  2276. for (var i in this._tiles) {
  2277. this._redrawTile(this._tiles[i]);
  2278. }
  2279. return this;
  2280. },
  2281. _redrawTile: function (tile) {
  2282. this.drawTile(tile, tile._tilePoint, this._map._zoom);
  2283. },
  2284. _createTileProto: function () {
  2285. var proto = this._canvasProto = L.DomUtil.create('canvas', 'leaflet-tile');
  2286. proto.width = proto.height = this.options.tileSize;
  2287. },
  2288. _createTile: function () {
  2289. var tile = this._canvasProto.cloneNode(false);
  2290. tile.onselectstart = tile.onmousemove = L.Util.falseFn;
  2291. return tile;
  2292. },
  2293. _loadTile: function (tile, tilePoint) {
  2294. tile._layer = this;
  2295. tile._tilePoint = tilePoint;
  2296. this._redrawTile(tile);
  2297. if (!this.options.async) {
  2298. this.tileDrawn(tile);
  2299. }
  2300. },
  2301. drawTile: function (/*tile, tilePoint*/) {
  2302. // override with rendering code
  2303. },
  2304. tileDrawn: function (tile) {
  2305. this._tileOnLoad.call(tile);
  2306. }
  2307. });
  2308. L.tileLayer.canvas = function (options) {
  2309. return new L.TileLayer.Canvas(options);
  2310. };
  2311. /*
  2312. * L.ImageOverlay is used to overlay images over the map (to specific geographical bounds).
  2313. */
  2314. L.ImageOverlay = L.Class.extend({
  2315. includes: L.Mixin.Events,
  2316. options: {
  2317. opacity: 1
  2318. },
  2319. initialize: function (url, bounds, options) { // (String, LatLngBounds, Object)
  2320. this._url = url;
  2321. this._bounds = L.latLngBounds(bounds);
  2322. L.setOptions(this, options);
  2323. },
  2324. onAdd: function (map) {
  2325. this._map = map;
  2326. if (!this._image) {
  2327. this._initImage();
  2328. }
  2329. map._panes.overlayPane.appendChild(this._image);
  2330. map.on('viewreset', this._reset, this);
  2331. if (map.options.zoomAnimation && L.Browser.any3d) {
  2332. map.on('zoomanim', this._animateZoom, this);
  2333. }
  2334. this._reset();
  2335. },
  2336. onRemove: function (map) {
  2337. map.getPanes().overlayPane.removeChild(this._image);
  2338. map.off('viewreset', this._reset, this);
  2339. if (map.options.zoomAnimation) {
  2340. map.off('zoomanim', this._animateZoom, this);
  2341. }
  2342. },
  2343. addTo: function (map) {
  2344. map.addLayer(this);
  2345. return this;
  2346. },
  2347. setOpacity: function (opacity) {
  2348. this.options.opacity = opacity;
  2349. this._updateOpacity();
  2350. return this;
  2351. },
  2352. // TODO remove bringToFront/bringToBack duplication from TileLayer/Path
  2353. bringToFront: function () {
  2354. if (this._image) {
  2355. this._map._panes.overlayPane.appendChild(this._image);
  2356. }
  2357. return this;
  2358. },
  2359. bringToBack: function () {
  2360. var pane = this._map._panes.overlayPane;
  2361. if (this._image) {
  2362. pane.insertBefore(this._image, pane.firstChild);
  2363. }
  2364. return this;
  2365. },
  2366. _initImage: function () {
  2367. this._image = L.DomUtil.create('img', 'leaflet-image-layer');
  2368. if (this._map.options.zoomAnimation && L.Browser.any3d) {
  2369. L.DomUtil.addClass(this._image, 'leaflet-zoom-animated');
  2370. } else {
  2371. L.DomUtil.addClass(this._image, 'leaflet-zoom-hide');
  2372. }
  2373. this._updateOpacity();
  2374. //TODO createImage util method to remove duplication
  2375. L.extend(this._image, {
  2376. galleryimg: 'no',
  2377. onselectstart: L.Util.falseFn,
  2378. onmousemove: L.Util.falseFn,
  2379. onload: L.bind(this._onImageLoad, this),
  2380. src: this._url
  2381. });
  2382. },
  2383. _animateZoom: function (e) {
  2384. var map = this._map,
  2385. image = this._image,
  2386. scale = map.getZoomScale(e.zoom),
  2387. nw = this._bounds.getNorthWest(),
  2388. se = this._bounds.getSouthEast(),
  2389. topLeft = map._latLngToNewLayerPoint(nw, e.zoom, e.center),
  2390. size = map._latLngToNewLayerPoint(se, e.zoom, e.center)._subtract(topLeft),
  2391. origin = topLeft._add(size._multiplyBy((1 / 2) * (1 - 1 / scale)));
  2392. image.style[L.DomUtil.TRANSFORM] =
  2393. L.DomUtil.getTranslateString(origin) + ' scale(' + scale + ') ';
  2394. },
  2395. _reset: function () {
  2396. var image = this._image,
  2397. topLeft = this._map.latLngToLayerPoint(this._bounds.getNorthWest()),
  2398. size = this._map.latLngToLayerPoint(this._bounds.getSouthEast())._subtract(topLeft);
  2399. L.DomUtil.setPosition(image, topLeft);
  2400. image.style.width = size.x + 'px';
  2401. image.style.height = size.y + 'px';
  2402. },
  2403. _onImageLoad: function () {
  2404. this.fire('load');
  2405. },
  2406. _updateOpacity: function () {
  2407. L.DomUtil.setOpacity(this._image, this.options.opacity);
  2408. }
  2409. });
  2410. L.imageOverlay = function (url, bounds, options) {
  2411. return new L.ImageOverlay(url, bounds, options);
  2412. };
  2413. /*
  2414. * L.Icon is an image-based icon class that you can use with L.Marker for custom markers.
  2415. */
  2416. L.Icon = L.Class.extend({
  2417. options: {
  2418. /*
  2419. iconUrl: (String) (required)
  2420. iconRetinaUrl: (String) (optional, used for retina devices if detected)
  2421. iconSize: (Point) (can be set through CSS)
  2422. iconAnchor: (Point) (centered by default, can be set in CSS with negative margins)
  2423. popupAnchor: (Point) (if not specified, popup opens in the anchor point)
  2424. shadowUrl: (String) (no shadow by default)
  2425. shadowRetinaUrl: (String) (optional, used for retina devices if detected)
  2426. shadowSize: (Point)
  2427. shadowAnchor: (Point)
  2428. */
  2429. className: ''
  2430. },
  2431. initialize: function (options) {
  2432. L.setOptions(this, options);
  2433. },
  2434. createIcon: function (oldIcon) {
  2435. return this._createIcon('icon', oldIcon);
  2436. },
  2437. createShadow: function (oldIcon) {
  2438. return this._createIcon('shadow', oldIcon);
  2439. },
  2440. _createIcon: function (name, oldIcon) {
  2441. var src = this._getIconUrl(name);
  2442. if (!src) {
  2443. if (name === 'icon') {
  2444. throw new Error('iconUrl not set in Icon options (see the docs).');
  2445. }
  2446. return null;
  2447. }
  2448. var img;
  2449. if (!oldIcon || oldIcon.tagName !== 'IMG') {
  2450. img = this._createImg(src);
  2451. } else {
  2452. img = this._createImg(src, oldIcon);
  2453. }
  2454. this._setIconStyles(img, name);
  2455. return img;
  2456. },
  2457. _setIconStyles: function (img, name) {
  2458. var options = this.options,
  2459. size = L.point(options[name + 'Size']),
  2460. anchor;
  2461. if (name === 'shadow') {
  2462. anchor = L.point(options.shadowAnchor || options.iconAnchor);
  2463. } else {
  2464. anchor = L.point(options.iconAnchor);
  2465. }
  2466. if (!anchor && size) {
  2467. anchor = size.divideBy(2, true);
  2468. }
  2469. img.className = 'leaflet-marker-' + name + ' ' + options.className;
  2470. if (anchor) {
  2471. img.style.marginLeft = (-anchor.x) + 'px';
  2472. img.style.marginTop = (-anchor.y) + 'px';
  2473. }
  2474. if (size) {
  2475. img.style.width = size.x + 'px';
  2476. img.style.height = size.y + 'px';
  2477. }
  2478. },
  2479. _createImg: function (src, el) {
  2480. if (!L.Browser.ie6) {
  2481. if (!el) {
  2482. el = document.createElement('img');
  2483. }
  2484. el.src = src;
  2485. } else {
  2486. if (!el) {
  2487. el = document.createElement('div');
  2488. }
  2489. el.style.filter =
  2490. 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '")';
  2491. }
  2492. return el;
  2493. },
  2494. _getIconUrl: function (name) {
  2495. if (L.Browser.retina && this.options[name + 'RetinaUrl']) {
  2496. return this.options[name + 'RetinaUrl'];
  2497. }
  2498. return this.options[name + 'Url'];
  2499. }
  2500. });
  2501. L.icon = function (options) {
  2502. return new L.Icon(options);
  2503. };
  2504. /*
  2505. * L.Icon.Default is the blue marker icon used by default in Leaflet.
  2506. */
  2507. L.Icon.Default = L.Icon.extend({
  2508. options: {
  2509. iconSize: [25, 41],
  2510. iconAnchor: [12, 41],
  2511. popupAnchor: [1, -34],
  2512. shadowSize: [41, 41]
  2513. },
  2514. _getIconUrl: function (name) {
  2515. var key = name + 'Url';
  2516. if (this.options[key]) {
  2517. return this.options[key];
  2518. }
  2519. if (L.Browser.retina && name === 'icon') {
  2520. name += '-2x';
  2521. }
  2522. var path = L.Icon.Default.imagePath;
  2523. if (!path) {
  2524. throw new Error('Couldn\'t autodetect L.Icon.Default.imagePath, set it manually.');
  2525. }
  2526. return path + '/marker-' + name + '.png';
  2527. }
  2528. });
  2529. L.Icon.Default.imagePath = (function () {
  2530. var scripts = document.getElementsByTagName('script'),
  2531. leafletRe = /[\/^]leaflet[\-\._]?([\w\-\._]*)\.js\??/;
  2532. var i, len, src, matches, path;
  2533. for (i = 0, len = scripts.length; i < len; i++) {
  2534. src = scripts[i].src;
  2535. matches = src.match(leafletRe);
  2536. if (matches) {
  2537. path = src.split(leafletRe)[0];
  2538. return (path ? path + '/' : '') + 'images';
  2539. }
  2540. }
  2541. }());
  2542. /*
  2543. * L.Marker is used to display clickable/draggable icons on the map.
  2544. */
  2545. L.Marker = L.Class.extend({
  2546. includes: L.Mixin.Events,
  2547. options: {
  2548. icon: new L.Icon.Default(),
  2549. title: '',
  2550. clickable: true,
  2551. draggable: false,
  2552. keyboard: true,
  2553. zIndexOffset: 0,
  2554. opacity: 1,
  2555. riseOnHover: false,
  2556. riseOffset: 250
  2557. },
  2558. initialize: function (latlng, options) {
  2559. L.setOptions(this, options);
  2560. this._latlng = L.latLng(latlng);
  2561. },
  2562. onAdd: function (map) {
  2563. this._map = map;
  2564. map.on('viewreset', this.update, this);
  2565. this._initIcon();
  2566. this.update();
  2567. if (map.options.zoomAnimation && map.options.markerZoomAnimation) {
  2568. map.on('zoomanim', this._animateZoom, this);
  2569. }
  2570. },
  2571. addTo: function (map) {
  2572. map.addLayer(this);
  2573. return this;
  2574. },
  2575. onRemove: function (map) {
  2576. if (this.dragging) {
  2577. this.dragging.disable();
  2578. }
  2579. this._removeIcon();
  2580. this._removeShadow();
  2581. this.fire('remove');
  2582. map.off({
  2583. 'viewreset': this.update,
  2584. 'zoomanim': this._animateZoom
  2585. }, this);
  2586. this._map = null;
  2587. },
  2588. getLatLng: function () {
  2589. return this._latlng;
  2590. },
  2591. setLatLng: function (latlng) {
  2592. this._latlng = L.latLng(latlng);
  2593. this.update();
  2594. return this.fire('move', { latlng: this._latlng });
  2595. },
  2596. setZIndexOffset: function (offset) {
  2597. this.options.zIndexOffset = offset;
  2598. this.update();
  2599. return this;
  2600. },
  2601. setIcon: function (icon) {
  2602. this.options.icon = icon;
  2603. if (this._map) {
  2604. this._initIcon();
  2605. this.update();
  2606. }
  2607. return this;
  2608. },
  2609. update: function () {
  2610. if (this._icon) {
  2611. var pos = this._map.latLngToLayerPoint(this._latlng).round();
  2612. this._setPos(pos);
  2613. }
  2614. return this;
  2615. },
  2616. _initIcon: function () {
  2617. var options = this.options,
  2618. map = this._map,
  2619. animation = (map.options.zoomAnimation && map.options.markerZoomAnimation),
  2620. classToAdd = animation ? 'leaflet-zoom-animated' : 'leaflet-zoom-hide';
  2621. var icon = options.icon.createIcon(this._icon),
  2622. addIcon = false;
  2623. // if we're not reusing the icon, remove the old one and init new one
  2624. if (icon !== this._icon) {
  2625. if (this._icon) {
  2626. this._removeIcon();
  2627. }
  2628. addIcon = true;
  2629. if (options.title) {
  2630. icon.title = options.title;
  2631. }
  2632. }
  2633. L.DomUtil.addClass(icon, classToAdd);
  2634. if (options.keyboard) {
  2635. icon.tabIndex = '0';
  2636. }
  2637. this._icon = icon;
  2638. this._initInteraction();
  2639. if (options.riseOnHover) {
  2640. L.DomEvent
  2641. .on(icon, 'mouseover', this._bringToFront, this)
  2642. .on(icon, 'mouseout', this._resetZIndex, this);
  2643. }
  2644. var newShadow = options.icon.createShadow(this._shadow),
  2645. addShadow = false;
  2646. if (newShadow !== this._shadow) {
  2647. this._removeShadow();
  2648. addShadow = true;
  2649. }
  2650. if (newShadow) {
  2651. L.DomUtil.addClass(newShadow, classToAdd);
  2652. }
  2653. this._shadow = newShadow;
  2654. if (options.opacity < 1) {
  2655. this._updateOpacity();
  2656. }
  2657. var panes = this._map._panes;
  2658. if (addIcon) {
  2659. panes.markerPane.appendChild(this._icon);
  2660. }
  2661. if (newShadow && addShadow) {
  2662. panes.shadowPane.appendChild(this._shadow);
  2663. }
  2664. },
  2665. _removeIcon: function () {
  2666. if (this.options.riseOnHover) {
  2667. L.DomEvent
  2668. .off(this._icon, 'mouseover', this._bringToFront)
  2669. .off(this._icon, 'mouseout', this._resetZIndex);
  2670. }
  2671. this._map._panes.markerPane.removeChild(this._icon);
  2672. this._icon = null;
  2673. },
  2674. _removeShadow: function () {
  2675. if (this._shadow) {
  2676. this._map._panes.shadowPane.removeChild(this._shadow);
  2677. }
  2678. this._shadow = null;
  2679. },
  2680. _setPos: function (pos) {
  2681. L.DomUtil.setPosition(this._icon, pos);
  2682. if (this._shadow) {
  2683. L.DomUtil.setPosition(this._shadow, pos);
  2684. }
  2685. this._zIndex = pos.y + this.options.zIndexOffset;
  2686. this._resetZIndex();
  2687. },
  2688. _updateZIndex: function (offset) {
  2689. this._icon.style.zIndex = this._zIndex + offset;
  2690. },
  2691. _animateZoom: function (opt) {
  2692. var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center);
  2693. this._setPos(pos);
  2694. },
  2695. _initInteraction: function () {
  2696. if (!this.options.clickable) { return; }
  2697. // TODO refactor into something shared with Map/Path/etc. to DRY it up
  2698. var icon = this._icon,
  2699. events = ['dblclick', 'mousedown', 'mouseover', 'mouseout', 'contextmenu'];
  2700. L.DomUtil.addClass(icon, 'leaflet-clickable');
  2701. L.DomEvent.on(icon, 'click', this._onMouseClick, this);
  2702. L.DomEvent.on(icon, 'keypress', this._onKeyPress, this);
  2703. for (var i = 0; i < events.length; i++) {
  2704. L.DomEvent.on(icon, events[i], this._fireMouseEvent, this);
  2705. }
  2706. if (L.Handler.MarkerDrag) {
  2707. this.dragging = new L.Handler.MarkerDrag(this);
  2708. if (this.options.draggable) {
  2709. this.dragging.enable();
  2710. }
  2711. }
  2712. },
  2713. _onMouseClick: function (e) {
  2714. var wasDragged = this.dragging && this.dragging.moved();
  2715. if (this.hasEventListeners(e.type) || wasDragged) {
  2716. L.DomEvent.stopPropagation(e);
  2717. }
  2718. if (wasDragged) { return; }
  2719. if ((!this.dragging || !this.dragging._enabled) && this._map.dragging && this._map.dragging.moved()) { return; }
  2720. this.fire(e.type, {
  2721. originalEvent: e,
  2722. latlng: this._latlng
  2723. });
  2724. },
  2725. _onKeyPress: function (e) {
  2726. if (e.keyCode === 13) {
  2727. this.fire('click', {
  2728. originalEvent: e,
  2729. latlng: this._latlng
  2730. });
  2731. }
  2732. },
  2733. _fireMouseEvent: function (e) {
  2734. this.fire(e.type, {
  2735. originalEvent: e,
  2736. latlng: this._latlng
  2737. });
  2738. // TODO proper custom event propagation
  2739. // this line will always be called if marker is in a FeatureGroup
  2740. if (e.type === 'contextmenu' && this.hasEventListeners(e.type)) {
  2741. L.DomEvent.preventDefault(e);
  2742. }
  2743. if (e.type !== 'mousedown') {
  2744. L.DomEvent.stopPropagation(e);
  2745. } else {
  2746. L.DomEvent.preventDefault(e);
  2747. }
  2748. },
  2749. setOpacity: function (opacity) {
  2750. this.options.opacity = opacity;
  2751. if (this._map) {
  2752. this._updateOpacity();
  2753. }
  2754. return this;
  2755. },
  2756. _updateOpacity: function () {
  2757. L.DomUtil.setOpacity(this._icon, this.options.opacity);
  2758. if (this._shadow) {
  2759. L.DomUtil.setOpacity(this._shadow, this.options.opacity);
  2760. }
  2761. },
  2762. _bringToFront: function () {
  2763. this._updateZIndex(this.options.riseOffset);
  2764. },
  2765. _resetZIndex: function () {
  2766. this._updateZIndex(0);
  2767. }
  2768. });
  2769. L.marker = function (latlng, options) {
  2770. return new L.Marker(latlng, options);
  2771. };
  2772. /*
  2773. * L.DivIcon is a lightweight HTML-based icon class (as opposed to the image-based L.Icon)
  2774. * to use with L.Marker.
  2775. */
  2776. L.DivIcon = L.Icon.extend({
  2777. options: {
  2778. iconSize: [12, 12], // also can be set through CSS
  2779. /*
  2780. iconAnchor: (Point)
  2781. popupAnchor: (Point)
  2782. html: (String)
  2783. bgPos: (Point)
  2784. */
  2785. className: 'leaflet-div-icon',
  2786. html: false
  2787. },
  2788. createIcon: function (oldIcon) {
  2789. var div = (oldIcon && oldIcon.tagName === 'DIV') ? oldIcon : document.createElement('div'),
  2790. options = this.options;
  2791. if (options.html !== false) {
  2792. div.innerHTML = options.html;
  2793. } else {
  2794. div.innerHTML = '';
  2795. }
  2796. if (options.bgPos) {
  2797. div.style.backgroundPosition =
  2798. (-options.bgPos.x) + 'px ' + (-options.bgPos.y) + 'px';
  2799. }
  2800. this._setIconStyles(div, 'icon');
  2801. return div;
  2802. },
  2803. createShadow: function () {
  2804. return null;
  2805. }
  2806. });
  2807. L.divIcon = function (options) {
  2808. return new L.DivIcon(options);
  2809. };
  2810. /*
  2811. * L.Popup is used for displaying popups on the map.
  2812. */
  2813. L.Map.mergeOptions({
  2814. closePopupOnClick: true
  2815. });
  2816. L.Popup = L.Class.extend({
  2817. includes: L.Mixin.Events,
  2818. options: {
  2819. minWidth: 50,
  2820. maxWidth: 300,
  2821. maxHeight: null,
  2822. autoPan: true,
  2823. closeButton: true,
  2824. offset: [0, 7],
  2825. autoPanPadding: [5, 5],
  2826. keepInView: false,
  2827. className: '',
  2828. zoomAnimation: true
  2829. },
  2830. initialize: function (options, source) {
  2831. L.setOptions(this, options);
  2832. this._source = source;
  2833. this._animated = L.Browser.any3d && this.options.zoomAnimation;
  2834. this._isOpen = false;
  2835. },
  2836. onAdd: function (map) {
  2837. this._map = map;
  2838. if (!this._container) {
  2839. this._initLayout();
  2840. }
  2841. this._updateContent();
  2842. var animFade = map.options.fadeAnimation;
  2843. if (animFade) {
  2844. L.DomUtil.setOpacity(this._container, 0);
  2845. }
  2846. map._panes.popupPane.appendChild(this._container);
  2847. map.on(this._getEvents(), this);
  2848. this._update();
  2849. if (animFade) {
  2850. L.DomUtil.setOpacity(this._container, 1);
  2851. }
  2852. this.fire('open');
  2853. map.fire('popupopen', {popup: this});
  2854. if (this._source) {
  2855. this._source.fire('popupopen', {popup: this});
  2856. }
  2857. },
  2858. addTo: function (map) {
  2859. map.addLayer(this);
  2860. return this;
  2861. },
  2862. openOn: function (map) {
  2863. map.openPopup(this);
  2864. return this;
  2865. },
  2866. onRemove: function (map) {
  2867. map._panes.popupPane.removeChild(this._container);
  2868. L.Util.falseFn(this._container.offsetWidth); // force reflow
  2869. map.off(this._getEvents(), this);
  2870. if (map.options.fadeAnimation) {
  2871. L.DomUtil.setOpacity(this._container, 0);
  2872. }
  2873. this._map = null;
  2874. this.fire('close');
  2875. map.fire('popupclose', {popup: this});
  2876. if (this._source) {
  2877. this._source.fire('popupclose', {popup: this});
  2878. }
  2879. },
  2880. setLatLng: function (latlng) {
  2881. this._latlng = L.latLng(latlng);
  2882. this._update();
  2883. return this;
  2884. },
  2885. setContent: function (content) {
  2886. this._content = content;
  2887. this._update();
  2888. return this;
  2889. },
  2890. _getEvents: function () {
  2891. var events = {
  2892. viewreset: this._updatePosition
  2893. };
  2894. if (this._animated) {
  2895. events.zoomanim = this._zoomAnimation;
  2896. }
  2897. if ('closeOnClick' in this.options ? this.options.closeOnClick : this._map.options.closePopupOnClick) {
  2898. events.preclick = this._close;
  2899. }
  2900. if (this.options.keepInView) {
  2901. events.moveend = this._adjustPan;
  2902. }
  2903. return events;
  2904. },
  2905. _close: function () {
  2906. if (this._map) {
  2907. this._map.closePopup(this);
  2908. }
  2909. },
  2910. _initLayout: function () {
  2911. var prefix = 'leaflet-popup',
  2912. containerClass = prefix + ' ' + this.options.className + ' leaflet-zoom-' +
  2913. (this._animated ? 'animated' : 'hide'),
  2914. container = this._container = L.DomUtil.create('div', containerClass),
  2915. closeButton;
  2916. if (this.options.closeButton) {
  2917. closeButton = this._closeButton =
  2918. L.DomUtil.create('a', prefix + '-close-button', container);
  2919. closeButton.href = '#close';
  2920. closeButton.innerHTML = '&#215;';
  2921. L.DomEvent.disableClickPropagation(closeButton);
  2922. L.DomEvent.on(closeButton, 'click', this._onCloseButtonClick, this);
  2923. }
  2924. var wrapper = this._wrapper =
  2925. L.DomUtil.create('div', prefix + '-content-wrapper', container);
  2926. L.DomEvent.disableClickPropagation(wrapper);
  2927. this._contentNode = L.DomUtil.create('div', prefix + '-content', wrapper);
  2928. L.DomEvent.on(this._contentNode, 'mousewheel', L.DomEvent.stopPropagation);
  2929. L.DomEvent.on(this._contentNode, 'MozMousePixelScroll', L.DomEvent.stopPropagation);
  2930. L.DomEvent.on(wrapper, 'contextmenu', L.DomEvent.stopPropagation);
  2931. this._tipContainer = L.DomUtil.create('div', prefix + '-tip-container', container);
  2932. this._tip = L.DomUtil.create('div', prefix + '-tip', this._tipContainer);
  2933. },
  2934. _update: function () {
  2935. if (!this._map) { return; }
  2936. this._container.style.visibility = 'hidden';
  2937. this._updateContent();
  2938. this._updateLayout();
  2939. this._updatePosition();
  2940. this._container.style.visibility = '';
  2941. this._adjustPan();
  2942. },
  2943. _updateContent: function () {
  2944. if (!this._content) { return; }
  2945. if (typeof this._content === 'string') {
  2946. this._contentNode.innerHTML = this._content;
  2947. } else {
  2948. while (this._contentNode.hasChildNodes()) {
  2949. this._contentNode.removeChild(this._contentNode.firstChild);
  2950. }
  2951. this._contentNode.appendChild(this._content);
  2952. }
  2953. this.fire('contentupdate');
  2954. },
  2955. _updateLayout: function () {
  2956. var container = this._contentNode,
  2957. style = container.style;
  2958. style.width = '';
  2959. style.whiteSpace = 'nowrap';
  2960. var width = container.offsetWidth;
  2961. width = Math.min(width, this.options.maxWidth);
  2962. width = Math.max(width, this.options.minWidth);
  2963. style.width = (width + 1) + 'px';
  2964. style.whiteSpace = '';
  2965. style.height = '';
  2966. var height = container.offsetHeight,
  2967. maxHeight = this.options.maxHeight,
  2968. scrolledClass = 'leaflet-popup-scrolled';
  2969. if (maxHeight && height > maxHeight) {
  2970. style.height = maxHeight + 'px';
  2971. L.DomUtil.addClass(container, scrolledClass);
  2972. } else {
  2973. L.DomUtil.removeClass(container, scrolledClass);
  2974. }
  2975. this._containerWidth = this._container.offsetWidth;
  2976. },
  2977. _updatePosition: function () {
  2978. if (!this._map) { return; }
  2979. var pos = this._map.latLngToLayerPoint(this._latlng),
  2980. animated = this._animated,
  2981. offset = L.point(this.options.offset);
  2982. if (animated) {
  2983. L.DomUtil.setPosition(this._container, pos);
  2984. }
  2985. this._containerBottom = -offset.y - (animated ? 0 : pos.y);
  2986. this._containerLeft = -Math.round(this._containerWidth / 2) + offset.x + (animated ? 0 : pos.x);
  2987. // bottom position the popup in case the height of the popup changes (images loading etc)
  2988. this._container.style.bottom = this._containerBottom + 'px';
  2989. this._container.style.left = this._containerLeft + 'px';
  2990. },
  2991. _zoomAnimation: function (opt) {
  2992. var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center);
  2993. L.DomUtil.setPosition(this._container, pos);
  2994. },
  2995. _adjustPan: function () {
  2996. if (!this.options.autoPan) { return; }
  2997. var map = this._map,
  2998. containerHeight = this._container.offsetHeight,
  2999. containerWidth = this._containerWidth,
  3000. layerPos = new L.Point(this._containerLeft, -containerHeight - this._containerBottom);
  3001. if (this._animated) {
  3002. layerPos._add(L.DomUtil.getPosition(this._container));
  3003. }
  3004. var containerPos = map.layerPointToContainerPoint(layerPos),
  3005. padding = L.point(this.options.autoPanPadding),
  3006. size = map.getSize(),
  3007. dx = 0,
  3008. dy = 0;
  3009. if (containerPos.x + containerWidth > size.x) { // right
  3010. dx = containerPos.x + containerWidth - size.x + padding.x;
  3011. }
  3012. if (containerPos.x - dx < 0) { // left
  3013. dx = containerPos.x - padding.x;
  3014. }
  3015. if (containerPos.y + containerHeight > size.y) { // bottom
  3016. dy = containerPos.y + containerHeight - size.y + padding.y;
  3017. }
  3018. if (containerPos.y - dy < 0) { // top
  3019. dy = containerPos.y - padding.y;
  3020. }
  3021. if (dx || dy) {
  3022. map
  3023. .fire('autopanstart')
  3024. .panBy([dx, dy]);
  3025. }
  3026. },
  3027. _onCloseButtonClick: function (e) {
  3028. this._close();
  3029. L.DomEvent.stop(e);
  3030. }
  3031. });
  3032. L.popup = function (options, source) {
  3033. return new L.Popup(options, source);
  3034. };
  3035. L.Map.include({
  3036. openPopup: function (popup, latlng, options) { // (Popup) or (String || HTMLElement, LatLng[, Object])
  3037. this.closePopup();
  3038. if (!(popup instanceof L.Popup)) {
  3039. var content = popup;
  3040. popup = new L.Popup(options)
  3041. .setLatLng(latlng)
  3042. .setContent(content);
  3043. }
  3044. popup._isOpen = true;
  3045. this._popup = popup;
  3046. return this.addLayer(popup);
  3047. },
  3048. closePopup: function (popup) {
  3049. if (!popup || popup === this._popup) {
  3050. popup = this._popup;
  3051. this._popup = null;
  3052. }
  3053. if (popup) {
  3054. this.removeLayer(popup);
  3055. popup._isOpen = false;
  3056. }
  3057. return this;
  3058. }
  3059. });
  3060. /*
  3061. * Popup extension to L.Marker, adding popup-related methods.
  3062. */
  3063. L.Marker.include({
  3064. openPopup: function () {
  3065. if (this._popup && this._map && !this._map.hasLayer(this._popup)) {
  3066. this._popup.setLatLng(this._latlng);
  3067. this._map.openPopup(this._popup);
  3068. }
  3069. return this;
  3070. },
  3071. closePopup: function () {
  3072. if (this._popup) {
  3073. this._popup._close();
  3074. }
  3075. return this;
  3076. },
  3077. togglePopup: function () {
  3078. if (this._popup) {
  3079. if (this._popup._isOpen) {
  3080. this.closePopup();
  3081. } else {
  3082. this.openPopup();
  3083. }
  3084. }
  3085. return this;
  3086. },
  3087. bindPopup: function (content, options) {
  3088. var anchor = L.point(this.options.icon.options.popupAnchor || [0, 0]);
  3089. anchor = anchor.add(L.Popup.prototype.options.offset);
  3090. if (options && options.offset) {
  3091. anchor = anchor.add(options.offset);
  3092. }
  3093. options = L.extend({offset: anchor}, options);
  3094. if (!this._popup) {
  3095. this
  3096. .on('click', this.togglePopup, this)
  3097. .on('remove', this.closePopup, this)
  3098. .on('move', this._movePopup, this);
  3099. }
  3100. if (content instanceof L.Popup) {
  3101. L.setOptions(content, options);
  3102. this._popup = content;
  3103. } else {
  3104. this._popup = new L.Popup(options, this)
  3105. .setContent(content);
  3106. }
  3107. return this;
  3108. },
  3109. setPopupContent: function (content) {
  3110. if (this._popup) {
  3111. this._popup.setContent(content);
  3112. }
  3113. return this;
  3114. },
  3115. unbindPopup: function () {
  3116. if (this._popup) {
  3117. this._popup = null;
  3118. this
  3119. .off('click', this.togglePopup)
  3120. .off('remove', this.closePopup)
  3121. .off('move', this._movePopup);
  3122. }
  3123. return this;
  3124. },
  3125. _movePopup: function (e) {
  3126. this._popup.setLatLng(e.latlng);
  3127. }
  3128. });
  3129. /*
  3130. * L.LayerGroup is a class to combine several layers into one so that
  3131. * you can manipulate the group (e.g. add/remove it) as one layer.
  3132. */
  3133. L.LayerGroup = L.Class.extend({
  3134. initialize: function (layers) {
  3135. this._layers = {};
  3136. var i, len;
  3137. if (layers) {
  3138. for (i = 0, len = layers.length; i < len; i++) {
  3139. this.addLayer(layers[i]);
  3140. }
  3141. }
  3142. },
  3143. addLayer: function (layer) {
  3144. var id = this.getLayerId(layer);
  3145. this._layers[id] = layer;
  3146. if (this._map) {
  3147. this._map.addLayer(layer);
  3148. }
  3149. return this;
  3150. },
  3151. removeLayer: function (layer) {
  3152. var id = layer in this._layers ? layer : this.getLayerId(layer);
  3153. if (this._map && this._layers[id]) {
  3154. this._map.removeLayer(this._layers[id]);
  3155. }
  3156. delete this._layers[id];
  3157. return this;
  3158. },
  3159. hasLayer: function (layer) {
  3160. if (!layer) { return false; }
  3161. return (layer in this._layers || this.getLayerId(layer) in this._layers);
  3162. },
  3163. clearLayers: function () {
  3164. this.eachLayer(this.removeLayer, this);
  3165. return this;
  3166. },
  3167. invoke: function (methodName) {
  3168. var args = Array.prototype.slice.call(arguments, 1),
  3169. i, layer;
  3170. for (i in this._layers) {
  3171. layer = this._layers[i];
  3172. if (layer[methodName]) {
  3173. layer[methodName].apply(layer, args);
  3174. }
  3175. }
  3176. return this;
  3177. },
  3178. onAdd: function (map) {
  3179. this._map = map;
  3180. this.eachLayer(map.addLayer, map);
  3181. },
  3182. onRemove: function (map) {
  3183. this.eachLayer(map.removeLayer, map);
  3184. this._map = null;
  3185. },
  3186. addTo: function (map) {
  3187. map.addLayer(this);
  3188. return this;
  3189. },
  3190. eachLayer: function (method, context) {
  3191. for (var i in this._layers) {
  3192. method.call(context, this._layers[i]);
  3193. }
  3194. return this;
  3195. },
  3196. getLayer: function (id) {
  3197. return this._layers[id];
  3198. },
  3199. getLayers: function () {
  3200. var layers = [];
  3201. for (var i in this._layers) {
  3202. layers.push(this._layers[i]);
  3203. }
  3204. return layers;
  3205. },
  3206. setZIndex: function (zIndex) {
  3207. return this.invoke('setZIndex', zIndex);
  3208. },
  3209. getLayerId: function (layer) {
  3210. return L.stamp(layer);
  3211. }
  3212. });
  3213. L.layerGroup = function (layers) {
  3214. return new L.LayerGroup(layers);
  3215. };
  3216. /*
  3217. * L.FeatureGroup extends L.LayerGroup by introducing mouse events and additional methods
  3218. * shared between a group of interactive layers (like vectors or markers).
  3219. */
  3220. L.FeatureGroup = L.LayerGroup.extend({
  3221. includes: L.Mixin.Events,
  3222. statics: {
  3223. EVENTS: 'click dblclick mouseover mouseout mousemove contextmenu popupopen popupclose'
  3224. },
  3225. addLayer: function (layer) {
  3226. if (this.hasLayer(layer)) {
  3227. return this;
  3228. }
  3229. layer.on(L.FeatureGroup.EVENTS, this._propagateEvent, this);
  3230. L.LayerGroup.prototype.addLayer.call(this, layer);
  3231. if (this._popupContent && layer.bindPopup) {
  3232. layer.bindPopup(this._popupContent, this._popupOptions);
  3233. }
  3234. return this.fire('layeradd', {layer: layer});
  3235. },
  3236. removeLayer: function (layer) {
  3237. if (!this.hasLayer(layer)) {
  3238. return this;
  3239. }
  3240. if (layer in this._layers) {
  3241. layer = this._layers[layer];
  3242. }
  3243. layer.off(L.FeatureGroup.EVENTS, this._propagateEvent, this);
  3244. L.LayerGroup.prototype.removeLayer.call(this, layer);
  3245. if (this._popupContent) {
  3246. this.invoke('unbindPopup');
  3247. }
  3248. return this.fire('layerremove', {layer: layer});
  3249. },
  3250. bindPopup: function (content, options) {
  3251. this._popupContent = content;
  3252. this._popupOptions = options;
  3253. return this.invoke('bindPopup', content, options);
  3254. },
  3255. setStyle: function (style) {
  3256. return this.invoke('setStyle', style);
  3257. },
  3258. bringToFront: function () {
  3259. return this.invoke('bringToFront');
  3260. },
  3261. bringToBack: function () {
  3262. return this.invoke('bringToBack');
  3263. },
  3264. getBounds: function () {
  3265. var bounds = new L.LatLngBounds();
  3266. this.eachLayer(function (layer) {
  3267. bounds.extend(layer instanceof L.Marker ? layer.getLatLng() : layer.getBounds());
  3268. });
  3269. return bounds;
  3270. },
  3271. _propagateEvent: function (e) {
  3272. if (!e.layer) {
  3273. e.layer = e.target;
  3274. }
  3275. e.target = this;
  3276. this.fire(e.type, e);
  3277. }
  3278. });
  3279. L.featureGroup = function (layers) {
  3280. return new L.FeatureGroup(layers);
  3281. };
  3282. /*
  3283. * L.Path is a base class for rendering vector paths on a map. Inherited by Polyline, Circle, etc.
  3284. */
  3285. L.Path = L.Class.extend({
  3286. includes: [L.Mixin.Events],
  3287. statics: {
  3288. // how much to extend the clip area around the map view
  3289. // (relative to its size, e.g. 0.5 is half the screen in each direction)
  3290. // set it so that SVG element doesn't exceed 1280px (vectors flicker on dragend if it is)
  3291. CLIP_PADDING: (function () {
  3292. var max = L.Browser.mobile ? 1280 : 2000,
  3293. target = (max / Math.max(window.outerWidth, window.outerHeight) - 1) / 2;
  3294. return Math.max(0, Math.min(0.5, target));
  3295. })()
  3296. },
  3297. options: {
  3298. stroke: true,
  3299. color: '#0033ff',
  3300. dashArray: null,
  3301. weight: 5,
  3302. opacity: 0.5,
  3303. fill: false,
  3304. fillColor: null, //same as color by default
  3305. fillOpacity: 0.2,
  3306. clickable: true
  3307. },
  3308. initialize: function (options) {
  3309. L.setOptions(this, options);
  3310. },
  3311. onAdd: function (map) {
  3312. this._map = map;
  3313. if (!this._container) {
  3314. this._initElements();
  3315. this._initEvents();
  3316. }
  3317. this.projectLatlngs();
  3318. this._updatePath();
  3319. if (this._container) {
  3320. this._map._pathRoot.appendChild(this._container);
  3321. }
  3322. this.fire('add');
  3323. map.on({
  3324. 'viewreset': this.projectLatlngs,
  3325. 'moveend': this._updatePath
  3326. }, this);
  3327. },
  3328. addTo: function (map) {
  3329. map.addLayer(this);
  3330. return this;
  3331. },
  3332. onRemove: function (map) {
  3333. map._pathRoot.removeChild(this._container);
  3334. // Need to fire remove event before we set _map to null as the event hooks might need the object
  3335. this.fire('remove');
  3336. this._map = null;
  3337. if (L.Browser.vml) {
  3338. this._container = null;
  3339. this._stroke = null;
  3340. this._fill = null;
  3341. }
  3342. map.off({
  3343. 'viewreset': this.projectLatlngs,
  3344. 'moveend': this._updatePath
  3345. }, this);
  3346. },
  3347. projectLatlngs: function () {
  3348. // do all projection stuff here
  3349. },
  3350. setStyle: function (style) {
  3351. L.setOptions(this, style);
  3352. if (this._container) {
  3353. this._updateStyle();
  3354. }
  3355. return this;
  3356. },
  3357. redraw: function () {
  3358. if (this._map) {
  3359. this.projectLatlngs();
  3360. this._updatePath();
  3361. }
  3362. return this;
  3363. }
  3364. });
  3365. L.Map.include({
  3366. _updatePathViewport: function () {
  3367. var p = L.Path.CLIP_PADDING,
  3368. size = this.getSize(),
  3369. panePos = L.DomUtil.getPosition(this._mapPane),
  3370. min = panePos.multiplyBy(-1)._subtract(size.multiplyBy(p)._round()),
  3371. max = min.add(size.multiplyBy(1 + p * 2)._round());
  3372. this._pathViewport = new L.Bounds(min, max);
  3373. }
  3374. });
  3375. /*
  3376. * Extends L.Path with SVG-specific rendering code.
  3377. */
  3378. L.Path.SVG_NS = 'http://www.w3.org/2000/svg';
  3379. L.Browser.svg = !!(document.createElementNS && document.createElementNS(L.Path.SVG_NS, 'svg').createSVGRect);
  3380. L.Path = L.Path.extend({
  3381. statics: {
  3382. SVG: L.Browser.svg
  3383. },
  3384. bringToFront: function () {
  3385. var root = this._map._pathRoot,
  3386. path = this._container;
  3387. if (path && root.lastChild !== path) {
  3388. root.appendChild(path);
  3389. }
  3390. return this;
  3391. },
  3392. bringToBack: function () {
  3393. var root = this._map._pathRoot,
  3394. path = this._container,
  3395. first = root.firstChild;
  3396. if (path && first !== path) {
  3397. root.insertBefore(path, first);
  3398. }
  3399. return this;
  3400. },
  3401. getPathString: function () {
  3402. // form path string here
  3403. },
  3404. _createElement: function (name) {
  3405. return document.createElementNS(L.Path.SVG_NS, name);
  3406. },
  3407. _initElements: function () {
  3408. this._map._initPathRoot();
  3409. this._initPath();
  3410. this._initStyle();
  3411. },
  3412. _initPath: function () {
  3413. this._container = this._createElement('g');
  3414. this._path = this._createElement('path');
  3415. this._container.appendChild(this._path);
  3416. },
  3417. _initStyle: function () {
  3418. if (this.options.stroke) {
  3419. this._path.setAttribute('stroke-linejoin', 'round');
  3420. this._path.setAttribute('stroke-linecap', 'round');
  3421. }
  3422. if (this.options.fill) {
  3423. this._path.setAttribute('fill-rule', 'evenodd');
  3424. }
  3425. if (this.options.pointerEvents) {
  3426. this._path.setAttribute('pointer-events', this.options.pointerEvents);
  3427. }
  3428. if (!this.options.clickable && !this.options.pointerEvents) {
  3429. this._path.setAttribute('pointer-events', 'none');
  3430. }
  3431. this._updateStyle();
  3432. },
  3433. _updateStyle: function () {
  3434. if (this.options.stroke) {
  3435. this._path.setAttribute('stroke', this.options.color);
  3436. this._path.setAttribute('stroke-opacity', this.options.opacity);
  3437. this._path.setAttribute('stroke-width', this.options.weight);
  3438. if (this.options.dashArray) {
  3439. this._path.setAttribute('stroke-dasharray', this.options.dashArray);
  3440. } else {
  3441. this._path.removeAttribute('stroke-dasharray');
  3442. }
  3443. } else {
  3444. this._path.setAttribute('stroke', 'none');
  3445. }
  3446. if (this.options.fill) {
  3447. this._path.setAttribute('fill', this.options.fillColor || this.options.color);
  3448. this._path.setAttribute('fill-opacity', this.options.fillOpacity);
  3449. } else {
  3450. this._path.setAttribute('fill', 'none');
  3451. }
  3452. },
  3453. _updatePath: function () {
  3454. var str = this.getPathString();
  3455. if (!str) {
  3456. // fix webkit empty string parsing bug
  3457. str = 'M0 0';
  3458. }
  3459. this._path.setAttribute('d', str);
  3460. },
  3461. // TODO remove duplication with L.Map
  3462. _initEvents: function () {
  3463. if (this.options.clickable) {
  3464. if (L.Browser.svg || !L.Browser.vml) {
  3465. this._path.setAttribute('class', 'leaflet-clickable');
  3466. }
  3467. L.DomEvent.on(this._container, 'click', this._onMouseClick, this);
  3468. var events = ['dblclick', 'mousedown', 'mouseover',
  3469. 'mouseout', 'mousemove', 'contextmenu'];
  3470. for (var i = 0; i < events.length; i++) {
  3471. L.DomEvent.on(this._container, events[i], this._fireMouseEvent, this);
  3472. }
  3473. }
  3474. },
  3475. _onMouseClick: function (e) {
  3476. if (this._map.dragging && this._map.dragging.moved()) { return; }
  3477. this._fireMouseEvent(e);
  3478. },
  3479. _fireMouseEvent: function (e) {
  3480. if (!this.hasEventListeners(e.type)) { return; }
  3481. var map = this._map,
  3482. containerPoint = map.mouseEventToContainerPoint(e),
  3483. layerPoint = map.containerPointToLayerPoint(containerPoint),
  3484. latlng = map.layerPointToLatLng(layerPoint);
  3485. this.fire(e.type, {
  3486. latlng: latlng,
  3487. layerPoint: layerPoint,
  3488. containerPoint: containerPoint,
  3489. originalEvent: e
  3490. });
  3491. if (e.type === 'contextmenu') {
  3492. L.DomEvent.preventDefault(e);
  3493. }
  3494. if (e.type !== 'mousemove') {
  3495. L.DomEvent.stopPropagation(e);
  3496. }
  3497. }
  3498. });
  3499. L.Map.include({
  3500. _initPathRoot: function () {
  3501. if (!this._pathRoot) {
  3502. this._pathRoot = L.Path.prototype._createElement('svg');
  3503. this._panes.overlayPane.appendChild(this._pathRoot);
  3504. if (this.options.zoomAnimation && L.Browser.any3d) {
  3505. this._pathRoot.setAttribute('class', ' leaflet-zoom-animated');
  3506. this.on({
  3507. 'zoomanim': this._animatePathZoom,
  3508. 'zoomend': this._endPathZoom
  3509. });
  3510. } else {
  3511. this._pathRoot.setAttribute('class', ' leaflet-zoom-hide');
  3512. }
  3513. this.on('moveend', this._updateSvgViewport);
  3514. this._updateSvgViewport();
  3515. }
  3516. },
  3517. _animatePathZoom: function (e) {
  3518. var scale = this.getZoomScale(e.zoom),
  3519. offset = this._getCenterOffset(e.center)._multiplyBy(-scale)._add(this._pathViewport.min);
  3520. this._pathRoot.style[L.DomUtil.TRANSFORM] =
  3521. L.DomUtil.getTranslateString(offset) + ' scale(' + scale + ') ';
  3522. this._pathZooming = true;
  3523. },
  3524. _endPathZoom: function () {
  3525. this._pathZooming = false;
  3526. },
  3527. _updateSvgViewport: function () {
  3528. if (this._pathZooming) {
  3529. // Do not update SVGs while a zoom animation is going on otherwise the animation will break.
  3530. // When the zoom animation ends we will be updated again anyway
  3531. // This fixes the case where you do a momentum move and zoom while the move is still ongoing.
  3532. return;
  3533. }
  3534. this._updatePathViewport();
  3535. var vp = this._pathViewport,
  3536. min = vp.min,
  3537. max = vp.max,
  3538. width = max.x - min.x,
  3539. height = max.y - min.y,
  3540. root = this._pathRoot,
  3541. pane = this._panes.overlayPane;
  3542. // Hack to make flicker on drag end on mobile webkit less irritating
  3543. if (L.Browser.mobileWebkit) {
  3544. pane.removeChild(root);
  3545. }
  3546. L.DomUtil.setPosition(root, min);
  3547. root.setAttribute('width', width);
  3548. root.setAttribute('height', height);
  3549. root.setAttribute('viewBox', [min.x, min.y, width, height].join(' '));
  3550. if (L.Browser.mobileWebkit) {
  3551. pane.appendChild(root);
  3552. }
  3553. }
  3554. });
  3555. /*
  3556. * Popup extension to L.Path (polylines, polygons, circles), adding popup-related methods.
  3557. */
  3558. L.Path.include({
  3559. bindPopup: function (content, options) {
  3560. if (content instanceof L.Popup) {
  3561. this._popup = content;
  3562. } else {
  3563. if (!this._popup || options) {
  3564. this._popup = new L.Popup(options, this);
  3565. }
  3566. this._popup.setContent(content);
  3567. }
  3568. if (!this._popupHandlersAdded) {
  3569. this
  3570. .on('click', this._openPopup, this)
  3571. .on('remove', this.closePopup, this);
  3572. this._popupHandlersAdded = true;
  3573. }
  3574. return this;
  3575. },
  3576. unbindPopup: function () {
  3577. if (this._popup) {
  3578. this._popup = null;
  3579. this
  3580. .off('click', this._openPopup)
  3581. .off('remove', this.closePopup);
  3582. this._popupHandlersAdded = false;
  3583. }
  3584. return this;
  3585. },
  3586. openPopup: function (latlng) {
  3587. if (this._popup) {
  3588. // open the popup from one of the path's points if not specified
  3589. latlng = latlng || this._latlng ||
  3590. this._latlngs[Math.floor(this._latlngs.length / 2)];
  3591. this._openPopup({latlng: latlng});
  3592. }
  3593. return this;
  3594. },
  3595. closePopup: function () {
  3596. if (this._popup) {
  3597. this._popup._close();
  3598. }
  3599. return this;
  3600. },
  3601. _openPopup: function (e) {
  3602. this._popup.setLatLng(e.latlng);
  3603. this._map.openPopup(this._popup);
  3604. }
  3605. });
  3606. /*
  3607. * Vector rendering for IE6-8 through VML.
  3608. * Thanks to Dmitry Baranovsky and his Raphael library for inspiration!
  3609. */
  3610. L.Browser.vml = !L.Browser.svg && (function () {
  3611. try {
  3612. var div = document.createElement('div');
  3613. div.innerHTML = '<v:shape adj="1"/>';
  3614. var shape = div.firstChild;
  3615. shape.style.behavior = 'url(#default#VML)';
  3616. return shape && (typeof shape.adj === 'object');
  3617. } catch (e) {
  3618. return false;
  3619. }
  3620. }());
  3621. L.Path = L.Browser.svg || !L.Browser.vml ? L.Path : L.Path.extend({
  3622. statics: {
  3623. VML: true,
  3624. CLIP_PADDING: 0.02
  3625. },
  3626. _createElement: (function () {
  3627. try {
  3628. document.namespaces.add('lvml', 'urn:schemas-microsoft-com:vml');
  3629. return function (name) {
  3630. return document.createElement('<lvml:' + name + ' class="lvml">');
  3631. };
  3632. } catch (e) {
  3633. return function (name) {
  3634. return document.createElement(
  3635. '<' + name + ' xmlns="urn:schemas-microsoft.com:vml" class="lvml">');
  3636. };
  3637. }
  3638. }()),
  3639. _initPath: function () {
  3640. var container = this._container = this._createElement('shape');
  3641. L.DomUtil.addClass(container, 'leaflet-vml-shape');
  3642. if (this.options.clickable) {
  3643. L.DomUtil.addClass(container, 'leaflet-clickable');
  3644. }
  3645. container.coordsize = '1 1';
  3646. this._path = this._createElement('path');
  3647. container.appendChild(this._path);
  3648. this._map._pathRoot.appendChild(container);
  3649. },
  3650. _initStyle: function () {
  3651. this._updateStyle();
  3652. },
  3653. _updateStyle: function () {
  3654. var stroke = this._stroke,
  3655. fill = this._fill,
  3656. options = this.options,
  3657. container = this._container;
  3658. container.stroked = options.stroke;
  3659. container.filled = options.fill;
  3660. if (options.stroke) {
  3661. if (!stroke) {
  3662. stroke = this._stroke = this._createElement('stroke');
  3663. stroke.endcap = 'round';
  3664. container.appendChild(stroke);
  3665. }
  3666. stroke.weight = options.weight + 'px';
  3667. stroke.color = options.color;
  3668. stroke.opacity = options.opacity;
  3669. if (options.dashArray) {
  3670. stroke.dashStyle = options.dashArray instanceof Array ?
  3671. options.dashArray.join(' ') :
  3672. options.dashArray.replace(/( *, *)/g, ' ');
  3673. } else {
  3674. stroke.dashStyle = '';
  3675. }
  3676. } else if (stroke) {
  3677. container.removeChild(stroke);
  3678. this._stroke = null;
  3679. }
  3680. if (options.fill) {
  3681. if (!fill) {
  3682. fill = this._fill = this._createElement('fill');
  3683. container.appendChild(fill);
  3684. }
  3685. fill.color = options.fillColor || options.color;
  3686. fill.opacity = options.fillOpacity;
  3687. } else if (fill) {
  3688. container.removeChild(fill);
  3689. this._fill = null;
  3690. }
  3691. },
  3692. _updatePath: function () {
  3693. var style = this._container.style;
  3694. style.display = 'none';
  3695. this._path.v = this.getPathString() + ' '; // the space fixes IE empty path string bug
  3696. style.display = '';
  3697. }
  3698. });
  3699. L.Map.include(L.Browser.svg || !L.Browser.vml ? {} : {
  3700. _initPathRoot: function () {
  3701. if (this._pathRoot) { return; }
  3702. var root = this._pathRoot = document.createElement('div');
  3703. root.className = 'leaflet-vml-container';
  3704. this._panes.overlayPane.appendChild(root);
  3705. this.on('moveend', this._updatePathViewport);
  3706. this._updatePathViewport();
  3707. }
  3708. });
  3709. /*
  3710. * Vector rendering for all browsers that support canvas.
  3711. */
  3712. L.Browser.canvas = (function () {
  3713. return !!document.createElement('canvas').getContext;
  3714. }());
  3715. L.Path = (L.Path.SVG && !window.L_PREFER_CANVAS) || !L.Browser.canvas ? L.Path : L.Path.extend({
  3716. statics: {
  3717. //CLIP_PADDING: 0.02, // not sure if there's a need to set it to a small value
  3718. CANVAS: true,
  3719. SVG: false
  3720. },
  3721. redraw: function () {
  3722. if (this._map) {
  3723. this.projectLatlngs();
  3724. this._requestUpdate();
  3725. }
  3726. return this;
  3727. },
  3728. setStyle: function (style) {
  3729. L.setOptions(this, style);
  3730. if (this._map) {
  3731. this._updateStyle();
  3732. this._requestUpdate();
  3733. }
  3734. return this;
  3735. },
  3736. onRemove: function (map) {
  3737. map
  3738. .off('viewreset', this.projectLatlngs, this)
  3739. .off('moveend', this._updatePath, this);
  3740. if (this.options.clickable) {
  3741. this._map.off('click', this._onClick, this);
  3742. this._map.off('mousemove', this._onMouseMove, this);
  3743. }
  3744. this._requestUpdate();
  3745. this._map = null;
  3746. },
  3747. _requestUpdate: function () {
  3748. if (this._map && !L.Path._updateRequest) {
  3749. L.Path._updateRequest = L.Util.requestAnimFrame(this._fireMapMoveEnd, this._map);
  3750. }
  3751. },
  3752. _fireMapMoveEnd: function () {
  3753. L.Path._updateRequest = null;
  3754. this.fire('moveend');
  3755. },
  3756. _initElements: function () {
  3757. this._map._initPathRoot();
  3758. this._ctx = this._map._canvasCtx;
  3759. },
  3760. _updateStyle: function () {
  3761. var options = this.options;
  3762. if (options.stroke) {
  3763. this._ctx.lineWidth = options.weight;
  3764. this._ctx.strokeStyle = options.color;
  3765. }
  3766. if (options.fill) {
  3767. this._ctx.fillStyle = options.fillColor || options.color;
  3768. }
  3769. },
  3770. _drawPath: function () {
  3771. var i, j, len, len2, point, drawMethod;
  3772. this._ctx.beginPath();
  3773. for (i = 0, len = this._parts.length; i < len; i++) {
  3774. for (j = 0, len2 = this._parts[i].length; j < len2; j++) {
  3775. point = this._parts[i][j];
  3776. drawMethod = (j === 0 ? 'move' : 'line') + 'To';
  3777. this._ctx[drawMethod](point.x, point.y);
  3778. }
  3779. // TODO refactor ugly hack
  3780. if (this instanceof L.Polygon) {
  3781. this._ctx.closePath();
  3782. }
  3783. }
  3784. },
  3785. _checkIfEmpty: function () {
  3786. return !this._parts.length;
  3787. },
  3788. _updatePath: function () {
  3789. if (this._checkIfEmpty()) { return; }
  3790. var ctx = this._ctx,
  3791. options = this.options;
  3792. this._drawPath();
  3793. ctx.save();
  3794. this._updateStyle();
  3795. if (options.fill) {
  3796. ctx.globalAlpha = options.fillOpacity;
  3797. ctx.fill();
  3798. }
  3799. if (options.stroke) {
  3800. ctx.globalAlpha = options.opacity;
  3801. ctx.stroke();
  3802. }
  3803. ctx.restore();
  3804. // TODO optimization: 1 fill/stroke for all features with equal style instead of 1 for each feature
  3805. },
  3806. _initEvents: function () {
  3807. if (this.options.clickable) {
  3808. // TODO dblclick
  3809. this._map.on('mousemove', this._onMouseMove, this);
  3810. this._map.on('click', this._onClick, this);
  3811. }
  3812. },
  3813. _onClick: function (e) {
  3814. if (this._containsPoint(e.layerPoint)) {
  3815. this.fire('click', e);
  3816. }
  3817. },
  3818. _onMouseMove: function (e) {
  3819. if (!this._map || this._map._animatingZoom) { return; }
  3820. // TODO don't do on each move
  3821. if (this._containsPoint(e.layerPoint)) {
  3822. this._ctx.canvas.style.cursor = 'pointer';
  3823. this._mouseInside = true;
  3824. this.fire('mouseover', e);
  3825. } else if (this._mouseInside) {
  3826. this._ctx.canvas.style.cursor = '';
  3827. this._mouseInside = false;
  3828. this.fire('mouseout', e);
  3829. }
  3830. }
  3831. });
  3832. L.Map.include((L.Path.SVG && !window.L_PREFER_CANVAS) || !L.Browser.canvas ? {} : {
  3833. _initPathRoot: function () {
  3834. var root = this._pathRoot,
  3835. ctx;
  3836. if (!root) {
  3837. root = this._pathRoot = document.createElement('canvas');
  3838. root.style.position = 'absolute';
  3839. ctx = this._canvasCtx = root.getContext('2d');
  3840. ctx.lineCap = 'round';
  3841. ctx.lineJoin = 'round';
  3842. this._panes.overlayPane.appendChild(root);
  3843. if (this.options.zoomAnimation) {
  3844. this._pathRoot.className = 'leaflet-zoom-animated';
  3845. this.on('zoomanim', this._animatePathZoom);
  3846. this.on('zoomend', this._endPathZoom);
  3847. }
  3848. this.on('moveend', this._updateCanvasViewport);
  3849. this._updateCanvasViewport();
  3850. }
  3851. },
  3852. _updateCanvasViewport: function () {
  3853. // don't redraw while zooming. See _updateSvgViewport for more details
  3854. if (this._pathZooming) { return; }
  3855. this._updatePathViewport();
  3856. var vp = this._pathViewport,
  3857. min = vp.min,
  3858. size = vp.max.subtract(min),
  3859. root = this._pathRoot;
  3860. //TODO check if this works properly on mobile webkit
  3861. L.DomUtil.setPosition(root, min);
  3862. root.width = size.x;
  3863. root.height = size.y;
  3864. root.getContext('2d').translate(-min.x, -min.y);
  3865. }
  3866. });
  3867. /*
  3868. * L.LineUtil contains different utility functions for line segments
  3869. * and polylines (clipping, simplification, distances, etc.)
  3870. */
  3871. /*jshint bitwise:false */ // allow bitwise oprations for this file
  3872. L.LineUtil = {
  3873. // Simplify polyline with vertex reduction and Douglas-Peucker simplification.
  3874. // Improves rendering performance dramatically by lessening the number of points to draw.
  3875. simplify: function (/*Point[]*/ points, /*Number*/ tolerance) {
  3876. if (!tolerance || !points.length) {
  3877. return points.slice();
  3878. }
  3879. var sqTolerance = tolerance * tolerance;
  3880. // stage 1: vertex reduction
  3881. points = this._reducePoints(points, sqTolerance);
  3882. // stage 2: Douglas-Peucker simplification
  3883. points = this._simplifyDP(points, sqTolerance);
  3884. return points;
  3885. },
  3886. // distance from a point to a segment between two points
  3887. pointToSegmentDistance: function (/*Point*/ p, /*Point*/ p1, /*Point*/ p2) {
  3888. return Math.sqrt(this._sqClosestPointOnSegment(p, p1, p2, true));
  3889. },
  3890. closestPointOnSegment: function (/*Point*/ p, /*Point*/ p1, /*Point*/ p2) {
  3891. return this._sqClosestPointOnSegment(p, p1, p2);
  3892. },
  3893. // Douglas-Peucker simplification, see http://en.wikipedia.org/wiki/Douglas-Peucker_algorithm
  3894. _simplifyDP: function (points, sqTolerance) {
  3895. var len = points.length,
  3896. ArrayConstructor = typeof Uint8Array !== undefined + '' ? Uint8Array : Array,
  3897. markers = new ArrayConstructor(len);
  3898. markers[0] = markers[len - 1] = 1;
  3899. this._simplifyDPStep(points, markers, sqTolerance, 0, len - 1);
  3900. var i,
  3901. newPoints = [];
  3902. for (i = 0; i < len; i++) {
  3903. if (markers[i]) {
  3904. newPoints.push(points[i]);
  3905. }
  3906. }
  3907. return newPoints;
  3908. },
  3909. _simplifyDPStep: function (points, markers, sqTolerance, first, last) {
  3910. var maxSqDist = 0,
  3911. index, i, sqDist;
  3912. for (i = first + 1; i <= last - 1; i++) {
  3913. sqDist = this._sqClosestPointOnSegment(points[i], points[first], points[last], true);
  3914. if (sqDist > maxSqDist) {
  3915. index = i;
  3916. maxSqDist = sqDist;
  3917. }
  3918. }
  3919. if (maxSqDist > sqTolerance) {
  3920. markers[index] = 1;
  3921. this._simplifyDPStep(points, markers, sqTolerance, first, index);
  3922. this._simplifyDPStep(points, markers, sqTolerance, index, last);
  3923. }
  3924. },
  3925. // reduce points that are too close to each other to a single point
  3926. _reducePoints: function (points, sqTolerance) {
  3927. var reducedPoints = [points[0]];
  3928. for (var i = 1, prev = 0, len = points.length; i < len; i++) {
  3929. if (this._sqDist(points[i], points[prev]) > sqTolerance) {
  3930. reducedPoints.push(points[i]);
  3931. prev = i;
  3932. }
  3933. }
  3934. if (prev < len - 1) {
  3935. reducedPoints.push(points[len - 1]);
  3936. }
  3937. return reducedPoints;
  3938. },
  3939. // Cohen-Sutherland line clipping algorithm.
  3940. // Used to avoid rendering parts of a polyline that are not currently visible.
  3941. clipSegment: function (a, b, bounds, useLastCode) {
  3942. var codeA = useLastCode ? this._lastCode : this._getBitCode(a, bounds),
  3943. codeB = this._getBitCode(b, bounds),
  3944. codeOut, p, newCode;
  3945. // save 2nd code to avoid calculating it on the next segment
  3946. this._lastCode = codeB;
  3947. while (true) {
  3948. // if a,b is inside the clip window (trivial accept)
  3949. if (!(codeA | codeB)) {
  3950. return [a, b];
  3951. // if a,b is outside the clip window (trivial reject)
  3952. } else if (codeA & codeB) {
  3953. return false;
  3954. // other cases
  3955. } else {
  3956. codeOut = codeA || codeB;
  3957. p = this._getEdgeIntersection(a, b, codeOut, bounds);
  3958. newCode = this._getBitCode(p, bounds);
  3959. if (codeOut === codeA) {
  3960. a = p;
  3961. codeA = newCode;
  3962. } else {
  3963. b = p;
  3964. codeB = newCode;
  3965. }
  3966. }
  3967. }
  3968. },
  3969. _getEdgeIntersection: function (a, b, code, bounds) {
  3970. var dx = b.x - a.x,
  3971. dy = b.y - a.y,
  3972. min = bounds.min,
  3973. max = bounds.max;
  3974. if (code & 8) { // top
  3975. return new L.Point(a.x + dx * (max.y - a.y) / dy, max.y);
  3976. } else if (code & 4) { // bottom
  3977. return new L.Point(a.x + dx * (min.y - a.y) / dy, min.y);
  3978. } else if (code & 2) { // right
  3979. return new L.Point(max.x, a.y + dy * (max.x - a.x) / dx);
  3980. } else if (code & 1) { // left
  3981. return new L.Point(min.x, a.y + dy * (min.x - a.x) / dx);
  3982. }
  3983. },
  3984. _getBitCode: function (/*Point*/ p, bounds) {
  3985. var code = 0;
  3986. if (p.x < bounds.min.x) { // left
  3987. code |= 1;
  3988. } else if (p.x > bounds.max.x) { // right
  3989. code |= 2;
  3990. }
  3991. if (p.y < bounds.min.y) { // bottom
  3992. code |= 4;
  3993. } else if (p.y > bounds.max.y) { // top
  3994. code |= 8;
  3995. }
  3996. return code;
  3997. },
  3998. // square distance (to avoid unnecessary Math.sqrt calls)
  3999. _sqDist: function (p1, p2) {
  4000. var dx = p2.x - p1.x,
  4001. dy = p2.y - p1.y;
  4002. return dx * dx + dy * dy;
  4003. },
  4004. // return closest point on segment or distance to that point
  4005. _sqClosestPointOnSegment: function (p, p1, p2, sqDist) {
  4006. var x = p1.x,
  4007. y = p1.y,
  4008. dx = p2.x - x,
  4009. dy = p2.y - y,
  4010. dot = dx * dx + dy * dy,
  4011. t;
  4012. if (dot > 0) {
  4013. t = ((p.x - x) * dx + (p.y - y) * dy) / dot;
  4014. if (t > 1) {
  4015. x = p2.x;
  4016. y = p2.y;
  4017. } else if (t > 0) {
  4018. x += dx * t;
  4019. y += dy * t;
  4020. }
  4021. }
  4022. dx = p.x - x;
  4023. dy = p.y - y;
  4024. return sqDist ? dx * dx + dy * dy : new L.Point(x, y);
  4025. }
  4026. };
  4027. /*
  4028. * L.Polyline is used to display polylines on a map.
  4029. */
  4030. L.Polyline = L.Path.extend({
  4031. initialize: function (latlngs, options) {
  4032. L.Path.prototype.initialize.call(this, options);
  4033. this._latlngs = this._convertLatLngs(latlngs);
  4034. },
  4035. options: {
  4036. // how much to simplify the polyline on each zoom level
  4037. // more = better performance and smoother look, less = more accurate
  4038. smoothFactor: 1.0,
  4039. noClip: false
  4040. },
  4041. projectLatlngs: function () {
  4042. this._originalPoints = [];
  4043. for (var i = 0, len = this._latlngs.length; i < len; i++) {
  4044. this._originalPoints[i] = this._map.latLngToLayerPoint(this._latlngs[i]);
  4045. }
  4046. },
  4047. getPathString: function () {
  4048. for (var i = 0, len = this._parts.length, str = ''; i < len; i++) {
  4049. str += this._getPathPartStr(this._parts[i]);
  4050. }
  4051. return str;
  4052. },
  4053. getLatLngs: function () {
  4054. return this._latlngs;
  4055. },
  4056. setLatLngs: function (latlngs) {
  4057. this._latlngs = this._convertLatLngs(latlngs);
  4058. return this.redraw();
  4059. },
  4060. addLatLng: function (latlng) {
  4061. this._latlngs.push(L.latLng(latlng));
  4062. return this.redraw();
  4063. },
  4064. spliceLatLngs: function () { // (Number index, Number howMany)
  4065. var removed = [].splice.apply(this._latlngs, arguments);
  4066. this._convertLatLngs(this._latlngs, true);
  4067. this.redraw();
  4068. return removed;
  4069. },
  4070. closestLayerPoint: function (p) {
  4071. var minDistance = Infinity, parts = this._parts, p1, p2, minPoint = null;
  4072. for (var j = 0, jLen = parts.length; j < jLen; j++) {
  4073. var points = parts[j];
  4074. for (var i = 1, len = points.length; i < len; i++) {
  4075. p1 = points[i - 1];
  4076. p2 = points[i];
  4077. var sqDist = L.LineUtil._sqClosestPointOnSegment(p, p1, p2, true);
  4078. if (sqDist < minDistance) {
  4079. minDistance = sqDist;
  4080. minPoint = L.LineUtil._sqClosestPointOnSegment(p, p1, p2);
  4081. }
  4082. }
  4083. }
  4084. if (minPoint) {
  4085. minPoint.distance = Math.sqrt(minDistance);
  4086. }
  4087. return minPoint;
  4088. },
  4089. getBounds: function () {
  4090. return new L.LatLngBounds(this.getLatLngs());
  4091. },
  4092. _convertLatLngs: function (latlngs, overwrite) {
  4093. var i, len, target = overwrite ? latlngs : [];
  4094. for (i = 0, len = latlngs.length; i < len; i++) {
  4095. if (L.Util.isArray(latlngs[i]) && typeof latlngs[i][0] !== 'number') {
  4096. return;
  4097. }
  4098. target[i] = L.latLng(latlngs[i]);
  4099. }
  4100. return target;
  4101. },
  4102. _initEvents: function () {
  4103. L.Path.prototype._initEvents.call(this);
  4104. },
  4105. _getPathPartStr: function (points) {
  4106. var round = L.Path.VML;
  4107. for (var j = 0, len2 = points.length, str = '', p; j < len2; j++) {
  4108. p = points[j];
  4109. if (round) {
  4110. p._round();
  4111. }
  4112. str += (j ? 'L' : 'M') + p.x + ' ' + p.y;
  4113. }
  4114. return str;
  4115. },
  4116. _clipPoints: function () {
  4117. var points = this._originalPoints,
  4118. len = points.length,
  4119. i, k, segment;
  4120. if (this.options.noClip) {
  4121. this._parts = [points];
  4122. return;
  4123. }
  4124. this._parts = [];
  4125. var parts = this._parts,
  4126. vp = this._map._pathViewport,
  4127. lu = L.LineUtil;
  4128. for (i = 0, k = 0; i < len - 1; i++) {
  4129. segment = lu.clipSegment(points[i], points[i + 1], vp, i);
  4130. if (!segment) {
  4131. continue;
  4132. }
  4133. parts[k] = parts[k] || [];
  4134. parts[k].push(segment[0]);
  4135. // if segment goes out of screen, or it's the last one, it's the end of the line part
  4136. if ((segment[1] !== points[i + 1]) || (i === len - 2)) {
  4137. parts[k].push(segment[1]);
  4138. k++;
  4139. }
  4140. }
  4141. },
  4142. // simplify each clipped part of the polyline
  4143. _simplifyPoints: function () {
  4144. var parts = this._parts,
  4145. lu = L.LineUtil;
  4146. for (var i = 0, len = parts.length; i < len; i++) {
  4147. parts[i] = lu.simplify(parts[i], this.options.smoothFactor);
  4148. }
  4149. },
  4150. _updatePath: function () {
  4151. if (!this._map) { return; }
  4152. this._clipPoints();
  4153. this._simplifyPoints();
  4154. L.Path.prototype._updatePath.call(this);
  4155. }
  4156. });
  4157. L.polyline = function (latlngs, options) {
  4158. return new L.Polyline(latlngs, options);
  4159. };
  4160. /*
  4161. * L.PolyUtil contains utility functions for polygons (clipping, etc.).
  4162. */
  4163. /*jshint bitwise:false */ // allow bitwise operations here
  4164. L.PolyUtil = {};
  4165. /*
  4166. * Sutherland-Hodgeman polygon clipping algorithm.
  4167. * Used to avoid rendering parts of a polygon that are not currently visible.
  4168. */
  4169. L.PolyUtil.clipPolygon = function (points, bounds) {
  4170. var clippedPoints,
  4171. edges = [1, 4, 2, 8],
  4172. i, j, k,
  4173. a, b,
  4174. len, edge, p,
  4175. lu = L.LineUtil;
  4176. for (i = 0, len = points.length; i < len; i++) {
  4177. points[i]._code = lu._getBitCode(points[i], bounds);
  4178. }
  4179. // for each edge (left, bottom, right, top)
  4180. for (k = 0; k < 4; k++) {
  4181. edge = edges[k];
  4182. clippedPoints = [];
  4183. for (i = 0, len = points.length, j = len - 1; i < len; j = i++) {
  4184. a = points[i];
  4185. b = points[j];
  4186. // if a is inside the clip window
  4187. if (!(a._code & edge)) {
  4188. // if b is outside the clip window (a->b goes out of screen)
  4189. if (b._code & edge) {
  4190. p = lu._getEdgeIntersection(b, a, edge, bounds);
  4191. p._code = lu._getBitCode(p, bounds);
  4192. clippedPoints.push(p);
  4193. }
  4194. clippedPoints.push(a);
  4195. // else if b is inside the clip window (a->b enters the screen)
  4196. } else if (!(b._code & edge)) {
  4197. p = lu._getEdgeIntersection(b, a, edge, bounds);
  4198. p._code = lu._getBitCode(p, bounds);
  4199. clippedPoints.push(p);
  4200. }
  4201. }
  4202. points = clippedPoints;
  4203. }
  4204. return points;
  4205. };
  4206. /*
  4207. * L.Polygon is used to display polygons on a map.
  4208. */
  4209. L.Polygon = L.Polyline.extend({
  4210. options: {
  4211. fill: true
  4212. },
  4213. initialize: function (latlngs, options) {
  4214. var i, len, hole;
  4215. L.Polyline.prototype.initialize.call(this, latlngs, options);
  4216. if (latlngs && L.Util.isArray(latlngs[0]) && (typeof latlngs[0][0] !== 'number')) {
  4217. this._latlngs = this._convertLatLngs(latlngs[0]);
  4218. this._holes = latlngs.slice(1);
  4219. for (i = 0, len = this._holes.length; i < len; i++) {
  4220. hole = this._holes[i] = this._convertLatLngs(this._holes[i]);
  4221. if (hole[0].equals(hole[hole.length - 1])) {
  4222. hole.pop();
  4223. }
  4224. }
  4225. }
  4226. // filter out last point if its equal to the first one
  4227. latlngs = this._latlngs;
  4228. if (latlngs.length >= 2 && latlngs[0].equals(latlngs[latlngs.length - 1])) {
  4229. latlngs.pop();
  4230. }
  4231. },
  4232. projectLatlngs: function () {
  4233. L.Polyline.prototype.projectLatlngs.call(this);
  4234. // project polygon holes points
  4235. // TODO move this logic to Polyline to get rid of duplication
  4236. this._holePoints = [];
  4237. if (!this._holes) { return; }
  4238. var i, j, len, len2;
  4239. for (i = 0, len = this._holes.length; i < len; i++) {
  4240. this._holePoints[i] = [];
  4241. for (j = 0, len2 = this._holes[i].length; j < len2; j++) {
  4242. this._holePoints[i][j] = this._map.latLngToLayerPoint(this._holes[i][j]);
  4243. }
  4244. }
  4245. },
  4246. _clipPoints: function () {
  4247. var points = this._originalPoints,
  4248. newParts = [];
  4249. this._parts = [points].concat(this._holePoints);
  4250. if (this.options.noClip) { return; }
  4251. for (var i = 0, len = this._parts.length; i < len; i++) {
  4252. var clipped = L.PolyUtil.clipPolygon(this._parts[i], this._map._pathViewport);
  4253. if (clipped.length) {
  4254. newParts.push(clipped);
  4255. }
  4256. }
  4257. this._parts = newParts;
  4258. },
  4259. _getPathPartStr: function (points) {
  4260. var str = L.Polyline.prototype._getPathPartStr.call(this, points);
  4261. return str + (L.Browser.svg ? 'z' : 'x');
  4262. }
  4263. });
  4264. L.polygon = function (latlngs, options) {
  4265. return new L.Polygon(latlngs, options);
  4266. };
  4267. /*
  4268. * Contains L.MultiPolyline and L.MultiPolygon layers.
  4269. */
  4270. (function () {
  4271. function createMulti(Klass) {
  4272. return L.FeatureGroup.extend({
  4273. initialize: function (latlngs, options) {
  4274. this._layers = {};
  4275. this._options = options;
  4276. this.setLatLngs(latlngs);
  4277. },
  4278. setLatLngs: function (latlngs) {
  4279. var i = 0,
  4280. len = latlngs.length;
  4281. this.eachLayer(function (layer) {
  4282. if (i < len) {
  4283. layer.setLatLngs(latlngs[i++]);
  4284. } else {
  4285. this.removeLayer(layer);
  4286. }
  4287. }, this);
  4288. while (i < len) {
  4289. this.addLayer(new Klass(latlngs[i++], this._options));
  4290. }
  4291. return this;
  4292. },
  4293. getLatLngs: function () {
  4294. var latlngs = [];
  4295. this.eachLayer(function (layer) {
  4296. latlngs.push(layer.getLatLngs());
  4297. });
  4298. return latlngs;
  4299. }
  4300. });
  4301. }
  4302. L.MultiPolyline = createMulti(L.Polyline);
  4303. L.MultiPolygon = createMulti(L.Polygon);
  4304. L.multiPolyline = function (latlngs, options) {
  4305. return new L.MultiPolyline(latlngs, options);
  4306. };
  4307. L.multiPolygon = function (latlngs, options) {
  4308. return new L.MultiPolygon(latlngs, options);
  4309. };
  4310. }());
  4311. /*
  4312. * L.Rectangle extends Polygon and creates a rectangle when passed a LatLngBounds object.
  4313. */
  4314. L.Rectangle = L.Polygon.extend({
  4315. initialize: function (latLngBounds, options) {
  4316. L.Polygon.prototype.initialize.call(this, this._boundsToLatLngs(latLngBounds), options);
  4317. },
  4318. setBounds: function (latLngBounds) {
  4319. this.setLatLngs(this._boundsToLatLngs(latLngBounds));
  4320. },
  4321. _boundsToLatLngs: function (latLngBounds) {
  4322. latLngBounds = L.latLngBounds(latLngBounds);
  4323. return [
  4324. latLngBounds.getSouthWest(),
  4325. latLngBounds.getNorthWest(),
  4326. latLngBounds.getNorthEast(),
  4327. latLngBounds.getSouthEast()
  4328. ];
  4329. }
  4330. });
  4331. L.rectangle = function (latLngBounds, options) {
  4332. return new L.Rectangle(latLngBounds, options);
  4333. };
  4334. /*
  4335. * L.Circle is a circle overlay (with a certain radius in meters).
  4336. */
  4337. L.Circle = L.Path.extend({
  4338. initialize: function (latlng, radius, options) {
  4339. L.Path.prototype.initialize.call(this, options);
  4340. this._latlng = L.latLng(latlng);
  4341. this._mRadius = radius;
  4342. },
  4343. options: {
  4344. fill: true
  4345. },
  4346. setLatLng: function (latlng) {
  4347. this._latlng = L.latLng(latlng);
  4348. return this.redraw();
  4349. },
  4350. setRadius: function (radius) {
  4351. this._mRadius = radius;
  4352. return this.redraw();
  4353. },
  4354. projectLatlngs: function () {
  4355. var lngRadius = this._getLngRadius(),
  4356. latlng = this._latlng,
  4357. pointLeft = this._map.latLngToLayerPoint([latlng.lat, latlng.lng - lngRadius]);
  4358. this._point = this._map.latLngToLayerPoint(latlng);
  4359. this._radius = Math.max(this._point.x - pointLeft.x, 1);
  4360. },
  4361. getBounds: function () {
  4362. var lngRadius = this._getLngRadius(),
  4363. latRadius = (this._mRadius / 40075017) * 360,
  4364. latlng = this._latlng;
  4365. return new L.LatLngBounds(
  4366. [latlng.lat - latRadius, latlng.lng - lngRadius],
  4367. [latlng.lat + latRadius, latlng.lng + lngRadius]);
  4368. },
  4369. getLatLng: function () {
  4370. return this._latlng;
  4371. },
  4372. getPathString: function () {
  4373. var p = this._point,
  4374. r = this._radius;
  4375. if (this._checkIfEmpty()) {
  4376. return '';
  4377. }
  4378. if (L.Browser.svg) {
  4379. return 'M' + p.x + ',' + (p.y - r) +
  4380. 'A' + r + ',' + r + ',0,1,1,' +
  4381. (p.x - 0.1) + ',' + (p.y - r) + ' z';
  4382. } else {
  4383. p._round();
  4384. r = Math.round(r);
  4385. return 'AL ' + p.x + ',' + p.y + ' ' + r + ',' + r + ' 0,' + (65535 * 360);
  4386. }
  4387. },
  4388. getRadius: function () {
  4389. return this._mRadius;
  4390. },
  4391. // TODO Earth hardcoded, move into projection code!
  4392. _getLatRadius: function () {
  4393. return (this._mRadius / 40075017) * 360;
  4394. },
  4395. _getLngRadius: function () {
  4396. return this._getLatRadius() / Math.cos(L.LatLng.DEG_TO_RAD * this._latlng.lat);
  4397. },
  4398. _checkIfEmpty: function () {
  4399. if (!this._map) {
  4400. return false;
  4401. }
  4402. var vp = this._map._pathViewport,
  4403. r = this._radius,
  4404. p = this._point;
  4405. return p.x - r > vp.max.x || p.y - r > vp.max.y ||
  4406. p.x + r < vp.min.x || p.y + r < vp.min.y;
  4407. }
  4408. });
  4409. L.circle = function (latlng, radius, options) {
  4410. return new L.Circle(latlng, radius, options);
  4411. };
  4412. /*
  4413. * L.CircleMarker is a circle overlay with a permanent pixel radius.
  4414. */
  4415. L.CircleMarker = L.Circle.extend({
  4416. options: {
  4417. radius: 10,
  4418. weight: 2
  4419. },
  4420. initialize: function (latlng, options) {
  4421. L.Circle.prototype.initialize.call(this, latlng, null, options);
  4422. this._radius = this.options.radius;
  4423. },
  4424. projectLatlngs: function () {
  4425. this._point = this._map.latLngToLayerPoint(this._latlng);
  4426. },
  4427. _updateStyle : function () {
  4428. L.Circle.prototype._updateStyle.call(this);
  4429. this.setRadius(this.options.radius);
  4430. },
  4431. setRadius: function (radius) {
  4432. this.options.radius = this._radius = radius;
  4433. return this.redraw();
  4434. }
  4435. });
  4436. L.circleMarker = function (latlng, options) {
  4437. return new L.CircleMarker(latlng, options);
  4438. };
  4439. /*
  4440. * Extends L.Polyline to be able to manually detect clicks on Canvas-rendered polylines.
  4441. */
  4442. L.Polyline.include(!L.Path.CANVAS ? {} : {
  4443. _containsPoint: function (p, closed) {
  4444. var i, j, k, len, len2, dist, part,
  4445. w = this.options.weight / 2;
  4446. if (L.Browser.touch) {
  4447. w += 10; // polyline click tolerance on touch devices
  4448. }
  4449. for (i = 0, len = this._parts.length; i < len; i++) {
  4450. part = this._parts[i];
  4451. for (j = 0, len2 = part.length, k = len2 - 1; j < len2; k = j++) {
  4452. if (!closed && (j === 0)) {
  4453. continue;
  4454. }
  4455. dist = L.LineUtil.pointToSegmentDistance(p, part[k], part[j]);
  4456. if (dist <= w) {
  4457. return true;
  4458. }
  4459. }
  4460. }
  4461. return false;
  4462. }
  4463. });
  4464. /*
  4465. * Extends L.Polygon to be able to manually detect clicks on Canvas-rendered polygons.
  4466. */
  4467. L.Polygon.include(!L.Path.CANVAS ? {} : {
  4468. _containsPoint: function (p) {
  4469. var inside = false,
  4470. part, p1, p2,
  4471. i, j, k,
  4472. len, len2;
  4473. // TODO optimization: check if within bounds first
  4474. if (L.Polyline.prototype._containsPoint.call(this, p, true)) {
  4475. // click on polygon border
  4476. return true;
  4477. }
  4478. // ray casting algorithm for detecting if point is in polygon
  4479. for (i = 0, len = this._parts.length; i < len; i++) {
  4480. part = this._parts[i];
  4481. for (j = 0, len2 = part.length, k = len2 - 1; j < len2; k = j++) {
  4482. p1 = part[j];
  4483. p2 = part[k];
  4484. if (((p1.y > p.y) !== (p2.y > p.y)) &&
  4485. (p.x < (p2.x - p1.x) * (p.y - p1.y) / (p2.y - p1.y) + p1.x)) {
  4486. inside = !inside;
  4487. }
  4488. }
  4489. }
  4490. return inside;
  4491. }
  4492. });
  4493. /*
  4494. * Extends L.Circle with Canvas-specific code.
  4495. */
  4496. L.Circle.include(!L.Path.CANVAS ? {} : {
  4497. _drawPath: function () {
  4498. var p = this._point;
  4499. this._ctx.beginPath();
  4500. this._ctx.arc(p.x, p.y, this._radius, 0, Math.PI * 2, false);
  4501. },
  4502. _containsPoint: function (p) {
  4503. var center = this._point,
  4504. w2 = this.options.stroke ? this.options.weight / 2 : 0;
  4505. return (p.distanceTo(center) <= this._radius + w2);
  4506. }
  4507. });
  4508. /*
  4509. * CircleMarker canvas specific drawing parts.
  4510. */
  4511. L.CircleMarker.include(!L.Path.CANVAS ? {} : {
  4512. _updateStyle: function () {
  4513. L.Path.prototype._updateStyle.call(this);
  4514. }
  4515. });
  4516. /*
  4517. * L.GeoJSON turns any GeoJSON data into a Leaflet layer.
  4518. */
  4519. L.GeoJSON = L.FeatureGroup.extend({
  4520. initialize: function (geojson, options) {
  4521. L.setOptions(this, options);
  4522. this._layers = {};
  4523. if (geojson) {
  4524. this.addData(geojson);
  4525. }
  4526. },
  4527. addData: function (geojson) {
  4528. var features = L.Util.isArray(geojson) ? geojson : geojson.features,
  4529. i, len, feature;
  4530. if (features) {
  4531. for (i = 0, len = features.length; i < len; i++) {
  4532. // Only add this if geometry or geometries are set and not null
  4533. feature = features[i];
  4534. if (feature.geometries || feature.geometry || feature.features || feature.coordinates) {
  4535. this.addData(features[i]);
  4536. }
  4537. }
  4538. return this;
  4539. }
  4540. var options = this.options;
  4541. if (options.filter && !options.filter(geojson)) { return; }
  4542. var layer = L.GeoJSON.geometryToLayer(geojson, options.pointToLayer, options.coordsToLatLng);
  4543. layer.feature = L.GeoJSON.asFeature(geojson);
  4544. layer.defaultOptions = layer.options;
  4545. this.resetStyle(layer);
  4546. if (options.onEachFeature) {
  4547. options.onEachFeature(geojson, layer);
  4548. }
  4549. return this.addLayer(layer);
  4550. },
  4551. resetStyle: function (layer) {
  4552. var style = this.options.style;
  4553. if (style) {
  4554. // reset any custom styles
  4555. L.Util.extend(layer.options, layer.defaultOptions);
  4556. this._setLayerStyle(layer, style);
  4557. }
  4558. },
  4559. setStyle: function (style) {
  4560. this.eachLayer(function (layer) {
  4561. this._setLayerStyle(layer, style);
  4562. }, this);
  4563. },
  4564. _setLayerStyle: function (layer, style) {
  4565. if (typeof style === 'function') {
  4566. style = style(layer.feature);
  4567. }
  4568. if (layer.setStyle) {
  4569. layer.setStyle(style);
  4570. }
  4571. }
  4572. });
  4573. L.extend(L.GeoJSON, {
  4574. geometryToLayer: function (geojson, pointToLayer, coordsToLatLng) {
  4575. var geometry = geojson.type === 'Feature' ? geojson.geometry : geojson,
  4576. coords = geometry.coordinates,
  4577. layers = [],
  4578. latlng, latlngs, i, len, layer;
  4579. coordsToLatLng = coordsToLatLng || this.coordsToLatLng;
  4580. switch (geometry.type) {
  4581. case 'Point':
  4582. latlng = coordsToLatLng(coords);
  4583. return pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng);
  4584. case 'MultiPoint':
  4585. for (i = 0, len = coords.length; i < len; i++) {
  4586. latlng = coordsToLatLng(coords[i]);
  4587. layer = pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng);
  4588. layers.push(layer);
  4589. }
  4590. return new L.FeatureGroup(layers);
  4591. case 'LineString':
  4592. latlngs = this.coordsToLatLngs(coords, 0, coordsToLatLng);
  4593. return new L.Polyline(latlngs);
  4594. case 'Polygon':
  4595. latlngs = this.coordsToLatLngs(coords, 1, coordsToLatLng);
  4596. return new L.Polygon(latlngs);
  4597. case 'MultiLineString':
  4598. latlngs = this.coordsToLatLngs(coords, 1, coordsToLatLng);
  4599. return new L.MultiPolyline(latlngs);
  4600. case 'MultiPolygon':
  4601. latlngs = this.coordsToLatLngs(coords, 2, coordsToLatLng);
  4602. return new L.MultiPolygon(latlngs);
  4603. case 'GeometryCollection':
  4604. for (i = 0, len = geometry.geometries.length; i < len; i++) {
  4605. layer = this.geometryToLayer({
  4606. geometry: geometry.geometries[i],
  4607. type: 'Feature',
  4608. properties: geojson.properties
  4609. }, pointToLayer, coordsToLatLng);
  4610. layers.push(layer);
  4611. }
  4612. return new L.FeatureGroup(layers);
  4613. default:
  4614. throw new Error('Invalid GeoJSON object.');
  4615. }
  4616. },
  4617. coordsToLatLng: function (coords) { // (Array[, Boolean]) -> LatLng
  4618. return new L.LatLng(coords[1], coords[0]);
  4619. },
  4620. coordsToLatLngs: function (coords, levelsDeep, coordsToLatLng) { // (Array[, Number, Function]) -> Array
  4621. var latlng, i, len,
  4622. latlngs = [];
  4623. for (i = 0, len = coords.length; i < len; i++) {
  4624. latlng = levelsDeep ?
  4625. this.coordsToLatLngs(coords[i], levelsDeep - 1, coordsToLatLng) :
  4626. (coordsToLatLng || this.coordsToLatLng)(coords[i]);
  4627. latlngs.push(latlng);
  4628. }
  4629. return latlngs;
  4630. },
  4631. latLngToCoords: function (latLng) {
  4632. return [latLng.lng, latLng.lat];
  4633. },
  4634. latLngsToCoords: function (latLngs) {
  4635. var coords = [];
  4636. for (var i = 0, len = latLngs.length; i < len; i++) {
  4637. coords.push(L.GeoJSON.latLngToCoords(latLngs[i]));
  4638. }
  4639. return coords;
  4640. },
  4641. getFeature: function (layer, newGeometry) {
  4642. return layer.feature ? L.extend({}, layer.feature, {geometry: newGeometry}) : L.GeoJSON.asFeature(newGeometry);
  4643. },
  4644. asFeature: function (geoJSON) {
  4645. if (geoJSON.type === 'Feature') {
  4646. return geoJSON;
  4647. }
  4648. return {
  4649. type: 'Feature',
  4650. properties: {},
  4651. geometry: geoJSON
  4652. };
  4653. }
  4654. });
  4655. var PointToGeoJSON = {
  4656. toGeoJSON: function () {
  4657. return L.GeoJSON.getFeature(this, {
  4658. type: 'Point',
  4659. coordinates: L.GeoJSON.latLngToCoords(this.getLatLng())
  4660. });
  4661. }
  4662. };
  4663. L.Marker.include(PointToGeoJSON);
  4664. L.Circle.include(PointToGeoJSON);
  4665. L.CircleMarker.include(PointToGeoJSON);
  4666. L.Polyline.include({
  4667. toGeoJSON: function () {
  4668. return L.GeoJSON.getFeature(this, {
  4669. type: 'LineString',
  4670. coordinates: L.GeoJSON.latLngsToCoords(this.getLatLngs())
  4671. });
  4672. }
  4673. });
  4674. L.Polygon.include({
  4675. toGeoJSON: function () {
  4676. var coords = [L.GeoJSON.latLngsToCoords(this.getLatLngs())],
  4677. i, len, hole;
  4678. coords[0].push(coords[0][0]);
  4679. if (this._holes) {
  4680. for (i = 0, len = this._holes.length; i < len; i++) {
  4681. hole = L.GeoJSON.latLngsToCoords(this._holes[i]);
  4682. hole.push(hole[0]);
  4683. coords.push(hole);
  4684. }
  4685. }
  4686. return L.GeoJSON.getFeature(this, {
  4687. type: 'Polygon',
  4688. coordinates: coords
  4689. });
  4690. }
  4691. });
  4692. (function () {
  4693. function includeMulti(Klass, type) {
  4694. Klass.include({
  4695. toGeoJSON: function () {
  4696. var coords = [];
  4697. this.eachLayer(function (layer) {
  4698. coords.push(layer.toGeoJSON().geometry.coordinates);
  4699. });
  4700. return L.GeoJSON.getFeature(this, {
  4701. type: type,
  4702. coordinates: coords
  4703. });
  4704. }
  4705. });
  4706. }
  4707. includeMulti(L.MultiPolyline, 'MultiLineString');
  4708. includeMulti(L.MultiPolygon, 'MultiPolygon');
  4709. }());
  4710. L.LayerGroup.include({
  4711. toGeoJSON: function () {
  4712. var features = [];
  4713. this.eachLayer(function (layer) {
  4714. if (layer.toGeoJSON) {
  4715. features.push(L.GeoJSON.asFeature(layer.toGeoJSON()));
  4716. }
  4717. });
  4718. return {
  4719. type: 'FeatureCollection',
  4720. features: features
  4721. };
  4722. }
  4723. });
  4724. L.geoJson = function (geojson, options) {
  4725. return new L.GeoJSON(geojson, options);
  4726. };
  4727. /*
  4728. * L.DomEvent contains functions for working with DOM events.
  4729. */
  4730. L.DomEvent = {
  4731. /* inspired by John Resig, Dean Edwards and YUI addEvent implementations */
  4732. addListener: function (obj, type, fn, context) { // (HTMLElement, String, Function[, Object])
  4733. var id = L.stamp(fn),
  4734. key = '_leaflet_' + type + id,
  4735. handler, originalHandler, newType;
  4736. if (obj[key]) { return this; }
  4737. handler = function (e) {
  4738. return fn.call(context || obj, e || L.DomEvent._getEvent());
  4739. };
  4740. if (L.Browser.msTouch && type.indexOf('touch') === 0) {
  4741. return this.addMsTouchListener(obj, type, handler, id);
  4742. }
  4743. if (L.Browser.touch && (type === 'dblclick') && this.addDoubleTapListener) {
  4744. this.addDoubleTapListener(obj, handler, id);
  4745. }
  4746. if ('addEventListener' in obj) {
  4747. if (type === 'mousewheel') {
  4748. obj.addEventListener('DOMMouseScroll', handler, false);
  4749. obj.addEventListener(type, handler, false);
  4750. } else if ((type === 'mouseenter') || (type === 'mouseleave')) {
  4751. originalHandler = handler;
  4752. newType = (type === 'mouseenter' ? 'mouseover' : 'mouseout');
  4753. handler = function (e) {
  4754. if (!L.DomEvent._checkMouse(obj, e)) { return; }
  4755. return originalHandler(e);
  4756. };
  4757. obj.addEventListener(newType, handler, false);
  4758. } else if (type === 'click' && L.Browser.android) {
  4759. originalHandler = handler;
  4760. handler = function (e) {
  4761. return L.DomEvent._filterClick(e, originalHandler);
  4762. };
  4763. obj.addEventListener(type, handler, false);
  4764. } else {
  4765. obj.addEventListener(type, handler, false);
  4766. }
  4767. } else if ('attachEvent' in obj) {
  4768. obj.attachEvent('on' + type, handler);
  4769. }
  4770. obj[key] = handler;
  4771. return this;
  4772. },
  4773. removeListener: function (obj, type, fn) { // (HTMLElement, String, Function)
  4774. var id = L.stamp(fn),
  4775. key = '_leaflet_' + type + id,
  4776. handler = obj[key];
  4777. if (!handler) { return this; }
  4778. if (L.Browser.msTouch && type.indexOf('touch') === 0) {
  4779. this.removeMsTouchListener(obj, type, id);
  4780. } else if (L.Browser.touch && (type === 'dblclick') && this.removeDoubleTapListener) {
  4781. this.removeDoubleTapListener(obj, id);
  4782. } else if ('removeEventListener' in obj) {
  4783. if (type === 'mousewheel') {
  4784. obj.removeEventListener('DOMMouseScroll', handler, false);
  4785. obj.removeEventListener(type, handler, false);
  4786. } else if ((type === 'mouseenter') || (type === 'mouseleave')) {
  4787. obj.removeEventListener((type === 'mouseenter' ? 'mouseover' : 'mouseout'), handler, false);
  4788. } else {
  4789. obj.removeEventListener(type, handler, false);
  4790. }
  4791. } else if ('detachEvent' in obj) {
  4792. obj.detachEvent('on' + type, handler);
  4793. }
  4794. obj[key] = null;
  4795. return this;
  4796. },
  4797. stopPropagation: function (e) {
  4798. if (e.stopPropagation) {
  4799. e.stopPropagation();
  4800. } else {
  4801. e.cancelBubble = true;
  4802. }
  4803. return this;
  4804. },
  4805. disableClickPropagation: function (el) {
  4806. var stop = L.DomEvent.stopPropagation;
  4807. for (var i = L.Draggable.START.length - 1; i >= 0; i--) {
  4808. L.DomEvent.addListener(el, L.Draggable.START[i], stop);
  4809. }
  4810. return L.DomEvent
  4811. .addListener(el, 'click', L.DomEvent._fakeStop)
  4812. .addListener(el, 'dblclick', stop);
  4813. },
  4814. preventDefault: function (e) {
  4815. if (e.preventDefault) {
  4816. e.preventDefault();
  4817. } else {
  4818. e.returnValue = false;
  4819. }
  4820. return this;
  4821. },
  4822. stop: function (e) {
  4823. return L.DomEvent.preventDefault(e).stopPropagation(e);
  4824. },
  4825. getMousePosition: function (e, container) {
  4826. var ie7 = L.Browser.ie7,
  4827. body = document.body,
  4828. docEl = document.documentElement,
  4829. x = e.pageX ? e.pageX - body.scrollLeft - docEl.scrollLeft: e.clientX,
  4830. y = e.pageY ? e.pageY - body.scrollTop - docEl.scrollTop: e.clientY,
  4831. pos = new L.Point(x, y),
  4832. rect = container.getBoundingClientRect(),
  4833. left = rect.left - container.clientLeft,
  4834. top = rect.top - container.clientTop;
  4835. // webkit (and ie <= 7) handles RTL scrollLeft different to everyone else
  4836. // https://code.google.com/p/closure-library/source/browse/trunk/closure/goog/style/bidi.js
  4837. if (!L.DomUtil.documentIsLtr() && (L.Browser.webkit || ie7)) {
  4838. left += container.scrollWidth - container.clientWidth;
  4839. // ie7 shows the scrollbar by default and provides clientWidth counting it, so we
  4840. // need to add it back in if it is visible; scrollbar is on the left as we are RTL
  4841. if (ie7 && L.DomUtil.getStyle(container, 'overflow-y') !== 'hidden' &&
  4842. L.DomUtil.getStyle(container, 'overflow') !== 'hidden') {
  4843. left += 17;
  4844. }
  4845. }
  4846. return pos._subtract(new L.Point(left, top));
  4847. },
  4848. getWheelDelta: function (e) {
  4849. var delta = 0;
  4850. if (e.wheelDelta) {
  4851. delta = e.wheelDelta / 120;
  4852. }
  4853. if (e.detail) {
  4854. delta = -e.detail / 3;
  4855. }
  4856. return delta;
  4857. },
  4858. _skipEvents: {},
  4859. _fakeStop: function (e) {
  4860. // fakes stopPropagation by setting a special event flag, checked/reset with L.DomEvent._skipped(e)
  4861. L.DomEvent._skipEvents[e.type] = true;
  4862. },
  4863. _skipped: function (e) {
  4864. var skipped = this._skipEvents[e.type];
  4865. // reset when checking, as it's only used in map container and propagates outside of the map
  4866. this._skipEvents[e.type] = false;
  4867. return skipped;
  4868. },
  4869. // check if element really left/entered the event target (for mouseenter/mouseleave)
  4870. _checkMouse: function (el, e) {
  4871. var related = e.relatedTarget;
  4872. if (!related) { return true; }
  4873. try {
  4874. while (related && (related !== el)) {
  4875. related = related.parentNode;
  4876. }
  4877. } catch (err) {
  4878. return false;
  4879. }
  4880. return (related !== el);
  4881. },
  4882. _getEvent: function () { // evil magic for IE
  4883. /*jshint noarg:false */
  4884. var e = window.event;
  4885. if (!e) {
  4886. var caller = arguments.callee.caller;
  4887. while (caller) {
  4888. e = caller['arguments'][0];
  4889. if (e && window.Event === e.constructor) {
  4890. break;
  4891. }
  4892. caller = caller.caller;
  4893. }
  4894. }
  4895. return e;
  4896. },
  4897. // this is a horrible workaround for a bug in Android where a single touch triggers two click events
  4898. _filterClick: function (e, handler) {
  4899. var timeStamp = (e.timeStamp || e.originalEvent.timeStamp),
  4900. elapsed = L.DomEvent._lastClick && (timeStamp - L.DomEvent._lastClick);
  4901. // are they closer together than 1000ms yet more than 100ms?
  4902. // Android typically triggers them ~300ms apart while multiple listeners
  4903. // on the same event should be triggered far faster;
  4904. // or check if click is simulated on the element, and if it is, reject any non-simulated events
  4905. if ((elapsed && elapsed > 100 && elapsed < 1000) || (e.target._simulatedClick && !e._simulated)) {
  4906. L.DomEvent.stop(e);
  4907. return;
  4908. }
  4909. L.DomEvent._lastClick = timeStamp;
  4910. return handler(e);
  4911. }
  4912. };
  4913. L.DomEvent.on = L.DomEvent.addListener;
  4914. L.DomEvent.off = L.DomEvent.removeListener;
  4915. /*
  4916. * L.Draggable allows you to add dragging capabilities to any element. Supports mobile devices too.
  4917. */
  4918. L.Draggable = L.Class.extend({
  4919. includes: L.Mixin.Events,
  4920. statics: {
  4921. START: L.Browser.touch ? ['touchstart', 'mousedown'] : ['mousedown'],
  4922. END: {
  4923. mousedown: 'mouseup',
  4924. touchstart: 'touchend',
  4925. MSPointerDown: 'touchend'
  4926. },
  4927. MOVE: {
  4928. mousedown: 'mousemove',
  4929. touchstart: 'touchmove',
  4930. MSPointerDown: 'touchmove'
  4931. }
  4932. },
  4933. initialize: function (element, dragStartTarget) {
  4934. this._element = element;
  4935. this._dragStartTarget = dragStartTarget || element;
  4936. },
  4937. enable: function () {
  4938. if (this._enabled) { return; }
  4939. for (var i = L.Draggable.START.length - 1; i >= 0; i--) {
  4940. L.DomEvent.on(this._dragStartTarget, L.Draggable.START[i], this._onDown, this);
  4941. }
  4942. this._enabled = true;
  4943. },
  4944. disable: function () {
  4945. if (!this._enabled) { return; }
  4946. for (var i = L.Draggable.START.length - 1; i >= 0; i--) {
  4947. L.DomEvent.off(this._dragStartTarget, L.Draggable.START[i], this._onDown, this);
  4948. }
  4949. this._enabled = false;
  4950. this._moved = false;
  4951. },
  4952. _onDown: function (e) {
  4953. if (e.shiftKey || ((e.which !== 1) && (e.button !== 1) && !e.touches)) { return; }
  4954. L.DomEvent
  4955. .stopPropagation(e);
  4956. if (L.Draggable._disabled) { return; }
  4957. L.DomUtil.disableImageDrag();
  4958. L.DomUtil.disableTextSelection();
  4959. var first = e.touches ? e.touches[0] : e,
  4960. el = first.target;
  4961. // if touching a link, highlight it
  4962. if (L.Browser.touch && el.tagName.toLowerCase() === 'a') {
  4963. L.DomUtil.addClass(el, 'leaflet-active');
  4964. }
  4965. this._moved = false;
  4966. if (this._moving) { return; }
  4967. this._startPoint = new L.Point(first.clientX, first.clientY);
  4968. this._startPos = this._newPos = L.DomUtil.getPosition(this._element);
  4969. L.DomEvent
  4970. .on(document, L.Draggable.MOVE[e.type], this._onMove, this)
  4971. .on(document, L.Draggable.END[e.type], this._onUp, this);
  4972. },
  4973. _onMove: function (e) {
  4974. if (e.touches && e.touches.length > 1) { return; }
  4975. var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e),
  4976. newPoint = new L.Point(first.clientX, first.clientY),
  4977. offset = newPoint.subtract(this._startPoint);
  4978. if (!offset.x && !offset.y) { return; }
  4979. L.DomEvent.preventDefault(e);
  4980. if (!this._moved) {
  4981. this.fire('dragstart');
  4982. this._moved = true;
  4983. this._startPos = L.DomUtil.getPosition(this._element).subtract(offset);
  4984. if (!L.Browser.touch) {
  4985. L.DomUtil.addClass(document.body, 'leaflet-dragging');
  4986. }
  4987. }
  4988. this._newPos = this._startPos.add(offset);
  4989. this._moving = true;
  4990. L.Util.cancelAnimFrame(this._animRequest);
  4991. this._animRequest = L.Util.requestAnimFrame(this._updatePosition, this, true, this._dragStartTarget);
  4992. },
  4993. _updatePosition: function () {
  4994. this.fire('predrag');
  4995. L.DomUtil.setPosition(this._element, this._newPos);
  4996. this.fire('drag');
  4997. },
  4998. _onUp: function () {
  4999. if (!L.Browser.touch) {
  5000. L.DomUtil.removeClass(document.body, 'leaflet-dragging');
  5001. }
  5002. for (var i in L.Draggable.MOVE) {
  5003. L.DomEvent
  5004. .off(document, L.Draggable.MOVE[i], this._onMove)
  5005. .off(document, L.Draggable.END[i], this._onUp);
  5006. }
  5007. L.DomUtil.enableImageDrag();
  5008. L.DomUtil.enableTextSelection();
  5009. if (this._moved) {
  5010. // ensure drag is not fired after dragend
  5011. L.Util.cancelAnimFrame(this._animRequest);
  5012. this.fire('dragend');
  5013. }
  5014. this._moving = false;
  5015. }
  5016. });
  5017. /*
  5018. L.Handler is a base class for handler classes that are used internally to inject
  5019. interaction features like dragging to classes like Map and Marker.
  5020. */
  5021. L.Handler = L.Class.extend({
  5022. initialize: function (map) {
  5023. this._map = map;
  5024. },
  5025. enable: function () {
  5026. if (this._enabled) { return; }
  5027. this._enabled = true;
  5028. this.addHooks();
  5029. },
  5030. disable: function () {
  5031. if (!this._enabled) { return; }
  5032. this._enabled = false;
  5033. this.removeHooks();
  5034. },
  5035. enabled: function () {
  5036. return !!this._enabled;
  5037. }
  5038. });
  5039. /*
  5040. * L.Handler.MapDrag is used to make the map draggable (with panning inertia), enabled by default.
  5041. */
  5042. L.Map.mergeOptions({
  5043. dragging: true,
  5044. inertia: !L.Browser.android23,
  5045. inertiaDeceleration: 3400, // px/s^2
  5046. inertiaMaxSpeed: Infinity, // px/s
  5047. inertiaThreshold: L.Browser.touch ? 32 : 18, // ms
  5048. easeLinearity: 0.25,
  5049. // TODO refactor, move to CRS
  5050. worldCopyJump: false
  5051. });
  5052. L.Map.Drag = L.Handler.extend({
  5053. addHooks: function () {
  5054. if (!this._draggable) {
  5055. var map = this._map;
  5056. this._draggable = new L.Draggable(map._mapPane, map._container);
  5057. this._draggable.on({
  5058. 'dragstart': this._onDragStart,
  5059. 'drag': this._onDrag,
  5060. 'dragend': this._onDragEnd
  5061. }, this);
  5062. if (map.options.worldCopyJump) {
  5063. this._draggable.on('predrag', this._onPreDrag, this);
  5064. map.on('viewreset', this._onViewReset, this);
  5065. this._onViewReset();
  5066. }
  5067. }
  5068. this._draggable.enable();
  5069. },
  5070. removeHooks: function () {
  5071. this._draggable.disable();
  5072. },
  5073. moved: function () {
  5074. return this._draggable && this._draggable._moved;
  5075. },
  5076. _onDragStart: function () {
  5077. var map = this._map;
  5078. if (map._panAnim) {
  5079. map._panAnim.stop();
  5080. }
  5081. map
  5082. .fire('movestart')
  5083. .fire('dragstart');
  5084. if (map.options.inertia) {
  5085. this._positions = [];
  5086. this._times = [];
  5087. }
  5088. },
  5089. _onDrag: function () {
  5090. if (this._map.options.inertia) {
  5091. var time = this._lastTime = +new Date(),
  5092. pos = this._lastPos = this._draggable._newPos;
  5093. this._positions.push(pos);
  5094. this._times.push(time);
  5095. if (time - this._times[0] > 200) {
  5096. this._positions.shift();
  5097. this._times.shift();
  5098. }
  5099. }
  5100. this._map
  5101. .fire('move')
  5102. .fire('drag');
  5103. },
  5104. _onViewReset: function () {
  5105. // TODO fix hardcoded Earth values
  5106. var pxCenter = this._map.getSize()._divideBy(2),
  5107. pxWorldCenter = this._map.latLngToLayerPoint([0, 0]);
  5108. this._initialWorldOffset = pxWorldCenter.subtract(pxCenter).x;
  5109. this._worldWidth = this._map.project([0, 180]).x;
  5110. },
  5111. _onPreDrag: function () {
  5112. // TODO refactor to be able to adjust map pane position after zoom
  5113. var worldWidth = this._worldWidth,
  5114. halfWidth = Math.round(worldWidth / 2),
  5115. dx = this._initialWorldOffset,
  5116. x = this._draggable._newPos.x,
  5117. newX1 = (x - halfWidth + dx) % worldWidth + halfWidth - dx,
  5118. newX2 = (x + halfWidth + dx) % worldWidth - halfWidth - dx,
  5119. newX = Math.abs(newX1 + dx) < Math.abs(newX2 + dx) ? newX1 : newX2;
  5120. this._draggable._newPos.x = newX;
  5121. },
  5122. _onDragEnd: function () {
  5123. var map = this._map,
  5124. options = map.options,
  5125. delay = +new Date() - this._lastTime,
  5126. noInertia = !options.inertia || delay > options.inertiaThreshold || !this._positions[0];
  5127. map.fire('dragend');
  5128. if (noInertia) {
  5129. map.fire('moveend');
  5130. } else {
  5131. var direction = this._lastPos.subtract(this._positions[0]),
  5132. duration = (this._lastTime + delay - this._times[0]) / 1000,
  5133. ease = options.easeLinearity,
  5134. speedVector = direction.multiplyBy(ease / duration),
  5135. speed = speedVector.distanceTo([0, 0]),
  5136. limitedSpeed = Math.min(options.inertiaMaxSpeed, speed),
  5137. limitedSpeedVector = speedVector.multiplyBy(limitedSpeed / speed),
  5138. decelerationDuration = limitedSpeed / (options.inertiaDeceleration * ease),
  5139. offset = limitedSpeedVector.multiplyBy(-decelerationDuration / 2).round();
  5140. if (!offset.x || !offset.y) {
  5141. map.fire('moveend');
  5142. } else {
  5143. L.Util.requestAnimFrame(function () {
  5144. map.panBy(offset, {
  5145. duration: decelerationDuration,
  5146. easeLinearity: ease,
  5147. noMoveStart: true
  5148. });
  5149. });
  5150. }
  5151. }
  5152. }
  5153. });
  5154. L.Map.addInitHook('addHandler', 'dragging', L.Map.Drag);
  5155. /*
  5156. * L.Handler.DoubleClickZoom is used to handle double-click zoom on the map, enabled by default.
  5157. */
  5158. L.Map.mergeOptions({
  5159. doubleClickZoom: true
  5160. });
  5161. L.Map.DoubleClickZoom = L.Handler.extend({
  5162. addHooks: function () {
  5163. this._map.on('dblclick', this._onDoubleClick);
  5164. },
  5165. removeHooks: function () {
  5166. this._map.off('dblclick', this._onDoubleClick);
  5167. },
  5168. _onDoubleClick: function (e) {
  5169. this.setZoomAround(e.containerPoint, this._zoom + 1);
  5170. }
  5171. });
  5172. L.Map.addInitHook('addHandler', 'doubleClickZoom', L.Map.DoubleClickZoom);
  5173. /*
  5174. * L.Handler.ScrollWheelZoom is used by L.Map to enable mouse scroll wheel zoom on the map.
  5175. */
  5176. L.Map.mergeOptions({
  5177. scrollWheelZoom: true
  5178. });
  5179. L.Map.ScrollWheelZoom = L.Handler.extend({
  5180. addHooks: function () {
  5181. L.DomEvent.on(this._map._container, 'mousewheel', this._onWheelScroll, this);
  5182. L.DomEvent.on(this._map._container, 'MozMousePixelScroll', L.DomEvent.preventDefault);
  5183. this._delta = 0;
  5184. },
  5185. removeHooks: function () {
  5186. L.DomEvent.off(this._map._container, 'mousewheel', this._onWheelScroll);
  5187. L.DomEvent.off(this._map._container, 'MozMousePixelScroll', L.DomEvent.preventDefault);
  5188. },
  5189. _onWheelScroll: function (e) {
  5190. var delta = L.DomEvent.getWheelDelta(e);
  5191. this._delta += delta;
  5192. this._lastMousePos = this._map.mouseEventToContainerPoint(e);
  5193. if (!this._startTime) {
  5194. this._startTime = +new Date();
  5195. }
  5196. var left = Math.max(40 - (+new Date() - this._startTime), 0);
  5197. clearTimeout(this._timer);
  5198. this._timer = setTimeout(L.bind(this._performZoom, this), left);
  5199. L.DomEvent.preventDefault(e);
  5200. L.DomEvent.stopPropagation(e);
  5201. },
  5202. _performZoom: function () {
  5203. var map = this._map,
  5204. delta = this._delta,
  5205. zoom = map.getZoom();
  5206. delta = delta > 0 ? Math.ceil(delta) : Math.floor(delta);
  5207. delta = Math.max(Math.min(delta, 4), -4);
  5208. delta = map._limitZoom(zoom + delta) - zoom;
  5209. this._delta = 0;
  5210. this._startTime = null;
  5211. if (!delta) { return; }
  5212. map.setZoomAround(this._lastMousePos, zoom + delta);
  5213. }
  5214. });
  5215. L.Map.addInitHook('addHandler', 'scrollWheelZoom', L.Map.ScrollWheelZoom);
  5216. /*
  5217. * Extends the event handling code with double tap support for mobile browsers.
  5218. */
  5219. L.extend(L.DomEvent, {
  5220. _touchstart: L.Browser.msTouch ? 'MSPointerDown' : 'touchstart',
  5221. _touchend: L.Browser.msTouch ? 'MSPointerUp' : 'touchend',
  5222. // inspired by Zepto touch code by Thomas Fuchs
  5223. addDoubleTapListener: function (obj, handler, id) {
  5224. var last,
  5225. doubleTap = false,
  5226. delay = 250,
  5227. touch,
  5228. pre = '_leaflet_',
  5229. touchstart = this._touchstart,
  5230. touchend = this._touchend,
  5231. trackedTouches = [];
  5232. function onTouchStart(e) {
  5233. var count;
  5234. if (L.Browser.msTouch) {
  5235. trackedTouches.push(e.pointerId);
  5236. count = trackedTouches.length;
  5237. } else {
  5238. count = e.touches.length;
  5239. }
  5240. if (count > 1) {
  5241. return;
  5242. }
  5243. var now = Date.now(),
  5244. delta = now - (last || now);
  5245. touch = e.touches ? e.touches[0] : e;
  5246. doubleTap = (delta > 0 && delta <= delay);
  5247. last = now;
  5248. }
  5249. function onTouchEnd(e) {
  5250. if (L.Browser.msTouch) {
  5251. var idx = trackedTouches.indexOf(e.pointerId);
  5252. if (idx === -1) {
  5253. return;
  5254. }
  5255. trackedTouches.splice(idx, 1);
  5256. }
  5257. if (doubleTap) {
  5258. if (L.Browser.msTouch) {
  5259. // work around .type being readonly with MSPointer* events
  5260. var newTouch = { },
  5261. prop;
  5262. // jshint forin:false
  5263. for (var i in touch) {
  5264. prop = touch[i];
  5265. if (typeof prop === 'function') {
  5266. newTouch[i] = prop.bind(touch);
  5267. } else {
  5268. newTouch[i] = prop;
  5269. }
  5270. }
  5271. touch = newTouch;
  5272. }
  5273. touch.type = 'dblclick';
  5274. handler(touch);
  5275. last = null;
  5276. }
  5277. }
  5278. obj[pre + touchstart + id] = onTouchStart;
  5279. obj[pre + touchend + id] = onTouchEnd;
  5280. // on msTouch we need to listen on the document, otherwise a drag starting on the map and moving off screen
  5281. // will not come through to us, so we will lose track of how many touches are ongoing
  5282. var endElement = L.Browser.msTouch ? document.documentElement : obj;
  5283. obj.addEventListener(touchstart, onTouchStart, false);
  5284. endElement.addEventListener(touchend, onTouchEnd, false);
  5285. if (L.Browser.msTouch) {
  5286. endElement.addEventListener('MSPointerCancel', onTouchEnd, false);
  5287. }
  5288. return this;
  5289. },
  5290. removeDoubleTapListener: function (obj, id) {
  5291. var pre = '_leaflet_';
  5292. obj.removeEventListener(this._touchstart, obj[pre + this._touchstart + id], false);
  5293. (L.Browser.msTouch ? document.documentElement : obj).removeEventListener(
  5294. this._touchend, obj[pre + this._touchend + id], false);
  5295. if (L.Browser.msTouch) {
  5296. document.documentElement.removeEventListener('MSPointerCancel', obj[pre + this._touchend + id], false);
  5297. }
  5298. return this;
  5299. }
  5300. });
  5301. /*
  5302. * Extends L.DomEvent to provide touch support for Internet Explorer and Windows-based devices.
  5303. */
  5304. L.extend(L.DomEvent, {
  5305. _msTouches: [],
  5306. _msDocumentListener: false,
  5307. // Provides a touch events wrapper for msPointer events.
  5308. // Based on changes by veproza https://github.com/CloudMade/Leaflet/pull/1019
  5309. addMsTouchListener: function (obj, type, handler, id) {
  5310. switch (type) {
  5311. case 'touchstart':
  5312. return this.addMsTouchListenerStart(obj, type, handler, id);
  5313. case 'touchend':
  5314. return this.addMsTouchListenerEnd(obj, type, handler, id);
  5315. case 'touchmove':
  5316. return this.addMsTouchListenerMove(obj, type, handler, id);
  5317. default:
  5318. throw 'Unknown touch event type';
  5319. }
  5320. },
  5321. addMsTouchListenerStart: function (obj, type, handler, id) {
  5322. var pre = '_leaflet_',
  5323. touches = this._msTouches;
  5324. var cb = function (e) {
  5325. var alreadyInArray = false;
  5326. for (var i = 0; i < touches.length; i++) {
  5327. if (touches[i].pointerId === e.pointerId) {
  5328. alreadyInArray = true;
  5329. break;
  5330. }
  5331. }
  5332. if (!alreadyInArray) {
  5333. touches.push(e);
  5334. }
  5335. e.touches = touches.slice();
  5336. e.changedTouches = [e];
  5337. handler(e);
  5338. };
  5339. obj[pre + 'touchstart' + id] = cb;
  5340. obj.addEventListener('MSPointerDown', cb, false);
  5341. // need to also listen for end events to keep the _msTouches list accurate
  5342. // this needs to be on the body and never go away
  5343. if (!this._msDocumentListener) {
  5344. var internalCb = function (e) {
  5345. for (var i = 0; i < touches.length; i++) {
  5346. if (touches[i].pointerId === e.pointerId) {
  5347. touches.splice(i, 1);
  5348. break;
  5349. }
  5350. }
  5351. };
  5352. //We listen on the documentElement as any drags that end by moving the touch off the screen get fired there
  5353. document.documentElement.addEventListener('MSPointerUp', internalCb, false);
  5354. document.documentElement.addEventListener('MSPointerCancel', internalCb, false);
  5355. this._msDocumentListener = true;
  5356. }
  5357. return this;
  5358. },
  5359. addMsTouchListenerMove: function (obj, type, handler, id) {
  5360. var pre = '_leaflet_',
  5361. touches = this._msTouches;
  5362. function cb(e) {
  5363. // don't fire touch moves when mouse isn't down
  5364. if (e.pointerType === e.MSPOINTER_TYPE_MOUSE && e.buttons === 0) { return; }
  5365. for (var i = 0; i < touches.length; i++) {
  5366. if (touches[i].pointerId === e.pointerId) {
  5367. touches[i] = e;
  5368. break;
  5369. }
  5370. }
  5371. e.touches = touches.slice();
  5372. e.changedTouches = [e];
  5373. handler(e);
  5374. }
  5375. obj[pre + 'touchmove' + id] = cb;
  5376. obj.addEventListener('MSPointerMove', cb, false);
  5377. return this;
  5378. },
  5379. addMsTouchListenerEnd: function (obj, type, handler, id) {
  5380. var pre = '_leaflet_',
  5381. touches = this._msTouches;
  5382. var cb = function (e) {
  5383. for (var i = 0; i < touches.length; i++) {
  5384. if (touches[i].pointerId === e.pointerId) {
  5385. touches.splice(i, 1);
  5386. break;
  5387. }
  5388. }
  5389. e.touches = touches.slice();
  5390. e.changedTouches = [e];
  5391. handler(e);
  5392. };
  5393. obj[pre + 'touchend' + id] = cb;
  5394. obj.addEventListener('MSPointerUp', cb, false);
  5395. obj.addEventListener('MSPointerCancel', cb, false);
  5396. return this;
  5397. },
  5398. removeMsTouchListener: function (obj, type, id) {
  5399. var pre = '_leaflet_',
  5400. cb = obj[pre + type + id];
  5401. switch (type) {
  5402. case 'touchstart':
  5403. obj.removeEventListener('MSPointerDown', cb, false);
  5404. break;
  5405. case 'touchmove':
  5406. obj.removeEventListener('MSPointerMove', cb, false);
  5407. break;
  5408. case 'touchend':
  5409. obj.removeEventListener('MSPointerUp', cb, false);
  5410. obj.removeEventListener('MSPointerCancel', cb, false);
  5411. break;
  5412. }
  5413. return this;
  5414. }
  5415. });
  5416. /*
  5417. * L.Handler.TouchZoom is used by L.Map to add pinch zoom on supported mobile browsers.
  5418. */
  5419. L.Map.mergeOptions({
  5420. touchZoom: L.Browser.touch && !L.Browser.android23
  5421. });
  5422. L.Map.TouchZoom = L.Handler.extend({
  5423. addHooks: function () {
  5424. L.DomEvent.on(this._map._container, 'touchstart', this._onTouchStart, this);
  5425. },
  5426. removeHooks: function () {
  5427. L.DomEvent.off(this._map._container, 'touchstart', this._onTouchStart, this);
  5428. },
  5429. _onTouchStart: function (e) {
  5430. var map = this._map;
  5431. if (!e.touches || e.touches.length !== 2 || map._animatingZoom || this._zooming) { return; }
  5432. var p1 = map.mouseEventToLayerPoint(e.touches[0]),
  5433. p2 = map.mouseEventToLayerPoint(e.touches[1]),
  5434. viewCenter = map._getCenterLayerPoint();
  5435. this._startCenter = p1.add(p2)._divideBy(2);
  5436. this._startDist = p1.distanceTo(p2);
  5437. this._moved = false;
  5438. this._zooming = true;
  5439. this._centerOffset = viewCenter.subtract(this._startCenter);
  5440. if (map._panAnim) {
  5441. map._panAnim.stop();
  5442. }
  5443. L.DomEvent
  5444. .on(document, 'touchmove', this._onTouchMove, this)
  5445. .on(document, 'touchend', this._onTouchEnd, this);
  5446. L.DomEvent.preventDefault(e);
  5447. },
  5448. _onTouchMove: function (e) {
  5449. var map = this._map;
  5450. if (!e.touches || e.touches.length !== 2 || !this._zooming) { return; }
  5451. var p1 = map.mouseEventToLayerPoint(e.touches[0]),
  5452. p2 = map.mouseEventToLayerPoint(e.touches[1]);
  5453. this._scale = p1.distanceTo(p2) / this._startDist;
  5454. this._delta = p1._add(p2)._divideBy(2)._subtract(this._startCenter);
  5455. if (this._scale === 1) { return; }
  5456. if (!this._moved) {
  5457. L.DomUtil.addClass(map._mapPane, 'leaflet-touching');
  5458. map
  5459. .fire('movestart')
  5460. .fire('zoomstart');
  5461. this._moved = true;
  5462. }
  5463. L.Util.cancelAnimFrame(this._animRequest);
  5464. this._animRequest = L.Util.requestAnimFrame(
  5465. this._updateOnMove, this, true, this._map._container);
  5466. L.DomEvent.preventDefault(e);
  5467. },
  5468. _updateOnMove: function () {
  5469. var map = this._map,
  5470. origin = this._getScaleOrigin(),
  5471. center = map.layerPointToLatLng(origin),
  5472. zoom = map.getScaleZoom(this._scale);
  5473. map._animateZoom(center, zoom, this._startCenter, this._scale, this._delta);
  5474. },
  5475. _onTouchEnd: function () {
  5476. if (!this._moved || !this._zooming) {
  5477. this._zooming = false;
  5478. return;
  5479. }
  5480. var map = this._map;
  5481. this._zooming = false;
  5482. L.DomUtil.removeClass(map._mapPane, 'leaflet-touching');
  5483. L.Util.cancelAnimFrame(this._animRequest);
  5484. L.DomEvent
  5485. .off(document, 'touchmove', this._onTouchMove)
  5486. .off(document, 'touchend', this._onTouchEnd);
  5487. var origin = this._getScaleOrigin(),
  5488. center = map.layerPointToLatLng(origin),
  5489. oldZoom = map.getZoom(),
  5490. floatZoomDelta = map.getScaleZoom(this._scale) - oldZoom,
  5491. roundZoomDelta = (floatZoomDelta > 0 ?
  5492. Math.ceil(floatZoomDelta) : Math.floor(floatZoomDelta)),
  5493. zoom = map._limitZoom(oldZoom + roundZoomDelta),
  5494. scale = map.getZoomScale(zoom) / this._scale;
  5495. map._animateZoom(center, zoom, origin, scale);
  5496. },
  5497. _getScaleOrigin: function () {
  5498. var centerOffset = this._centerOffset.subtract(this._delta).divideBy(this._scale);
  5499. return this._startCenter.add(centerOffset);
  5500. }
  5501. });
  5502. L.Map.addInitHook('addHandler', 'touchZoom', L.Map.TouchZoom);
  5503. /*
  5504. * L.Map.Tap is used to enable mobile hacks like quick taps and long hold.
  5505. */
  5506. L.Map.mergeOptions({
  5507. tap: true,
  5508. tapTolerance: 15
  5509. });
  5510. L.Map.Tap = L.Handler.extend({
  5511. addHooks: function () {
  5512. L.DomEvent.on(this._map._container, 'touchstart', this._onDown, this);
  5513. },
  5514. removeHooks: function () {
  5515. L.DomEvent.off(this._map._container, 'touchstart', this._onDown, this);
  5516. },
  5517. _onDown: function (e) {
  5518. if (!e.touches) { return; }
  5519. L.DomEvent.preventDefault(e);
  5520. this._fireClick = true;
  5521. // don't simulate click or track longpress if more than 1 touch
  5522. if (e.touches.length > 1) {
  5523. this._fireClick = false;
  5524. clearTimeout(this._holdTimeout);
  5525. return;
  5526. }
  5527. var first = e.touches[0],
  5528. el = first.target;
  5529. this._startPos = this._newPos = new L.Point(first.clientX, first.clientY);
  5530. // if touching a link, highlight it
  5531. if (el.tagName.toLowerCase() === 'a') {
  5532. L.DomUtil.addClass(el, 'leaflet-active');
  5533. }
  5534. // simulate long hold but setting a timeout
  5535. this._holdTimeout = setTimeout(L.bind(function () {
  5536. if (this._isTapValid()) {
  5537. this._fireClick = false;
  5538. this._onUp();
  5539. this._simulateEvent('contextmenu', first);
  5540. }
  5541. }, this), 1000);
  5542. L.DomEvent
  5543. .on(document, 'touchmove', this._onMove, this)
  5544. .on(document, 'touchend', this._onUp, this);
  5545. },
  5546. _onUp: function (e) {
  5547. clearTimeout(this._holdTimeout);
  5548. L.DomEvent
  5549. .off(document, 'touchmove', this._onMove, this)
  5550. .off(document, 'touchend', this._onUp, this);
  5551. if (this._fireClick && e && e.changedTouches) {
  5552. var first = e.changedTouches[0],
  5553. el = first.target;
  5554. if (el.tagName.toLowerCase() === 'a') {
  5555. L.DomUtil.removeClass(el, 'leaflet-active');
  5556. }
  5557. // simulate click if the touch didn't move too much
  5558. if (this._isTapValid()) {
  5559. this._simulateEvent('click', first);
  5560. }
  5561. }
  5562. },
  5563. _isTapValid: function () {
  5564. return this._newPos.distanceTo(this._startPos) <= this._map.options.tapTolerance;
  5565. },
  5566. _onMove: function (e) {
  5567. var first = e.touches[0];
  5568. this._newPos = new L.Point(first.clientX, first.clientY);
  5569. },
  5570. _simulateEvent: function (type, e) {
  5571. var simulatedEvent = document.createEvent('MouseEvents');
  5572. simulatedEvent._simulated = true;
  5573. e.target._simulatedClick = true;
  5574. simulatedEvent.initMouseEvent(
  5575. type, true, true, window, 1,
  5576. e.screenX, e.screenY,
  5577. e.clientX, e.clientY,
  5578. false, false, false, false, 0, null);
  5579. e.target.dispatchEvent(simulatedEvent);
  5580. }
  5581. });
  5582. if (L.Browser.touch && !L.Browser.msTouch) {
  5583. L.Map.addInitHook('addHandler', 'tap', L.Map.Tap);
  5584. }
  5585. /*
  5586. * L.Handler.ShiftDragZoom is used to add shift-drag zoom interaction to the map
  5587. * (zoom to a selected bounding box), enabled by default.
  5588. */
  5589. L.Map.mergeOptions({
  5590. boxZoom: true
  5591. });
  5592. L.Map.BoxZoom = L.Handler.extend({
  5593. initialize: function (map) {
  5594. this._map = map;
  5595. this._container = map._container;
  5596. this._pane = map._panes.overlayPane;
  5597. },
  5598. addHooks: function () {
  5599. L.DomEvent.on(this._container, 'mousedown', this._onMouseDown, this);
  5600. },
  5601. removeHooks: function () {
  5602. L.DomEvent.off(this._container, 'mousedown', this._onMouseDown);
  5603. },
  5604. _onMouseDown: function (e) {
  5605. if (!e.shiftKey || ((e.which !== 1) && (e.button !== 1))) { return false; }
  5606. L.DomUtil.disableTextSelection();
  5607. L.DomUtil.disableImageDrag();
  5608. this._startLayerPoint = this._map.mouseEventToLayerPoint(e);
  5609. this._box = L.DomUtil.create('div', 'leaflet-zoom-box', this._pane);
  5610. L.DomUtil.setPosition(this._box, this._startLayerPoint);
  5611. //TODO refactor: move cursor to styles
  5612. this._container.style.cursor = 'crosshair';
  5613. L.DomEvent
  5614. .on(document, 'mousemove', this._onMouseMove, this)
  5615. .on(document, 'mouseup', this._onMouseUp, this)
  5616. .on(document, 'keydown', this._onKeyDown, this);
  5617. this._map.fire('boxzoomstart');
  5618. },
  5619. _onMouseMove: function (e) {
  5620. var startPoint = this._startLayerPoint,
  5621. box = this._box,
  5622. layerPoint = this._map.mouseEventToLayerPoint(e),
  5623. offset = layerPoint.subtract(startPoint),
  5624. newPos = new L.Point(
  5625. Math.min(layerPoint.x, startPoint.x),
  5626. Math.min(layerPoint.y, startPoint.y));
  5627. L.DomUtil.setPosition(box, newPos);
  5628. // TODO refactor: remove hardcoded 4 pixels
  5629. box.style.width = (Math.max(0, Math.abs(offset.x) - 4)) + 'px';
  5630. box.style.height = (Math.max(0, Math.abs(offset.y) - 4)) + 'px';
  5631. },
  5632. _finish: function () {
  5633. this._pane.removeChild(this._box);
  5634. this._container.style.cursor = '';
  5635. L.DomUtil.enableTextSelection();
  5636. L.DomUtil.enableImageDrag();
  5637. L.DomEvent
  5638. .off(document, 'mousemove', this._onMouseMove)
  5639. .off(document, 'mouseup', this._onMouseUp)
  5640. .off(document, 'keydown', this._onKeyDown);
  5641. },
  5642. _onMouseUp: function (e) {
  5643. this._finish();
  5644. var map = this._map,
  5645. layerPoint = map.mouseEventToLayerPoint(e);
  5646. if (this._startLayerPoint.equals(layerPoint)) { return; }
  5647. var bounds = new L.LatLngBounds(
  5648. map.layerPointToLatLng(this._startLayerPoint),
  5649. map.layerPointToLatLng(layerPoint));
  5650. map.fitBounds(bounds);
  5651. map.fire('boxzoomend', {
  5652. boxZoomBounds: bounds
  5653. });
  5654. },
  5655. _onKeyDown: function (e) {
  5656. if (e.keyCode === 27) {
  5657. this._finish();
  5658. }
  5659. }
  5660. });
  5661. L.Map.addInitHook('addHandler', 'boxZoom', L.Map.BoxZoom);
  5662. /*
  5663. * L.Map.Keyboard is handling keyboard interaction with the map, enabled by default.
  5664. */
  5665. L.Map.mergeOptions({
  5666. keyboard: true,
  5667. keyboardPanOffset: 80,
  5668. keyboardZoomOffset: 1
  5669. });
  5670. L.Map.Keyboard = L.Handler.extend({
  5671. keyCodes: {
  5672. left: [37],
  5673. right: [39],
  5674. down: [40],
  5675. up: [38],
  5676. zoomIn: [187, 107, 61],
  5677. zoomOut: [189, 109, 173]
  5678. },
  5679. initialize: function (map) {
  5680. this._map = map;
  5681. this._setPanOffset(map.options.keyboardPanOffset);
  5682. this._setZoomOffset(map.options.keyboardZoomOffset);
  5683. },
  5684. addHooks: function () {
  5685. var container = this._map._container;
  5686. // make the container focusable by tabbing
  5687. if (container.tabIndex === -1) {
  5688. container.tabIndex = '0';
  5689. }
  5690. L.DomEvent
  5691. .on(container, 'focus', this._onFocus, this)
  5692. .on(container, 'blur', this._onBlur, this)
  5693. .on(container, 'mousedown', this._onMouseDown, this);
  5694. this._map
  5695. .on('focus', this._addHooks, this)
  5696. .on('blur', this._removeHooks, this);
  5697. },
  5698. removeHooks: function () {
  5699. this._removeHooks();
  5700. var container = this._map._container;
  5701. L.DomEvent
  5702. .off(container, 'focus', this._onFocus, this)
  5703. .off(container, 'blur', this._onBlur, this)
  5704. .off(container, 'mousedown', this._onMouseDown, this);
  5705. this._map
  5706. .off('focus', this._addHooks, this)
  5707. .off('blur', this._removeHooks, this);
  5708. },
  5709. _onMouseDown: function () {
  5710. if (this._focused) { return; }
  5711. var body = document.body,
  5712. docEl = document.documentElement,
  5713. top = body.scrollTop || docEl.scrollTop,
  5714. left = body.scrollLeft || docEl.scrollLeft;
  5715. this._map._container.focus();
  5716. window.scrollTo(left, top);
  5717. },
  5718. _onFocus: function () {
  5719. this._focused = true;
  5720. this._map.fire('focus');
  5721. },
  5722. _onBlur: function () {
  5723. this._focused = false;
  5724. this._map.fire('blur');
  5725. },
  5726. _setPanOffset: function (pan) {
  5727. var keys = this._panKeys = {},
  5728. codes = this.keyCodes,
  5729. i, len;
  5730. for (i = 0, len = codes.left.length; i < len; i++) {
  5731. keys[codes.left[i]] = [-1 * pan, 0];
  5732. }
  5733. for (i = 0, len = codes.right.length; i < len; i++) {
  5734. keys[codes.right[i]] = [pan, 0];
  5735. }
  5736. for (i = 0, len = codes.down.length; i < len; i++) {
  5737. keys[codes.down[i]] = [0, pan];
  5738. }
  5739. for (i = 0, len = codes.up.length; i < len; i++) {
  5740. keys[codes.up[i]] = [0, -1 * pan];
  5741. }
  5742. },
  5743. _setZoomOffset: function (zoom) {
  5744. var keys = this._zoomKeys = {},
  5745. codes = this.keyCodes,
  5746. i, len;
  5747. for (i = 0, len = codes.zoomIn.length; i < len; i++) {
  5748. keys[codes.zoomIn[i]] = zoom;
  5749. }
  5750. for (i = 0, len = codes.zoomOut.length; i < len; i++) {
  5751. keys[codes.zoomOut[i]] = -zoom;
  5752. }
  5753. },
  5754. _addHooks: function () {
  5755. L.DomEvent.on(document, 'keydown', this._onKeyDown, this);
  5756. },
  5757. _removeHooks: function () {
  5758. L.DomEvent.off(document, 'keydown', this._onKeyDown, this);
  5759. },
  5760. _onKeyDown: function (e) {
  5761. var key = e.keyCode,
  5762. map = this._map;
  5763. if (key in this._panKeys) {
  5764. if (map._panAnim && map._panAnim._inProgress) { return; }
  5765. map.panBy(this._panKeys[key]);
  5766. if (map.options.maxBounds) {
  5767. map.panInsideBounds(map.options.maxBounds);
  5768. }
  5769. } else if (key in this._zoomKeys) {
  5770. map.setZoom(map.getZoom() + this._zoomKeys[key]);
  5771. } else {
  5772. return;
  5773. }
  5774. L.DomEvent.stop(e);
  5775. }
  5776. });
  5777. L.Map.addInitHook('addHandler', 'keyboard', L.Map.Keyboard);
  5778. /*
  5779. * L.Handler.MarkerDrag is used internally by L.Marker to make the markers draggable.
  5780. */
  5781. L.Handler.MarkerDrag = L.Handler.extend({
  5782. initialize: function (marker) {
  5783. this._marker = marker;
  5784. },
  5785. addHooks: function () {
  5786. var icon = this._marker._icon;
  5787. if (!this._draggable) {
  5788. this._draggable = new L.Draggable(icon, icon);
  5789. }
  5790. this._draggable
  5791. .on('dragstart', this._onDragStart, this)
  5792. .on('drag', this._onDrag, this)
  5793. .on('dragend', this._onDragEnd, this);
  5794. this._draggable.enable();
  5795. },
  5796. removeHooks: function () {
  5797. this._draggable
  5798. .off('dragstart', this._onDragStart, this)
  5799. .off('drag', this._onDrag, this)
  5800. .off('dragend', this._onDragEnd, this);
  5801. this._draggable.disable();
  5802. },
  5803. moved: function () {
  5804. return this._draggable && this._draggable._moved;
  5805. },
  5806. _onDragStart: function () {
  5807. this._marker
  5808. .closePopup()
  5809. .fire('movestart')
  5810. .fire('dragstart');
  5811. },
  5812. _onDrag: function () {
  5813. var marker = this._marker,
  5814. shadow = marker._shadow,
  5815. iconPos = L.DomUtil.getPosition(marker._icon),
  5816. latlng = marker._map.layerPointToLatLng(iconPos);
  5817. // update shadow position
  5818. if (shadow) {
  5819. L.DomUtil.setPosition(shadow, iconPos);
  5820. }
  5821. marker._latlng = latlng;
  5822. marker
  5823. .fire('move', {latlng: latlng})
  5824. .fire('drag');
  5825. },
  5826. _onDragEnd: function () {
  5827. this._marker
  5828. .fire('moveend')
  5829. .fire('dragend');
  5830. }
  5831. });
  5832. /*
  5833. * L.Control is a base class for implementing map controls. Handles positioning.
  5834. * All other controls extend from this class.
  5835. */
  5836. L.Control = L.Class.extend({
  5837. options: {
  5838. position: 'topright'
  5839. },
  5840. initialize: function (options) {
  5841. L.setOptions(this, options);
  5842. },
  5843. getPosition: function () {
  5844. return this.options.position;
  5845. },
  5846. setPosition: function (position) {
  5847. var map = this._map;
  5848. if (map) {
  5849. map.removeControl(this);
  5850. }
  5851. this.options.position = position;
  5852. if (map) {
  5853. map.addControl(this);
  5854. }
  5855. return this;
  5856. },
  5857. getContainer: function () {
  5858. return this._container;
  5859. },
  5860. addTo: function (map) {
  5861. this._map = map;
  5862. var container = this._container = this.onAdd(map),
  5863. pos = this.getPosition(),
  5864. corner = map._controlCorners[pos];
  5865. L.DomUtil.addClass(container, 'leaflet-control');
  5866. if (pos.indexOf('bottom') !== -1) {
  5867. corner.insertBefore(container, corner.firstChild);
  5868. } else {
  5869. corner.appendChild(container);
  5870. }
  5871. return this;
  5872. },
  5873. removeFrom: function (map) {
  5874. var pos = this.getPosition(),
  5875. corner = map._controlCorners[pos];
  5876. corner.removeChild(this._container);
  5877. this._map = null;
  5878. if (this.onRemove) {
  5879. this.onRemove(map);
  5880. }
  5881. return this;
  5882. }
  5883. });
  5884. L.control = function (options) {
  5885. return new L.Control(options);
  5886. };
  5887. // adds control-related methods to L.Map
  5888. L.Map.include({
  5889. addControl: function (control) {
  5890. control.addTo(this);
  5891. return this;
  5892. },
  5893. removeControl: function (control) {
  5894. control.removeFrom(this);
  5895. return this;
  5896. },
  5897. _initControlPos: function () {
  5898. var corners = this._controlCorners = {},
  5899. l = 'leaflet-',
  5900. container = this._controlContainer =
  5901. L.DomUtil.create('div', l + 'control-container', this._container);
  5902. function createCorner(vSide, hSide) {
  5903. var className = l + vSide + ' ' + l + hSide;
  5904. corners[vSide + hSide] = L.DomUtil.create('div', className, container);
  5905. }
  5906. createCorner('top', 'left');
  5907. createCorner('top', 'right');
  5908. createCorner('bottom', 'left');
  5909. createCorner('bottom', 'right');
  5910. },
  5911. _clearControlPos: function () {
  5912. this._container.removeChild(this._controlContainer);
  5913. }
  5914. });
  5915. /*
  5916. * L.Control.Zoom is used for the default zoom buttons on the map.
  5917. */
  5918. L.Control.Zoom = L.Control.extend({
  5919. options: {
  5920. position: 'topleft'
  5921. },
  5922. onAdd: function (map) {
  5923. var zoomName = 'leaflet-control-zoom',
  5924. container = L.DomUtil.create('div', zoomName + ' leaflet-bar');
  5925. this._map = map;
  5926. this._zoomInButton = this._createButton(
  5927. '+', 'Zoom in', zoomName + '-in', container, this._zoomIn, this);
  5928. this._zoomOutButton = this._createButton(
  5929. '-', 'Zoom out', zoomName + '-out', container, this._zoomOut, this);
  5930. map.on('zoomend zoomlevelschange', this._updateDisabled, this);
  5931. return container;
  5932. },
  5933. onRemove: function (map) {
  5934. map.off('zoomend zoomlevelschange', this._updateDisabled, this);
  5935. },
  5936. _zoomIn: function (e) {
  5937. this._map.zoomIn(e.shiftKey ? 3 : 1);
  5938. },
  5939. _zoomOut: function (e) {
  5940. this._map.zoomOut(e.shiftKey ? 3 : 1);
  5941. },
  5942. _createButton: function (html, title, className, container, fn, context) {
  5943. var link = L.DomUtil.create('a', className, container);
  5944. link.innerHTML = html;
  5945. link.href = '#';
  5946. link.title = title;
  5947. var stop = L.DomEvent.stopPropagation;
  5948. L.DomEvent
  5949. .on(link, 'click', stop)
  5950. .on(link, 'mousedown', stop)
  5951. .on(link, 'dblclick', stop)
  5952. .on(link, 'click', L.DomEvent.preventDefault)
  5953. .on(link, 'click', fn, context);
  5954. return link;
  5955. },
  5956. _updateDisabled: function () {
  5957. var map = this._map,
  5958. className = 'leaflet-disabled';
  5959. L.DomUtil.removeClass(this._zoomInButton, className);
  5960. L.DomUtil.removeClass(this._zoomOutButton, className);
  5961. if (map._zoom === map.getMinZoom()) {
  5962. L.DomUtil.addClass(this._zoomOutButton, className);
  5963. }
  5964. if (map._zoom === map.getMaxZoom()) {
  5965. L.DomUtil.addClass(this._zoomInButton, className);
  5966. }
  5967. }
  5968. });
  5969. L.Map.mergeOptions({
  5970. zoomControl: true
  5971. });
  5972. L.Map.addInitHook(function () {
  5973. if (this.options.zoomControl) {
  5974. this.zoomControl = new L.Control.Zoom();
  5975. this.addControl(this.zoomControl);
  5976. }
  5977. });
  5978. L.control.zoom = function (options) {
  5979. return new L.Control.Zoom(options);
  5980. };
  5981. /*
  5982. * L.Control.Attribution is used for displaying attribution on the map (added by default).
  5983. */
  5984. L.Control.Attribution = L.Control.extend({
  5985. options: {
  5986. position: 'bottomright',
  5987. prefix: '<a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a>'
  5988. },
  5989. initialize: function (options) {
  5990. L.setOptions(this, options);
  5991. this._attributions = {};
  5992. },
  5993. onAdd: function (map) {
  5994. this._container = L.DomUtil.create('div', 'leaflet-control-attribution');
  5995. L.DomEvent.disableClickPropagation(this._container);
  5996. map
  5997. .on('layeradd', this._onLayerAdd, this)
  5998. .on('layerremove', this._onLayerRemove, this);
  5999. this._update();
  6000. return this._container;
  6001. },
  6002. onRemove: function (map) {
  6003. map
  6004. .off('layeradd', this._onLayerAdd)
  6005. .off('layerremove', this._onLayerRemove);
  6006. },
  6007. setPrefix: function (prefix) {
  6008. this.options.prefix = prefix;
  6009. this._update();
  6010. return this;
  6011. },
  6012. addAttribution: function (text) {
  6013. if (!text) { return; }
  6014. if (!this._attributions[text]) {
  6015. this._attributions[text] = 0;
  6016. }
  6017. this._attributions[text]++;
  6018. this._update();
  6019. return this;
  6020. },
  6021. removeAttribution: function (text) {
  6022. if (!text) { return; }
  6023. if (this._attributions[text]) {
  6024. this._attributions[text]--;
  6025. this._update();
  6026. }
  6027. return this;
  6028. },
  6029. _update: function () {
  6030. if (!this._map) { return; }
  6031. var attribs = [];
  6032. for (var i in this._attributions) {
  6033. if (this._attributions[i]) {
  6034. attribs.push(i);
  6035. }
  6036. }
  6037. var prefixAndAttribs = [];
  6038. if (this.options.prefix) {
  6039. prefixAndAttribs.push(this.options.prefix);
  6040. }
  6041. if (attribs.length) {
  6042. prefixAndAttribs.push(attribs.join(', '));
  6043. }
  6044. this._container.innerHTML = prefixAndAttribs.join(' | ');
  6045. },
  6046. _onLayerAdd: function (e) {
  6047. if (e.layer.getAttribution) {
  6048. this.addAttribution(e.layer.getAttribution());
  6049. }
  6050. },
  6051. _onLayerRemove: function (e) {
  6052. if (e.layer.getAttribution) {
  6053. this.removeAttribution(e.layer.getAttribution());
  6054. }
  6055. }
  6056. });
  6057. L.Map.mergeOptions({
  6058. attributionControl: true
  6059. });
  6060. L.Map.addInitHook(function () {
  6061. if (this.options.attributionControl) {
  6062. this.attributionControl = (new L.Control.Attribution()).addTo(this);
  6063. }
  6064. });
  6065. L.control.attribution = function (options) {
  6066. return new L.Control.Attribution(options);
  6067. };
  6068. /*
  6069. * L.Control.Scale is used for displaying metric/imperial scale on the map.
  6070. */
  6071. L.Control.Scale = L.Control.extend({
  6072. options: {
  6073. position: 'bottomleft',
  6074. maxWidth: 100,
  6075. metric: true,
  6076. imperial: true,
  6077. updateWhenIdle: false
  6078. },
  6079. onAdd: function (map) {
  6080. this._map = map;
  6081. var className = 'leaflet-control-scale',
  6082. container = L.DomUtil.create('div', className),
  6083. options = this.options;
  6084. this._addScales(options, className, container);
  6085. map.on(options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
  6086. map.whenReady(this._update, this);
  6087. return container;
  6088. },
  6089. onRemove: function (map) {
  6090. map.off(this.options.updateWhenIdle ? 'moveend' : 'move', this._update, this);
  6091. },
  6092. _addScales: function (options, className, container) {
  6093. if (options.metric) {
  6094. this._mScale = L.DomUtil.create('div', className + '-line', container);
  6095. }
  6096. if (options.imperial) {
  6097. this._iScale = L.DomUtil.create('div', className + '-line', container);
  6098. }
  6099. },
  6100. _update: function () {
  6101. var bounds = this._map.getBounds(),
  6102. centerLat = bounds.getCenter().lat,
  6103. halfWorldMeters = 6378137 * Math.PI * Math.cos(centerLat * Math.PI / 180),
  6104. dist = halfWorldMeters * (bounds.getNorthEast().lng - bounds.getSouthWest().lng) / 180,
  6105. size = this._map.getSize(),
  6106. options = this.options,
  6107. maxMeters = 0;
  6108. if (size.x > 0) {
  6109. maxMeters = dist * (options.maxWidth / size.x);
  6110. }
  6111. this._updateScales(options, maxMeters);
  6112. },
  6113. _updateScales: function (options, maxMeters) {
  6114. if (options.metric && maxMeters) {
  6115. this._updateMetric(maxMeters);
  6116. }
  6117. if (options.imperial && maxMeters) {
  6118. this._updateImperial(maxMeters);
  6119. }
  6120. },
  6121. _updateMetric: function (maxMeters) {
  6122. var meters = this._getRoundNum(maxMeters);
  6123. this._mScale.style.width = this._getScaleWidth(meters / maxMeters) + 'px';
  6124. this._mScale.innerHTML = meters < 1000 ? meters + ' m' : (meters / 1000) + ' km';
  6125. },
  6126. _updateImperial: function (maxMeters) {
  6127. var maxFeet = maxMeters * 3.2808399,
  6128. scale = this._iScale,
  6129. maxMiles, miles, feet;
  6130. if (maxFeet > 5280) {
  6131. maxMiles = maxFeet / 5280;
  6132. miles = this._getRoundNum(maxMiles);
  6133. scale.style.width = this._getScaleWidth(miles / maxMiles) + 'px';
  6134. scale.innerHTML = miles + ' mi';
  6135. } else {
  6136. feet = this._getRoundNum(maxFeet);
  6137. scale.style.width = this._getScaleWidth(feet / maxFeet) + 'px';
  6138. scale.innerHTML = feet + ' ft';
  6139. }
  6140. },
  6141. _getScaleWidth: function (ratio) {
  6142. return Math.round(this.options.maxWidth * ratio) - 10;
  6143. },
  6144. _getRoundNum: function (num) {
  6145. var pow10 = Math.pow(10, (Math.floor(num) + '').length - 1),
  6146. d = num / pow10;
  6147. d = d >= 10 ? 10 : d >= 5 ? 5 : d >= 3 ? 3 : d >= 2 ? 2 : 1;
  6148. return pow10 * d;
  6149. }
  6150. });
  6151. L.control.scale = function (options) {
  6152. return new L.Control.Scale(options);
  6153. };
  6154. /*
  6155. * L.Control.Layers is a control to allow users to switch between different layers on the map.
  6156. */
  6157. L.Control.Layers = L.Control.extend({
  6158. options: {
  6159. collapsed: true,
  6160. position: 'topright',
  6161. autoZIndex: true
  6162. },
  6163. initialize: function (baseLayers, overlays, options) {
  6164. L.setOptions(this, options);
  6165. this._layers = {};
  6166. this._lastZIndex = 0;
  6167. this._handlingClick = false;
  6168. for (var i in baseLayers) {
  6169. this._addLayer(baseLayers[i], i);
  6170. }
  6171. for (i in overlays) {
  6172. this._addLayer(overlays[i], i, true);
  6173. }
  6174. },
  6175. onAdd: function (map) {
  6176. this._initLayout();
  6177. this._update();
  6178. map
  6179. .on('layeradd', this._onLayerChange, this)
  6180. .on('layerremove', this._onLayerChange, this);
  6181. return this._container;
  6182. },
  6183. onRemove: function (map) {
  6184. map
  6185. .off('layeradd', this._onLayerChange)
  6186. .off('layerremove', this._onLayerChange);
  6187. },
  6188. addBaseLayer: function (layer, name) {
  6189. this._addLayer(layer, name);
  6190. this._update();
  6191. return this;
  6192. },
  6193. addOverlay: function (layer, name) {
  6194. this._addLayer(layer, name, true);
  6195. this._update();
  6196. return this;
  6197. },
  6198. removeLayer: function (layer) {
  6199. var id = L.stamp(layer);
  6200. delete this._layers[id];
  6201. this._update();
  6202. return this;
  6203. },
  6204. _initLayout: function () {
  6205. var className = 'leaflet-control-layers',
  6206. container = this._container = L.DomUtil.create('div', className);
  6207. //Makes this work on IE10 Touch devices by stopping it from firing a mouseout event when the touch is released
  6208. container.setAttribute('aria-haspopup', true);
  6209. if (!L.Browser.touch) {
  6210. L.DomEvent.disableClickPropagation(container);
  6211. L.DomEvent.on(container, 'mousewheel', L.DomEvent.stopPropagation);
  6212. } else {
  6213. L.DomEvent.on(container, 'click', L.DomEvent.stopPropagation);
  6214. }
  6215. var form = this._form = L.DomUtil.create('form', className + '-list');
  6216. if (this.options.collapsed) {
  6217. if (!L.Browser.android) {
  6218. L.DomEvent
  6219. .on(container, 'mouseover', this._expand, this)
  6220. .on(container, 'mouseout', this._collapse, this);
  6221. }
  6222. var link = this._layersLink = L.DomUtil.create('a', className + '-toggle', container);
  6223. link.href = '#';
  6224. link.title = 'Layers';
  6225. if (L.Browser.touch) {
  6226. L.DomEvent
  6227. .on(link, 'click', L.DomEvent.stop)
  6228. .on(link, 'click', this._expand, this);
  6229. }
  6230. else {
  6231. L.DomEvent.on(link, 'focus', this._expand, this);
  6232. }
  6233. this._map.on('click', this._collapse, this);
  6234. // TODO keyboard accessibility
  6235. } else {
  6236. this._expand();
  6237. }
  6238. this._baseLayersList = L.DomUtil.create('div', className + '-base', form);
  6239. this._separator = L.DomUtil.create('div', className + '-separator', form);
  6240. this._overlaysList = L.DomUtil.create('div', className + '-overlays', form);
  6241. container.appendChild(form);
  6242. },
  6243. _addLayer: function (layer, name, overlay) {
  6244. var id = L.stamp(layer);
  6245. this._layers[id] = {
  6246. layer: layer,
  6247. name: name,
  6248. overlay: overlay
  6249. };
  6250. if (this.options.autoZIndex && layer.setZIndex) {
  6251. this._lastZIndex++;
  6252. layer.setZIndex(this._lastZIndex);
  6253. }
  6254. },
  6255. _update: function () {
  6256. if (!this._container) {
  6257. return;
  6258. }
  6259. this._baseLayersList.innerHTML = '';
  6260. this._overlaysList.innerHTML = '';
  6261. var baseLayersPresent = false,
  6262. overlaysPresent = false,
  6263. i, obj;
  6264. for (i in this._layers) {
  6265. obj = this._layers[i];
  6266. this._addItem(obj);
  6267. overlaysPresent = overlaysPresent || obj.overlay;
  6268. baseLayersPresent = baseLayersPresent || !obj.overlay;
  6269. }
  6270. this._separator.style.display = overlaysPresent && baseLayersPresent ? '' : 'none';
  6271. },
  6272. _onLayerChange: function (e) {
  6273. var obj = this._layers[L.stamp(e.layer)];
  6274. if (!obj) { return; }
  6275. if (!this._handlingClick) {
  6276. this._update();
  6277. }
  6278. var type = obj.overlay ?
  6279. (e.type === 'layeradd' ? 'overlayadd' : 'overlayremove') :
  6280. (e.type === 'layeradd' ? 'baselayerchange' : null);
  6281. if (type) {
  6282. this._map.fire(type, obj);
  6283. }
  6284. },
  6285. // IE7 bugs out if you create a radio dynamically, so you have to do it this hacky way (see http://bit.ly/PqYLBe)
  6286. _createRadioElement: function (name, checked) {
  6287. var radioHtml = '<input type="radio" class="leaflet-control-layers-selector" name="' + name + '"';
  6288. if (checked) {
  6289. radioHtml += ' checked="checked"';
  6290. }
  6291. radioHtml += '/>';
  6292. var radioFragment = document.createElement('div');
  6293. radioFragment.innerHTML = radioHtml;
  6294. return radioFragment.firstChild;
  6295. },
  6296. _addItem: function (obj) {
  6297. var label = document.createElement('label'),
  6298. input,
  6299. checked = this._map.hasLayer(obj.layer);
  6300. if (obj.overlay) {
  6301. input = document.createElement('input');
  6302. input.type = 'checkbox';
  6303. input.className = 'leaflet-control-layers-selector';
  6304. input.defaultChecked = checked;
  6305. } else {
  6306. input = this._createRadioElement('leaflet-base-layers', checked);
  6307. }
  6308. input.layerId = L.stamp(obj.layer);
  6309. L.DomEvent.on(input, 'click', this._onInputClick, this);
  6310. var name = document.createElement('span');
  6311. name.innerHTML = ' ' + obj.name;
  6312. label.appendChild(input);
  6313. label.appendChild(name);
  6314. var container = obj.overlay ? this._overlaysList : this._baseLayersList;
  6315. container.appendChild(label);
  6316. return label;
  6317. },
  6318. _onInputClick: function () {
  6319. var i, input, obj,
  6320. inputs = this._form.getElementsByTagName('input'),
  6321. inputsLen = inputs.length;
  6322. this._handlingClick = true;
  6323. for (i = 0; i < inputsLen; i++) {
  6324. input = inputs[i];
  6325. obj = this._layers[input.layerId];
  6326. if (input.checked && !this._map.hasLayer(obj.layer)) {
  6327. this._map.addLayer(obj.layer);
  6328. } else if (!input.checked && this._map.hasLayer(obj.layer)) {
  6329. this._map.removeLayer(obj.layer);
  6330. }
  6331. }
  6332. this._handlingClick = false;
  6333. },
  6334. _expand: function () {
  6335. L.DomUtil.addClass(this._container, 'leaflet-control-layers-expanded');
  6336. },
  6337. _collapse: function () {
  6338. this._container.className = this._container.className.replace(' leaflet-control-layers-expanded', '');
  6339. }
  6340. });
  6341. L.control.layers = function (baseLayers, overlays, options) {
  6342. return new L.Control.Layers(baseLayers, overlays, options);
  6343. };
  6344. /*
  6345. * L.PosAnimation is used by Leaflet internally for pan animations.
  6346. */
  6347. L.PosAnimation = L.Class.extend({
  6348. includes: L.Mixin.Events,
  6349. run: function (el, newPos, duration, easeLinearity) { // (HTMLElement, Point[, Number, Number])
  6350. this.stop();
  6351. this._el = el;
  6352. this._inProgress = true;
  6353. this._newPos = newPos;
  6354. this.fire('start');
  6355. el.style[L.DomUtil.TRANSITION] = 'all ' + (duration || 0.25) +
  6356. 's cubic-bezier(0,0,' + (easeLinearity || 0.5) + ',1)';
  6357. L.DomEvent.on(el, L.DomUtil.TRANSITION_END, this._onTransitionEnd, this);
  6358. L.DomUtil.setPosition(el, newPos);
  6359. // toggle reflow, Chrome flickers for some reason if you don't do this
  6360. L.Util.falseFn(el.offsetWidth);
  6361. // there's no native way to track value updates of transitioned properties, so we imitate this
  6362. this._stepTimer = setInterval(L.bind(this._onStep, this), 50);
  6363. },
  6364. stop: function () {
  6365. if (!this._inProgress) { return; }
  6366. // if we just removed the transition property, the element would jump to its final position,
  6367. // so we need to make it stay at the current position
  6368. L.DomUtil.setPosition(this._el, this._getPos());
  6369. this._onTransitionEnd();
  6370. L.Util.falseFn(this._el.offsetWidth); // force reflow in case we are about to start a new animation
  6371. },
  6372. _onStep: function () {
  6373. var stepPos = this._getPos();
  6374. if (!stepPos) {
  6375. this._onTransitionEnd();
  6376. return;
  6377. }
  6378. // jshint camelcase: false
  6379. // make L.DomUtil.getPosition return intermediate position value during animation
  6380. this._el._leaflet_pos = stepPos;
  6381. this.fire('step');
  6382. },
  6383. // you can't easily get intermediate values of properties animated with CSS3 Transitions,
  6384. // we need to parse computed style (in case of transform it returns matrix string)
  6385. _transformRe: /([-+]?(?:\d*\.)?\d+)\D*, ([-+]?(?:\d*\.)?\d+)\D*\)/,
  6386. _getPos: function () {
  6387. var left, top, matches,
  6388. el = this._el,
  6389. style = window.getComputedStyle(el);
  6390. if (L.Browser.any3d) {
  6391. matches = style[L.DomUtil.TRANSFORM].match(this._transformRe);
  6392. if (!matches) { return; }
  6393. left = parseFloat(matches[1]);
  6394. top = parseFloat(matches[2]);
  6395. } else {
  6396. left = parseFloat(style.left);
  6397. top = parseFloat(style.top);
  6398. }
  6399. return new L.Point(left, top, true);
  6400. },
  6401. _onTransitionEnd: function () {
  6402. L.DomEvent.off(this._el, L.DomUtil.TRANSITION_END, this._onTransitionEnd, this);
  6403. if (!this._inProgress) { return; }
  6404. this._inProgress = false;
  6405. this._el.style[L.DomUtil.TRANSITION] = '';
  6406. // jshint camelcase: false
  6407. // make sure L.DomUtil.getPosition returns the final position value after animation
  6408. this._el._leaflet_pos = this._newPos;
  6409. clearInterval(this._stepTimer);
  6410. this.fire('step').fire('end');
  6411. }
  6412. });
  6413. /*
  6414. * Extends L.Map to handle panning animations.
  6415. */
  6416. L.Map.include({
  6417. setView: function (center, zoom, options) {
  6418. zoom = this._limitZoom(zoom);
  6419. center = L.latLng(center);
  6420. options = options || {};
  6421. if (this._panAnim) {
  6422. this._panAnim.stop();
  6423. }
  6424. if (this._loaded && !options.reset && options !== true) {
  6425. if (options.animate !== undefined) {
  6426. options.zoom = L.extend({animate: options.animate}, options.zoom);
  6427. options.pan = L.extend({animate: options.animate}, options.pan);
  6428. }
  6429. // try animating pan or zoom
  6430. var animated = (this._zoom !== zoom) ?
  6431. this._tryAnimatedZoom && this._tryAnimatedZoom(center, zoom, options.zoom) :
  6432. this._tryAnimatedPan(center, options.pan);
  6433. if (animated) {
  6434. // prevent resize handler call, the view will refresh after animation anyway
  6435. clearTimeout(this._sizeTimer);
  6436. return this;
  6437. }
  6438. }
  6439. // animation didn't start, just reset the map view
  6440. this._resetView(center, zoom);
  6441. return this;
  6442. },
  6443. panBy: function (offset, options) {
  6444. offset = L.point(offset).round();
  6445. options = options || {};
  6446. if (!offset.x && !offset.y) {
  6447. return this;
  6448. }
  6449. if (!this._panAnim) {
  6450. this._panAnim = new L.PosAnimation();
  6451. this._panAnim.on({
  6452. 'step': this._onPanTransitionStep,
  6453. 'end': this._onPanTransitionEnd
  6454. }, this);
  6455. }
  6456. // don't fire movestart if animating inertia
  6457. if (!options.noMoveStart) {
  6458. this.fire('movestart');
  6459. }
  6460. // animate pan unless animate: false specified
  6461. if (options.animate !== false) {
  6462. L.DomUtil.addClass(this._mapPane, 'leaflet-pan-anim');
  6463. var newPos = this._getMapPanePos().subtract(offset);
  6464. this._panAnim.run(this._mapPane, newPos, options.duration || 0.25, options.easeLinearity);
  6465. } else {
  6466. this._rawPanBy(offset);
  6467. this.fire('move').fire('moveend');
  6468. }
  6469. return this;
  6470. },
  6471. _onPanTransitionStep: function () {
  6472. this.fire('move');
  6473. },
  6474. _onPanTransitionEnd: function () {
  6475. L.DomUtil.removeClass(this._mapPane, 'leaflet-pan-anim');
  6476. this.fire('moveend');
  6477. },
  6478. _tryAnimatedPan: function (center, options) {
  6479. // difference between the new and current centers in pixels
  6480. var offset = this._getCenterOffset(center)._floor();
  6481. // don't animate too far unless animate: true specified in options
  6482. if ((options && options.animate) !== true && !this.getSize().contains(offset)) { return false; }
  6483. this.panBy(offset, options);
  6484. return true;
  6485. }
  6486. });
  6487. /*
  6488. * L.PosAnimation fallback implementation that powers Leaflet pan animations
  6489. * in browsers that don't support CSS3 Transitions.
  6490. */
  6491. L.PosAnimation = L.DomUtil.TRANSITION ? L.PosAnimation : L.PosAnimation.extend({
  6492. run: function (el, newPos, duration, easeLinearity) { // (HTMLElement, Point[, Number, Number])
  6493. this.stop();
  6494. this._el = el;
  6495. this._inProgress = true;
  6496. this._duration = duration || 0.25;
  6497. this._easeOutPower = 1 / Math.max(easeLinearity || 0.5, 0.2);
  6498. this._startPos = L.DomUtil.getPosition(el);
  6499. this._offset = newPos.subtract(this._startPos);
  6500. this._startTime = +new Date();
  6501. this.fire('start');
  6502. this._animate();
  6503. },
  6504. stop: function () {
  6505. if (!this._inProgress) { return; }
  6506. this._step();
  6507. this._complete();
  6508. },
  6509. _animate: function () {
  6510. // animation loop
  6511. this._animId = L.Util.requestAnimFrame(this._animate, this);
  6512. this._step();
  6513. },
  6514. _step: function () {
  6515. var elapsed = (+new Date()) - this._startTime,
  6516. duration = this._duration * 1000;
  6517. if (elapsed < duration) {
  6518. this._runFrame(this._easeOut(elapsed / duration));
  6519. } else {
  6520. this._runFrame(1);
  6521. this._complete();
  6522. }
  6523. },
  6524. _runFrame: function (progress) {
  6525. var pos = this._startPos.add(this._offset.multiplyBy(progress));
  6526. L.DomUtil.setPosition(this._el, pos);
  6527. this.fire('step');
  6528. },
  6529. _complete: function () {
  6530. L.Util.cancelAnimFrame(this._animId);
  6531. this._inProgress = false;
  6532. this.fire('end');
  6533. },
  6534. _easeOut: function (t) {
  6535. return 1 - Math.pow(1 - t, this._easeOutPower);
  6536. }
  6537. });
  6538. /*
  6539. * Extends L.Map to handle zoom animations.
  6540. */
  6541. L.Map.mergeOptions({
  6542. zoomAnimation: true,
  6543. zoomAnimationThreshold: 4
  6544. });
  6545. if (L.DomUtil.TRANSITION) {
  6546. L.Map.addInitHook(function () {
  6547. // don't animate on browsers without hardware-accelerated transitions or old Android/Opera
  6548. this._zoomAnimated = this.options.zoomAnimation && L.DomUtil.TRANSITION &&
  6549. L.Browser.any3d && !L.Browser.android23 && !L.Browser.mobileOpera;
  6550. // zoom transitions run with the same duration for all layers, so if one of transitionend events
  6551. // happens after starting zoom animation (propagating to the map pane), we know that it ended globally
  6552. if (this._zoomAnimated) {
  6553. L.DomEvent.on(this._mapPane, L.DomUtil.TRANSITION_END, this._catchTransitionEnd, this);
  6554. }
  6555. });
  6556. }
  6557. L.Map.include(!L.DomUtil.TRANSITION ? {} : {
  6558. _catchTransitionEnd: function () {
  6559. if (this._animatingZoom) {
  6560. this._onZoomTransitionEnd();
  6561. }
  6562. },
  6563. _nothingToAnimate: function () {
  6564. return !this._container.getElementsByClassName('leaflet-zoom-animated').length;
  6565. },
  6566. _tryAnimatedZoom: function (center, zoom, options) {
  6567. if (this._animatingZoom) { return true; }
  6568. options = options || {};
  6569. // don't animate if disabled, not supported or zoom difference is too large
  6570. if (!this._zoomAnimated || options.animate === false || this._nothingToAnimate() ||
  6571. Math.abs(zoom - this._zoom) > this.options.zoomAnimationThreshold) { return false; }
  6572. // offset is the pixel coords of the zoom origin relative to the current center
  6573. var scale = this.getZoomScale(zoom),
  6574. offset = this._getCenterOffset(center)._divideBy(1 - 1 / scale),
  6575. origin = this._getCenterLayerPoint()._add(offset);
  6576. // don't animate if the zoom origin isn't within one screen from the current center, unless forced
  6577. if (options.animate !== true && !this.getSize().contains(offset)) { return false; }
  6578. this
  6579. .fire('movestart')
  6580. .fire('zoomstart');
  6581. this._animateZoom(center, zoom, origin, scale, null, true);
  6582. return true;
  6583. },
  6584. _animateZoom: function (center, zoom, origin, scale, delta, backwards) {
  6585. this._animatingZoom = true;
  6586. // put transform transition on all layers with leaflet-zoom-animated class
  6587. L.DomUtil.addClass(this._mapPane, 'leaflet-zoom-anim');
  6588. // remember what center/zoom to set after animation
  6589. this._animateToCenter = center;
  6590. this._animateToZoom = zoom;
  6591. // disable any dragging during animation
  6592. if (L.Draggable) {
  6593. L.Draggable._disabled = true;
  6594. }
  6595. this.fire('zoomanim', {
  6596. center: center,
  6597. zoom: zoom,
  6598. origin: origin,
  6599. scale: scale,
  6600. delta: delta,
  6601. backwards: backwards
  6602. });
  6603. },
  6604. _onZoomTransitionEnd: function () {
  6605. this._animatingZoom = false;
  6606. L.DomUtil.removeClass(this._mapPane, 'leaflet-zoom-anim');
  6607. this._resetView(this._animateToCenter, this._animateToZoom, true, true);
  6608. if (L.Draggable) {
  6609. L.Draggable._disabled = false;
  6610. }
  6611. }
  6612. });
  6613. /*
  6614. Zoom animation logic for L.TileLayer.
  6615. */
  6616. L.TileLayer.include({
  6617. _animateZoom: function (e) {
  6618. if (!this._animating) {
  6619. this._animating = true;
  6620. this._prepareBgBuffer();
  6621. }
  6622. var bg = this._bgBuffer,
  6623. transform = L.DomUtil.TRANSFORM,
  6624. initialTransform = e.delta ? L.DomUtil.getTranslateString(e.delta) : bg.style[transform],
  6625. scaleStr = L.DomUtil.getScaleString(e.scale, e.origin);
  6626. bg.style[transform] = e.backwards ?
  6627. scaleStr + ' ' + initialTransform :
  6628. initialTransform + ' ' + scaleStr;
  6629. },
  6630. _endZoomAnim: function () {
  6631. var front = this._tileContainer,
  6632. bg = this._bgBuffer;
  6633. front.style.visibility = '';
  6634. front.parentNode.appendChild(front); // Bring to fore
  6635. // force reflow
  6636. L.Util.falseFn(bg.offsetWidth);
  6637. this._animating = false;
  6638. },
  6639. _clearBgBuffer: function () {
  6640. var map = this._map;
  6641. if (map && !map._animatingZoom && !map.touchZoom._zooming) {
  6642. this._bgBuffer.innerHTML = '';
  6643. this._bgBuffer.style[L.DomUtil.TRANSFORM] = '';
  6644. }
  6645. },
  6646. _prepareBgBuffer: function () {
  6647. var front = this._tileContainer,
  6648. bg = this._bgBuffer;
  6649. // if foreground layer doesn't have many tiles but bg layer does,
  6650. // keep the existing bg layer and just zoom it some more
  6651. var bgLoaded = this._getLoadedTilesPercentage(bg),
  6652. frontLoaded = this._getLoadedTilesPercentage(front);
  6653. if (bg && bgLoaded > 0.5 && frontLoaded < 0.5) {
  6654. front.style.visibility = 'hidden';
  6655. this._stopLoadingImages(front);
  6656. return;
  6657. }
  6658. // prepare the buffer to become the front tile pane
  6659. bg.style.visibility = 'hidden';
  6660. bg.style[L.DomUtil.TRANSFORM] = '';
  6661. // switch out the current layer to be the new bg layer (and vice-versa)
  6662. this._tileContainer = bg;
  6663. bg = this._bgBuffer = front;
  6664. this._stopLoadingImages(bg);
  6665. //prevent bg buffer from clearing right after zoom
  6666. clearTimeout(this._clearBgBufferTimer);
  6667. },
  6668. _getLoadedTilesPercentage: function (container) {
  6669. var tiles = container.getElementsByTagName('img'),
  6670. i, len, count = 0;
  6671. for (i = 0, len = tiles.length; i < len; i++) {
  6672. if (tiles[i].complete) {
  6673. count++;
  6674. }
  6675. }
  6676. return count / len;
  6677. },
  6678. // stops loading all tiles in the background layer
  6679. _stopLoadingImages: function (container) {
  6680. var tiles = Array.prototype.slice.call(container.getElementsByTagName('img')),
  6681. i, len, tile;
  6682. for (i = 0, len = tiles.length; i < len; i++) {
  6683. tile = tiles[i];
  6684. if (!tile.complete) {
  6685. tile.onload = L.Util.falseFn;
  6686. tile.onerror = L.Util.falseFn;
  6687. tile.src = L.Util.emptyImageUrl;
  6688. tile.parentNode.removeChild(tile);
  6689. }
  6690. }
  6691. }
  6692. });
  6693. /*
  6694. * Provides L.Map with convenient shortcuts for using browser geolocation features.
  6695. */
  6696. L.Map.include({
  6697. _defaultLocateOptions: {
  6698. watch: false,
  6699. setView: false,
  6700. maxZoom: Infinity,
  6701. timeout: 10000,
  6702. maximumAge: 0,
  6703. enableHighAccuracy: false
  6704. },
  6705. locate: function (/*Object*/ options) {
  6706. options = this._locateOptions = L.extend(this._defaultLocateOptions, options);
  6707. if (!navigator.geolocation) {
  6708. this._handleGeolocationError({
  6709. code: 0,
  6710. message: 'Geolocation not supported.'
  6711. });
  6712. return this;
  6713. }
  6714. var onResponse = L.bind(this._handleGeolocationResponse, this),
  6715. onError = L.bind(this._handleGeolocationError, this);
  6716. if (options.watch) {
  6717. this._locationWatchId =
  6718. navigator.geolocation.watchPosition(onResponse, onError, options);
  6719. } else {
  6720. navigator.geolocation.getCurrentPosition(onResponse, onError, options);
  6721. }
  6722. return this;
  6723. },
  6724. stopLocate: function () {
  6725. if (navigator.geolocation) {
  6726. navigator.geolocation.clearWatch(this._locationWatchId);
  6727. }
  6728. if (this._locateOptions) {
  6729. this._locateOptions.setView = false;
  6730. }
  6731. return this;
  6732. },
  6733. _handleGeolocationError: function (error) {
  6734. var c = error.code,
  6735. message = error.message ||
  6736. (c === 1 ? 'permission denied' :
  6737. (c === 2 ? 'position unavailable' : 'timeout'));
  6738. if (this._locateOptions.setView && !this._loaded) {
  6739. this.fitWorld();
  6740. }
  6741. this.fire('locationerror', {
  6742. code: c,
  6743. message: 'Geolocation error: ' + message + '.'
  6744. });
  6745. },
  6746. _handleGeolocationResponse: function (pos) {
  6747. var lat = pos.coords.latitude,
  6748. lng = pos.coords.longitude,
  6749. latlng = new L.LatLng(lat, lng),
  6750. latAccuracy = 180 * pos.coords.accuracy / 40075017,
  6751. lngAccuracy = latAccuracy / Math.cos(L.LatLng.DEG_TO_RAD * lat),
  6752. bounds = L.latLngBounds(
  6753. [lat - latAccuracy, lng - lngAccuracy],
  6754. [lat + latAccuracy, lng + lngAccuracy]),
  6755. options = this._locateOptions;
  6756. if (options.setView) {
  6757. var zoom = Math.min(this.getBoundsZoom(bounds), options.maxZoom);
  6758. this.setView(latlng, zoom);
  6759. }
  6760. var data = {
  6761. latlng: latlng,
  6762. bounds: bounds
  6763. };
  6764. for (var i in pos.coords) {
  6765. if (typeof pos.coords[i] === 'number') {
  6766. data[i] = pos.coords[i];
  6767. }
  6768. }
  6769. this.fire('locationfound', data);
  6770. }
  6771. });
  6772. }(window, document));