leaflet-src.js 198 KB

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