Implementing Mapbox On React Native - GeekyAnts Tech Blog
文章推薦指數: 80 %
An introduction on implementing Mapbox, a popular location data platform, on your React Native application.
ImplementingMapboxOnReactNativeAnintroductiononimplementingMapbox,apopularlocationdataplatform,onyourReactNativeapplicationDeekshaMehta·Jul15,2021·7minreadListentothisarticleYourbrowserdoesnotsupporttheaudioelement.SPEED1XIntroduction
Inthisarticle,wearegoingtolearnthebasicsofusingMapboxasatoolforintegratingmapsinReactNative.WhileGoogleMapsisprobablythemostfamousmappingsystem,therearevariousotherworthycontendersinthemarket.WhatmakesMapboxoneofthemostprominentplayersinthefieldisthefactthatitisalocationdataplatformthatpowersmapsandlocationservicesusedinmanypopularappsmakingitoneofthebestalternativesforGoogleMaps.Inthistutorial,wearegoingdelvestep-by-stepintocreatingcustommarkers,annotations,polylinesandpolygonsusingMapbox.
Let'sGetStarted
InordertouseMapbox,makesuretosignupforanaccounthere.Clickonthe"Startmappingforfree"buttonwhichwillnavigateyoutothesign-uppage.Butifyoualreadyhaveanaccount,youcanalternativelyloginwithyourusernameoremail.
Aftersigningin,youcanseetheaccesstokeninthedashboardscreenwhichyouwillneedtogoahead.Refertothepicturebelow:
ThenextstepistogoforwardistocreateaReactNativeprojectusingthestepsgivenhere.
AddingMapboxMapsSDKforReactNative
Now,wearegoingtosetupMapboxinourReactforwhichweneeda@react-native-mapbox-gl/mapsnpmpackagewhichwecaninstallusingthecommandgivenbelow:
yarnadd@react-native-mapbox-gl/maps
Alternatively,youcanalsousethefollowingcommand:
npminstall@react-native-mapbox-gl/maps--save
ForiOS,weneedtosetuptheMapboxmanuallyaspartoftheinstallationprocess.AddthesefollowinglinesinyouriOSpodfiletoachievethis:
pre_installdo|installer|
$RNMBGL.pre_install(installer)
...otherpreinstallhooks
end
post_installdo|installer|
$RNMBGL.post_install(installer)
...otherpostinstallhooks
end
pod'react-native-mapbox-gl',:path=>'../node_modules/@react-native-mapbox-gl/maps'
Don'tforgettorunpodinstallintheiOSdirectoryafterinstallationusingthefollowingcode:
cdios&&podinstall&&cd..
Note:Formoreinformation,refertotheguidehere.
Asweproceedtothenextstep,wewillhavetochangethecontentwithintheapp.jstoremovethedefaultcode.Usethefollowingcodetodothis:
importReact,{useState}from"react";
import{View,StyleSheet,Text,Image}from"react-native";
importMapboxGLfrom"@react-native-mapbox-gl/maps";
Afterthis,wewillhavetosettheaccesstokenonMapboxGLastheMapboxdashboardalreadyhasthedefaulttoken.CopyandpastethecodegivenbelowinplaceoftheYOUR_MAPBOX_ACCESS_TOKEN:
MapboxGL.setAccessToken("YOUR_MAPBOX_ACCESS_TOKEN");
Next,wearegoingtocreateApp.jscomponentusingfunctionalcomponentsasshownbelow:
constApp=()=>{
}
exportdefaultApp;
AddingMapView,CameraandPointAnnotation
MapViewprovidesanembeddablemapinterfacewhichyoucanusetodisplaymapinformationandtomanipulatethemapcontentsfromyourapplication.Youcanexecutethisbycenteringthemaponagivencoordinateandspecifyingthesizeoftheareayouwanttodisplayaswellasthestyleofthefeaturesofthemaptofityourapplication'susecase.
Meanwhile,Cameraisthemap'sfieldofviewinmapsthatuseMapboxGL.Thecamera'sviewportisdeterminedbyseveralfactorslikeCenterwhichcoordinatesthecentreofthemapandZoomlevelwhichisanumberusedtodeterminehowclosetheviewportistothesurfaceofthemaps.Fordetailedinformation,referhere.
PointAnnotationisusedtorenderthepointonamapusingcertainprops.Oneoftheprops,namelythecoordinateprop(const[coordinates]=useState([longitude,latitude]);),showsuswherethispointwouldbelocatedonthemap.Usethefollowingcodetogoahead:
constApp=()=>{
const[coordinates]=useState([78.9629,20.5937]);
return(
延伸文章資訊
- 1React Native: A quick comparison between different map ...
Mapbox Maps SDK for RN, “unofficial React Native library for building maps with the Mapbox Maps S...
- 2A 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...
- 3Implementing Mapbox On React Native - GeekyAnts Tech Blog
An introduction on implementing Mapbox, a popular location data platform, on your React Native ap...
- 4React Native MapboxGL - How to display offline pack on ...
createPack({ name: 'LionRock', styleURL: "mapbox://styles/mapbox/streets-v11", minZoom: 14, maxZo...
- 5Maps SDK for React Native | Help | Mapbox
The Maps SDK for React Native is a community-managed React Native library for integrating Mapbox ...