@mapbox/mapbox-sdk - npm

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

JS SDK for accessing Mapbox APIs. Latest version: 0.13.3, last published: 2 months ago. Start using @mapbox/mapbox-sdk in your project by ... @mapbox/mapbox-sdk0.13.3 • Public • Published2monthsagoReadmeExploreBETA8Dependencies52Dependents22Versions @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. KeywordsmapboxsdkapimapstyletilesetdatasetsearchnavigationInstallnpmi@mapbox/mapbox-sdkRepositoryGitgithub.com/mapbox/mapbox-sdk-jsHomepagegithub.com/mapbox/mapbox-sdk-js#readmeDownloadsWeeklyDownloads134,687Version0.13.3LicenseBSD-2-ClauseUnpackedSize533kBTotalFiles68Lastpublish2monthsagoCollaboratorsTryonRunKitReportmalware



請為這篇文章評分?