@mapbox/mapbox-sdk - npm
文章推薦指數: 80 %
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
延伸文章資訊
- 1Installation | Maps SDK | Android | Mapbox
Mapbox provides the Maps SDK via Maven. To add the Mapbox Maps SDK as a dependency, you will need...
- 2Mobile Maps SDK v10 - Mapbox
The Mapbox Mobile SDKs power the most compelling map experiences available on iOS and Android. Ve...
- 3@mapbox/mapbox-sdk - npm
JS SDK for accessing Mapbox APIs. Latest version: 0.13.3, last published: 2 months ago. Start usi...
- 4Mapbox: Maps, geocoding, and navigation APIs & SDKs
Integrate custom live maps, location search, and turn-by-turn navigation into any mobile or web a...
- 5Mapbox Maps SDK for iOS
The Mapbox Maps SDK for iOS is a library for embedding highly customized maps within iOS applicat...