mapbox/mapbox-sdk-js: A JavaScript client to ... - GitHub

文章推薦指數: 80 %
投票人數:10人

A JS SDK for working with Mapbox APIs. Works in Node, the browser, and React Native. As of 6/11/18, the codebase has been rewritten and a new npm ... Skiptocontent {{message}} mapbox / mapbox-sdk-js Public Notifications Fork 172 Star 553 AJavaScriptclienttoMapboxservices,supportingNode,browsers,andReactNative License Viewlicense 553 stars 172 forks Star Notifications Code Issues 39 Pullrequests 7 Projects 0 Wiki Security Insights More Code Issues Pullrequests Projects Wiki Security Insights Thiscommitdoesnotbelongtoanybranchonthisrepository,andmaybelongtoaforkoutsideoftherepository. main Branches Tags Couldnotloadbranches Nothingtoshow {{refName}} default Couldnotloadtags Nothingtoshow {{refName}} default 20 branches 47 tags Code Latestcommit   Gitstats 516 commits Files Permalink Failedtoloadlatestcommitinformation. Type Name Latestcommitmessage Committime conf AddOptimizationAPI(#257) Jul10,2018 docs Add'ip'asvalidvalueforgeocodingproximityparameter Mar3,2022 lib CorrectlyhandlearrayscontainingzerovaluesinappendQueryObject(#… Apr22,2020 services Add'ip'asvalidvalueforgeocodingproximityparameter Mar3,2022 test Updategotto10.7.0(#422) Jun23,2021 .eslintignore Buildpre-bundledfilesforunpkg.com(#233) Jun11,2018 .eslintrc.json addeqeqeslintrule Jun11,2018 .gitignore Prepare0.2.0 Jul10,2018 .prettierignore Buildpre-bundledfilesforunpkg.com(#233) Jun11,2018 .travis.yml tryandbuildbundleaspartofautomatedtestsuittocatchanybuil… Jun29,2020 CHANGELOG.md Add'ip'asvalidvalueforgeocodingproximityparameter Mar3,2022 CODE_OF_CONDUCT.md Initialrevitalizationcommit Jun11,2018 LICENSE Initialrevitalizationcommit Jun11,2018 README.md AddTravisbadge(#403) Nov9,2020 bundle.js Isochroneapi(#292) Nov13,2019 index.js Initialrevitalizationcommit Jun11,2018 package-lock.json Updatepackage-lock.json Mar3,2022 package.json Bumpversioninpackage.json Mar3,2022 rollup.config.js Buildpre-bundledfilesforunpkg.com(#233) Jun11,2018 Viewcode @mapbox/mapbox-sdk Tableofcontents Installation Usage Creatingclients Creatingandsendingrequests Overviewofrequests,responses,anderrors MapiRequest MapiResponse MapiError Services Pre-bundledfilesonunpkg.com Development README.md @mapbox/mapbox-sdk AJSSDKforworkingwithMapboxAPIs. WorksinNode,thebrowser,andReactNative. Asof6/11/18,thecodebasehasbeenrewrittenandanewnpmpackagereleased. Themapboxpackageisdeprecatedinfavorofthenew@mapbox/mapbox-sdkpackage. Pleasereadthedocumentationandopenissueswithquestionsorproblems. Tableofcontents Installation Usage Creatingclients Creatingandsendingrequests Overviewofrequests,responses,anderrors MapiRequest MapiResponse MapiError Services Pre-bundledfilesonunpkg.com Development Installation npminstall@mapbox/mapbox-sdk Ifyouaresupportingolderbrowsers,youwillneedaPromisepolyfill. es6-promiseisagoodone,ifyou'reuncertain. Thedocumentationbelowassumesyou'reusingaJSmodulesystem. Ifyouaren't,read"Pre-bundledfilesonunpkg.com". Usage Thereare3basicstepstogettinganAPIresponse: Createaclient. Createarequest. Sendtherequest. Creatingclients Tocreateaserviceclient,importtheservice'sfactoryfunctionfrom'@mapbox/mapbox-sdk/services/{service}'andprovideitwithyouraccesstoken. Theserviceclientexposesmethodsthatcreaterequests. constmbxStyles=require('@mapbox/mapbox-sdk/services/styles'); conststylesService=mbxStyles({accessToken:MY_ACCESS_TOKEN}); //stylesServiceexposeslistStyles(),createStyle(),getStyle(),etc. Youcanalsoshareoneconfigurationbetweenmultipleservices. Todothat,initializeabaseclientandthenpassthatintoservicefactoryfunctions. constmbxClient=require('@mapbox/mapbox-sdk'); constmbxStyles=require('@mapbox/mapbox-sdk/services/styles'); constmbxTilesets=require('@mapbox/mapbox-sdk/services/tilesets'); constbaseClient=mbxClient({accessToken:MY_ACCESS_TOKEN}); conststylesService=mbxStyles(baseClient); consttilesetsService=mbxTilesets(baseClient); Creatingandsendingrequests Tocreatearequest,invokeamethodonaserviceclient. Onceyou'vecreatedarequest,sendtherequestwithitssendmethod. ItwillreturnaPromisethatresolveswithaMapiResponse. constmbxClient=require('@mapbox/mapbox-sdk'); constmbxStyles=require('@mapbox/mapbox-sdk/services/styles'); constmbxTilesets=require('@mapbox/mapbox-sdk/services/tilesets'); constbaseClient=mbxClient({accessToken:MY_ACCESS_TOKEN}); conststylesService=mbxStyles(baseClient); consttilesetsService=mbxTilesets(baseClient); //Createastyle. stylesService.createStyle({..}) .send() .then(response=>{..},error=>{..}); //Listtilesets. tilesetsService.listTilesets() .send() .then(response=>{..},error=>{..}) Overviewofrequests,responses,anderrors Formoredetails,pleasereadthefullclassesdocumentation. MapiRequest ServicemethodsreturnMapiRequestobjects. Typically,you'llcreateaMapiRequestthensendit. sendreturnsaPromisethatresolveswithaMapiResponseorrejectswithaMapiError. MapiRequestsalsoexposeotherpropertiesandmethodsthatyoumightusefromtimetotime. Forexample: MapiRequest#abortabortstherequest. MapiRequest#eachPageexecutesacallbackforeachpageofapaginatedAPIresponse. MapiRequest.emitterexposesaneventemitterthatfireseventslikedownloadProgressanduploadProgress. Formoredetails,pleasereadthefullMapiRequestdocumentation. //Createarequestandsendit. stylesService.createStyle({..}) .send() .then(response=>{..},error=>{..}); //Abortarequest. constreq=tilesetsService.listTilesets(); req.send().then(response=>{..},error=>{ //Becausetherequestisaborted,anerrorwillbethrownthatwecan //catchandhandle. }); req.abort(); //Paginatethrougharesponse. tilesetsService.listTilesets().eachPage((error,response,next)=>{ //Dosomethingwiththepage,thencallnext()tosendtherequest //forthenextpage. //Youcancheckwhethertherewillbeanextpageusing //MapiResponse#hasNextPage,ifyouwanttodosomething //differentonthelastpage. if(!response.hasNextPage()){..} }); //ListenforuploadProgressevents. constreq=stylesService.createStyleIcon({..}); req.on('uploadProgress',event=>{ //Dosomethingwiththeprogresseventinformation. }); req.send().then(response=>{..},error=>{..}); MapiResponse WhenyousendaMapiRequest,thereturnedPromiseresolveswithaMapiResponse. Typically,you'lluseMapiResponse.bodytoaccesstheparsedAPIresponse. MapiResponsesalsoexposeotherpropertiesandmethods. Forexample: MapiResponse#hasNextPageindicatesifthereisanotherpageofresults. Ifthereisanotherpage,MapiResponse#nextPagecreatesaMapiRequestthatyoucansendtogetthatnextpage. MapiResponse.headersexposestheparsedHTTPheadersfromtheAPIresponse. Formoredetails,pleasereadthefullMapiResponsedocumentation. //Readaresponsebody. stylesService.getStyle({..}) .send() .then(resp=>{ conststyle=resp.body; //Dosomethingwiththestyle. },err=>{..}); //Getthenextpageofresults. tilesetsService.listTilesets() .send() .then(resp=>{ if(resp.hasNextPage()){ constnextPageReq=resp.nextPage(); nextPageReq.send().then(..); } },err=>{..}); //Checktheheaders. tilesetsService.listTilesets() .send() .then(resp=>{ console.log(resp.headers); },err=>{..}); MapiError IftheserverrespondstoyourMapiRequestwithanerror,orifyouaborttherequest,thePromisereturnedbysendwillrejectwithaMapiError. MapiErrorsexposetheinformationyou'llneedtohandleandrespondtotheerror. Forexample: MapiError.typeexposesthetypeoferror,soyou'llknowifitwasanHTTPerrorfromtheserverortherequestwasaborted. MapiError.statusCodeexposesthestatuscodeofHTTPerrors. MapiError.bodyexposesthebodyoftheHTTPresponse,parsedasJSONifpossible. MapiError.messagetellsyouwhatwentwrong. Formoredetails,pleasereadthefullMapiErrordocumentation. //Checktheerror. stylesService.getStyle({..}) .send() .then(response=>{..},error=>{ if(err.type==='RequestAbortedError'){ return; } console.error(error.message); }); Services Pleasereadthefulldocumentationforservices. Pre-bundledfilesonunpkg.com Ifyouaren'tusingaJSmodulesystem,youcanusea ThesefilesareaUMDbuildofthepackage,exposingaglobalmapboxSdkfunctionthatcreatesaclient,initializesalltheservices,andattachesthoseservicestotheclient. Here'showyoumightuseit. Development Pleaseread./docs/development.md. About AJavaScriptclienttoMapboxservices,supportingNode,browsers,andReactNative Resources Readme License Viewlicense Codeofconduct Codeofconduct Stars 553 stars Watchers 102 watching Forks 172 forks Releases 1 v0.12.1 Latest Feb5,2021 Packages0 Nopackagespublished Usedby7.9k +7,898 Contributors54 +43contributors Languages JavaScript 99.6% HTML 0.4% Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.



請為這篇文章評分?