Ultimate Guide to Integrating React Native with MapboxGL
文章推薦指數: 80 %
Ultimate Guide to Integrating React Native with MapboxGL- A Google Map Alternative. Mapbox comes bundled with a lot of services.
WriteapostSIGNUPLOGINFindDevelopers&MentorsCommunityPostBlogSIGNUPLOGINDanielAmahFollowSeniorSoftwareFullStackEngineerUltimateGuidetoIntegratingReactNativewithMapboxGL-AGoogleMapAlternativePublishedSep04,2020LastupdatedSep09,2020Mapboxcomesbundledwithalotofservices.Theseservicesincludes:
Directions
Matrix
Geocoding
Optimization
MapboxisoneofthegreatalternativestoGooglemapsoutthere.MapboxwasbuiltontheOpenStreetMapAPIwithamazingcoolfeatures.IfyouarelookingforacheapmapserviceasidefromGoogle,takeagoodlookatMapbox.Togetstarted,visitMapboxOfficialWebsite
Tocreateanaccount,clickonstartmappingforfree.Thiswouldtakeyoutosignuppagetocreateanaccount.
Ifyoualreadyhaveanaccount,youcouldsigninwithyourlogindetails(usernameoremailandpassword)
Onsigningin,yougettoseeyourbeautifuldashboardwithyourdefaultpublictokenasshownbelow:
Mapboxoffersafreeplanwhichisreasonableasfarasyoudon’texceedthelimitprovided.Thelimitscanbeseenontherighthandsideofthedashboard.ThedefaultpublictokenwouldbeusedtomakerequeststoMapboxAPI.
Next,Weneedtocreateourreactnativeapp.Tocreateareactnativeapp,visitthislinkfordirectionsonhowtosetupyourmachineforReactNative.VisitReactNativeOfficialWebsite
TocreateanewReactnativeapp,Runthisonyourterminal
npxreact-nativeinitReactNativeMapbox
Changedirectory(cd)intotheReactNativeMapboxfolder.Todothat,runthecommandbelowonyourterminal.
cdReactNativeMapbox
Runtheapplicationonyoursimulator.Forthisarticle,Iwouldberunningoniossimulator.(IwouldnotbefocusingonsettinguponforAndroidinthisarticle).Torunthereactnativeapponios,Runthecommandbelowonyourterminal
yarnios
ThiscommandwouldopentheapponthedefaultiOSsimulator.Asshownbelow
Next,wearegoingtosetupMapboxonourReactNativeapplication.TodothatwewouldbeinstallingtheMapboxnpmpackage.Thenpmpackageisfoundhere:MapboxReactNativepackageforMaps.
RunthefollowingcommandtoinstallMapboxontheReactNativeapp
yarnadd@react-native-mapbox-gl/maps
Aspartoftheinstallationprocess,wewouldneedtoclickoniOSsetupontherepositoryforanextrasetup.Asshownintheimagebelow:
Next,AddthefollowinglinetoyourPodfileasshownintheiOSsectionoftheinstallationguide.
pod'react-native-mapbox-gl',:path=>'../node_modules/@react-native-mapbox-gl/maps'
Asshownintheimagebelow:
Next,cdtoiosfolderandrunpodinstallonyourterminalasshownbelow.
(Ifyougettheerrorpodnotfound,runthecommandgeminstallcocoapodstoinstallcocoapodsandgetaccesstothepodcommand)
cdios
podinstall
Next,cdbacktoyourrootdirectoryusingthecommand.
cd..
Next,wewouldgotoApp.jsfileandclearthedefaultcontentsthatcomeswiththereactnativeinitializationprocess.Weshouldhaveblackscreen.
Next,wewouldbeimportingourlibrariestocreateourmap.Asshownbelow
importReact,{useEffect,useRef}from"react";
import{StyleSheet,View,Image,Platform}from"react-native";
importMapboxGLfrom"@react-native-mapbox-gl/maps";
Next,let'ssettheaccesstokenonMapboxGL.TheMapboxtokenisourdefaultpublictokenfromourmapboxdashboard.Thecodeisshownbelow:
MapboxGL.setAccessToken("MAPBOX_TOKEN_GOES_HERE");
Next,wewouldbecreatingtheAppcomponent.Asshownbelow
exportdefaultApp=()=>{
}
Next,weneedtoaddthecomponentelementsasshownbelow.
return(
延伸文章資訊
- 1@react-native-mapbox-gl/maps - npm
A Mapbox GL react native module for creating custom maps. Latest version: 8.6.0-beta.0, last publ...
- 2Is there a way to use Mapbox GL JS for Free? - Stack Overflow
- 3A Mapbox react native module for creating custom maps
Mapbox Maps SDK for React Native. An unofficial React Native library for building maps with the M...
- 4In-App Mapbox Navigation in React Native - Better Programming
Setup a React Native project 3. Install Mapbox Navigation SDK for iOS 4. Create the native module...
- 5Implementing Mapbox On React Native - GeekyAnts Tech Blog