Site icon C1CTech

React Native Working With Google Map

<p>This article aims to give knowledge about how to implement <strong><span style&equals;"color&colon; &num;008000&semi;">Google Maps<&sol;span><&sol;strong> into your react native applications with basic concepts like Rendering a Map with an initial region&comma; Changing map type &comma;Placing a marker &comma;<span style&equals;"color&colon; &num;000000&semi;">Get user&&num;8217&semi;s current Location &comma;Move to specific position with animation etc&period;<&sol;span><&sol;p>&NewLine;<p>Get <strong><span style&equals;"color&colon; &num;0000ff&semi;">GITHUB<&sol;span><&sol;strong> code from <span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;ReactNativeMapExample"><strong>HERE<&sol;strong><&sol;a><&sol;span>&period;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"EXQkgQOe0Qg" title&equals;"React Native Working With Google Map"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;EXQkgQOe0Qg"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;EXQkgQOe0Qg&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"React Native Working With Google Map"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Note<&sol;span><&sol;strong>&colon; In the above video on pressing <span style&equals;"color&colon; &num;008000&semi;"><strong>LOCATE ME<&sol;strong><&sol;span> button shows your current location&period;<&sol;p>&NewLine;<ul>&NewLine;<li>Start a new React Native project&period; If you don’t know how then read my this <a href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;installing-react-native-on-windows&sol;"><strong>tutorial<&sol;strong><&sol;a>&period;<&sol;li>&NewLine;<li>After creating project Open Command Prompt in your system &period;<&sol;li>&NewLine;<li>Now using command prompt locate your project’s  root folder &period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3 style&equals;"color&colon; &num;000080&semi;"><strong>Adding React Native Map to Android<&sol;strong><&sol;h3>&NewLine;<ul>&NewLine;<li>Now  install react-native-maps inside your project  &colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p style&equals;"padding-left&colon; 30px&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>npm install react-native-maps &&num;8211&semi;save<&sol;strong><&sol;span><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Configure Project for Android devices<&sol;strong><&sol;span><&sol;h3>&NewLine;<ul>&NewLine;<li>In your <strong><span style&equals;"color&colon; &num;0000ff&semi;">android&sol;app&sol;build&period;gradle<&sol;span><&sol;strong> add&colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><code>dependencies &lbrace;&NewLine; <span class&equals;"pl-k">&period;&period;<&sol;span>&period;&NewLine; implementation&lpar;project&lpar;<span class&equals;"pl-s"><span class&equals;"pl-pds">'<&sol;span>&colon;react-native-maps<span class&equals;"pl-pds">'<&sol;span><&sol;span>&rpar;&rpar;&lbrace;&NewLine; exclude <span class&equals;"pl-c1">group<&sol;span>&colon; <span class&equals;"pl-s"><span class&equals;"pl-pds">'<&sol;span>com&period;google&period;android&period;gms<span class&equals;"pl-pds">'<&sol;span><&sol;span>&comma; <span class&equals;"pl-c1">module<&sol;span>&colon; <span class&equals;"pl-s"><span class&equals;"pl-pds">'<&sol;span>play-services-base<span class&equals;"pl-pds">'<&sol;span><&sol;span>&NewLine; exclude <span class&equals;"pl-c1">group<&sol;span>&colon; <span class&equals;"pl-s"><span class&equals;"pl-pds">'<&sol;span>com&period;google&period;android&period;gms<span class&equals;"pl-pds">'<&sol;span><&sol;span>&comma; <span class&equals;"pl-c1">module<&sol;span>&colon; <span class&equals;"pl-s"><span class&equals;"pl-pds">'<&sol;span>play-services-maps<span class&equals;"pl-pds">'<&sol;span><&sol;span>&NewLine; &rcub;&NewLine; implementation <span class&equals;"pl-s"><span class&equals;"pl-pds">'<&sol;span>com&period;google&period;android&period;gms&colon;play-services-base&colon;10&period;0&period;1<span class&equals;"pl-pds">'<&sol;span><&sol;span>&NewLine; implementation <span class&equals;"pl-s"><span class&equals;"pl-pds">'<&sol;span>com&period;google&period;android&period;gms&colon;play-services-maps&colon;10&period;0&period;1<span class&equals;"pl-pds">'<&sol;span><&sol;span>&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<ul>&NewLine;<li>In your <strong><span style&equals;"color&colon; &num;0000ff&semi;">android&sol;settings&period;gradle<&sol;span><&sol;strong> add&colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><code>include <span class&equals;"pl-s"><span class&equals;"pl-pds">'<&sol;span>&colon;react-native-maps<span class&equals;"pl-pds">'<&sol;span><&sol;span>&NewLine;project&lpar;<span class&equals;"pl-s"><span class&equals;"pl-pds">'<&sol;span>&colon;react-native-maps<span class&equals;"pl-pds">'<&sol;span><&sol;span>&rpar;<span class&equals;"pl-k">&period;<&sol;span>projectDir <span class&equals;"pl-k">&equals;<&sol;span> <span class&equals;"pl-k">new<&sol;span> <span class&equals;"pl-k">File<&sol;span>&lpar;rootProject<span class&equals;"pl-k">&period;<&sol;span>projectDir&comma; <span class&equals;"pl-s"><span class&equals;"pl-pds">'<&sol;span>&period;&period;&sol;node&lowbar;modules&sol;react-native-maps&sol;lib&sol;android<span class&equals;"pl-pds">'<&sol;span><&sol;span>&rpar;<&sol;code><&sol;pre>&NewLine;<ul>&NewLine;<li>Specify your <strong><span style&equals;"color&colon; &num;0000ff&semi;">Google Maps API Key<&sol;span><&sol;strong>&colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p style&equals;"padding-left&colon; 30px&semi;">Add your API key to your manifest file<strong><span style&equals;"color&colon; &num;0000ff&semi;">&lpar;android&sol;app&sol;src&sol;main&sol;AndroidManifest&period;xml&rpar;<&sol;span><&sol;strong>&colon;<&sol;p>&NewLine;<div class&equals;"highlight highlight-text-xml">&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><code>&lt&semi;<span class&equals;"pl-ent">application<&sol;span>&gt&semi;&NewLine; <span class&equals;"pl-c">&lt&semi;&excl;-- You will only need to add this meta-data tag&comma; but make sure it's a child of application --&gt&semi;<&sol;span>&NewLine; &lt&semi;<span class&equals;"pl-ent">meta-data<&sol;span>&NewLine; <span class&equals;"pl-e">android<&sol;span><span class&equals;"pl-e">&colon;<&sol;span><span class&equals;"pl-e">name<&sol;span>&equals;<span class&equals;"pl-s"><span class&equals;"pl-pds">"<&sol;span>com&period;google&period;android&period;geo&period;API&lowbar;KEY<span class&equals;"pl-pds">"<&sol;span><&sol;span>&NewLine; <span class&equals;"pl-e">android<&sol;span><span class&equals;"pl-e">&colon;<&sol;span><span class&equals;"pl-e">value<&sol;span>&equals;<span class&equals;"pl-s"><span class&equals;"pl-pds">"<&sol;span>Your Google maps API Key Here<span class&equals;"pl-pds">"<&sol;span><&sol;span>&sol;&gt&semi;&NewLine;&lt&semi;&sol;<span class&equals;"pl-ent">application<&sol;span>&gt&semi;<&sol;code><&sol;pre>&NewLine;<ul>&NewLine;<li>Add <span style&equals;"color&colon; &num;0000ff&semi;"><strong>import com&period;airbnb&period;android&period;react&period;maps&period;MapsPackage<&sol;strong><&sol;span>&semi; and <strong><span style&equals;"color&colon; &num;0000ff&semi;">new MapsPackage&lpar;&rpar;<&sol;span><&sol;strong> in your <span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainApplication&period;java <&sol;strong><&sol;span>&colon;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><code>import com&period;airbnb&period;android&period;react&period;maps&period;MapsPackage&semi;&NewLine;&period;&period;&period;&NewLine;&commat;Override&NewLine; protected List&lt&semi;ReactPackage&gt&semi; getPackages&lpar;&rpar; &lbrace;&NewLine; return Arrays&period;&lt&semi;ReactPackage&gt&semi;asList&lpar;&NewLine; new MainReactPackage&lpar;&rpar;&comma;&NewLine; new MapsPackage&lpar;&rpar;&NewLine; &rpar;&semi;&NewLine; &rcub;&NewLine;<&sol;code><&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>MapView<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>This <strong><span style&equals;"color&colon; &num;008000&semi;">MapView<&sol;span><&sol;strong> component is built so that features on the map &lpar;such as Markers&comma; Polygons&comma; etc&period;&rpar; are specified as children of the MapView itself&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"meta import js"><span class&equals;"keyword control js">import<&sol;span> <span class&equals;"variable other module js">MapView<&sol;span> <span class&equals;"keyword control js">from<&sol;span> <span class&equals;"string quoted single js"><span class&equals;"punctuation definition string begin js">&&num;8216&semi;<&sol;span>react-native-maps<span class&equals;"punctuation definition string end js">&&num;8216&semi;<&sol;span><&sol;span><&sol;span><span class&equals;"punctuation terminator statement js">&semi;<&sol;span><&sol;span><&sol;strong><&sol;p>&NewLine;<p>OR<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"storage type js">var<&sol;span> MapView <span class&equals;"keyword operator assignment js">&equals;<&sol;span> <span class&equals;"meta function-call js"><span class&equals;"support function js">require<&sol;span><span class&equals;"meta js"><span class&equals;"punctuation definition begin round js">&lpar;<&sol;span><span class&equals;"string quoted single js"><span class&equals;"punctuation definition string begin js">&&num;8216&semi;<&sol;span>react-native-maps<span class&equals;"punctuation definition string end js">&&num;8216&semi;<&sol;span><&sol;span><span class&equals;"punctuation definition end round js">&rpar;<&sol;span><&sol;span><&sol;span><span class&equals;"punctuation terminator statement js">&semi;<&sol;span><&sol;span><&sol;strong><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Rendering a Map with an initial region<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>In the below code &colon;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">latitude<&sol;span><&sol;strong> &comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>longitude<&sol;strong><&sol;span> &colon; refers to a specific position or point&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">latitudeDelta<&sol;span><&sol;strong> &comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>longitudeDelta<&sol;strong><&sol;span> &colon; refers to the area around the specific position&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>initialRegion<&sol;strong><&sol;span> &colon; sets the map region on initialization&period; Cannot accommodate updates&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong class&equals;"markup--strong markup--li-strong">region<&sol;strong><&sol;span>&hairsp; &colon; &hairsp;able to take location updates from your application and re-render map accordingly&period;<&sol;p>&NewLine;<pre><code>state &equals; &lbrace;&NewLine;&NewLine;focusedLocation&colon; &lbrace;&NewLine;latitude&colon; 37&period;7900352&comma;&NewLine;longitude&colon; -122&period;4013726&comma;&NewLine;latitudeDelta&colon; 0&period;0122&comma;&NewLine;longitudeDelta&colon;&NewLine;Dimensions&period;get&lpar;<strong>"window"<&sol;strong>&rpar;&period;width &sol;&NewLine;Dimensions&period;get&lpar;<strong>"window"<&sol;strong>&rpar;&period;height &ast;&NewLine;0&period;0122&NewLine;&rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;render&lpar;&rpar; &lbrace;&NewLine;&NewLine;<strong>return <&sol;strong>&lpar;&NewLine;&lt&semi;View style&equals;&lbrace;styles&period;container&rcub;&gt&semi;&NewLine;&lt&semi;MapView&NewLine;initialRegion&equals;&lbrace;<strong>this<&sol;strong>&period;state&period;focusedLocation&rcub;&NewLine;style&equals;&lbrace;styles&period;map&rcub;&NewLine;>&NewLine;&lt&semi;&sol;MapView&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;View&gt&semi;&NewLine;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;&NewLine;&NewLine;const styles &equals; StyleSheet&period;create&lpar;&lbrace;&NewLine;container&colon; &lbrace;&NewLine;flex&colon; 1&NewLine;&rcub;&comma;&NewLine;map&colon; &lbrace;&NewLine;width&colon; <strong>"100&percnt;"<&sol;strong>&comma;&NewLine;height&colon; <strong>"100&percnt;"&NewLine;<&sol;strong>&rcub;&NewLine;&rcub;&rpar;&semi;<&sol;code><&sol;pre>&NewLine;<p><strong>Run your project and congratulations if you see a map displaying on your device&period;<&sol;strong><&sol;p>&NewLine;<p><img class&equals;"wp-image-579 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;standard-576x1024&period;png" alt&equals;"" width&equals;"419" height&equals;"745" &sol;><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Changing Map Type<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>You can move to any kind of map by defining <span style&equals;"color&colon; &num;008000&semi;"><strong>mapType<&sol;strong><&sol;span> prop as &colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p>&&num;8211&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">standard<&sol;span><&sol;strong>&colon; standard road map &lpar;default&rpar;<br &sol;>&NewLine;&&num;8211&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">none<&sol;span><&sol;strong>&colon; no map<br &sol;>&NewLine;&&num;8211&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">satellite<&sol;span><&sol;strong>&colon; satellite view<br &sol;>&NewLine;&&num;8211&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">hybrid<&sol;span><&sol;strong>&colon; satellite view with roads and points of interest overlayed<br &sol;>&NewLine;&&num;8211&semi; <strong><span style&equals;"color&colon; &num;008000&semi;">terrain<&sol;span><&sol;strong>&colon; &lpar;Android only&rpar; topographic view<br &sol;>&NewLine;&&num;8211&semi; <span style&equals;"color&colon; &num;008000&semi;"><strong>mutedStandard<&sol;strong><&sol;span>&colon; more subtle&comma; makes markers&sol;lines pop more &lpar;iOS 11&period;0&plus; only&rpar;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-medium wp-image-579" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;standard-169x300&period;png" alt&equals;"" width&equals;"169" height&equals;"300" &sol;>   <img class&equals;"alignnone size-medium wp-image-582" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;satellite-169x300&period;png" alt&equals;"" width&equals;"169" height&equals;"300" &sol;><&sol;p>&NewLine;<p><img class&equals;"alignnone size-medium wp-image-580" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;hybrid-169x300&period;png" alt&equals;"" width&equals;"169" height&equals;"300" &sol;>   <img class&equals;"alignnone size-medium wp-image-581" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;terrain-169x300&period;png" alt&equals;"" width&equals;"169" height&equals;"300" &sol;><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Placing a Marker<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>You can place a marker on the map by using following code&colon;<&sol;p>&NewLine;<pre><code>&lt&semi;MapView&period;Marker coordinate&equals;&lbrace; &lbrace;latitude&colon; 37&period;7900352&comma;longitude&colon; -122&period;4013726&rcub; &rcub;&NewLine;title &equals; "title"&NewLine;description &equals; "description"&NewLine;pinColor &equals; "blue"&NewLine;image &equals; &lbrace;require&lpar;"&period;&sol;assets&sol;custum&lowbar;icon&period;png"&rpar;&rcub; &sol;&gt&semi;<&sol;code><&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>coordinate<&sol;strong><&sol;span> &colon; The coordinate for the marker&period;<&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-584 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;map&lowbar;icon-576x1024&period;png" alt&equals;"" width&equals;"411" height&equals;"731" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">title<&sol;span><&sol;strong> &colon; The title of the marker&period; This is only used if the component has no children that are a &lt&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>Callout<&sol;strong><&sol;span> &sol;&gt&semi;&comma; in which case the default callout behavior will be used&comma; which will show both the <span style&equals;"color&colon; &num;008000&semi;"><strong>title<&sol;strong><&sol;span>  and the <strong><span style&equals;"color&colon; &num;008000&semi;">description<&sol;span><&sol;strong>&comma; if provided&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">description<&sol;span><&sol;strong> &colon;  The description of the marker&period; This is only used if the component has no children that are a &lt&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">Callout<&sol;span><&sol;strong> &sol;&gt&semi;&comma; in which case the default callout behavior will be used&comma; which will show both the <strong><span style&equals;"color&colon; &num;008000&semi;">title<&sol;span><&sol;strong> and the <span style&equals;"color&colon; &num;008000&semi;"><strong>description<&sol;strong><&sol;span>&comma; if provided&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-586 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;marker&lowbar;title&lowbar;decp-576x1024&period;png" alt&equals;"" width&equals;"442" height&equals;"786" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">pinColor<&sol;span><&sol;strong> &colon; If no custom marker view or custom image is provided&comma; the platform default pin will be used&comma; which can be customized by this color&period; Ignored if a custom marker is being used&period;<&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-585 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;marker&lowbar;color-576x1024&period;png" alt&equals;"" width&equals;"430" height&equals;"764" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">image<&sol;span><&sol;strong> &colon; A custom image to be used as the marker&&num;8217&semi;s icon&period; Only local image resources are allowed to be used&period;<&sol;p>&NewLine;<p><img class&equals;"aligncenter wp-image-583 " src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;06&sol;custum&lowbar;image-576x1024&period;png" alt&equals;"" width&equals;"442" height&equals;"785" &sol;><&sol;p>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Get User&&num;8217&semi;s current Location<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;0000ff&semi;">navigator geolocation property<&sol;span><&sol;strong>&lpar;navigator&period;geolocation&rpar; returns a Geolocation object that can be used to locate the user&&num;8217&semi;s position&period;<&sol;p>&NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">getCurrentPosition<&sol;span><&sol;strong>&lpar;&rpar; method is used to return the user&&num;8217&semi;s position&period;<&sol;p>&NewLine;<pre><code>state &equals; &lbrace;&NewLine; &NewLine; focusedLocation&colon; &lbrace;&NewLine; latitude&colon; 37&period;7900352&comma;&NewLine; longitude&colon; -122&period;4013726&comma;&NewLine; latitudeDelta&colon; 0&period;0122&comma;&NewLine; longitudeDelta&colon;&NewLine; Dimensions&period;get&lpar;"window"&rpar;&period;width &sol;&NewLine; Dimensions&period;get&lpar;"window"&rpar;&period;height &ast;&NewLine; 0&period;0122&NewLine; &rcub;&NewLine; &NewLine;&rcub;&NewLine;&NewLine;navigator&period;geolocation&period;getCurrentPosition&lpar;pos &equals;&gt&semi; &lbrace;&NewLine;this&period;setState&lpar;prevState &equals;&gt&semi; &lbrace;&NewLine; return &lbrace;&NewLine; focusedLocation&colon; &lbrace;&NewLine; &period;&period;&period;prevState&period;focusedLocation&comma;&NewLine; latitude&colon; pos&period;coords&period;latitude&comma;&NewLine; longitude&colon; pos&period;coords&period;longitude &rcub;&NewLine; &NewLine; &rcub;&semi;&NewLine; &rcub;&rpar;&semi;&NewLine;&rcub;&comma;&NewLine;err &equals;&gt&semi; &lbrace;&NewLine;console&period;log&lpar;err&rpar;&semi;&NewLine;alert&lpar;"Fetching the Position failed"&rpar;&semi;&NewLine;&rcub;&rpar;<&sol;code><&sol;pre>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Move to specific Position With Animation<&sol;span><&sol;strong><&sol;h3>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">onPress<&sol;span><&sol;strong> &colon; Callback that is called when user taps on the map&period; It returns an object &lpar;with property coordinate and position &rpar;and we can access event data by using nativeEvent object&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">animateToRegion<&sol;span><&sol;strong> &colon; this method is used when we want to animate at specific position with animation&period;We pass region and duration as two arguments&period;<&sol;p>&NewLine;<pre><code>state &equals; &lbrace;&NewLine;&NewLine; focusedLocation&colon; &lbrace;&NewLine; latitude&colon; 37&period;7900352&comma;&NewLine; longitude&colon; -122&period;4013726&comma;&NewLine; latitudeDelta&colon; 0&period;0122&comma;&NewLine; longitudeDelta&colon;&NewLine; Dimensions&period;get&lpar;"window"&rpar;&period;width &sol;&NewLine; Dimensions&period;get&lpar;"window"&rpar;&period;height &ast;&NewLine; 0&period;0122&NewLine; &rcub;&NewLine; &NewLine;&rcub;&NewLine;&lt&semi;MapView&NewLine;initialRegion&equals;&lbrace;this&period;state&period;focusedLocation&rcub;&NewLine;region&equals;&lbrace;&excl;this&period;state&period;locationChosen &quest; this&period;state&period;focusedLocation &colon; null&rcub;&NewLine;style&equals;&lbrace;styles&period;map&rcub;&NewLine;onPress&equals;&lbrace;this&period;pickLocationHandler&rcub;&NewLine;ref&equals;&lbrace;ref &equals;&gt&semi; this&period;map &equals; ref&rcub;&NewLine;>&NewLine;&NewLine;&NewLine;pickLocationHandler &equals; event &equals;&gt&semi; &lbrace;&NewLine; const coords &equals; event&period;nativeEvent&period;coordinate&semi;&NewLine; this&period;map&period;animateToRegion&lpar;&lbrace;&NewLine; &period;&period;&period;this&period;state&period;focusedLocation&comma;&NewLine; latitude&colon; coords&period;latitude&comma;&NewLine; longitude&colon; coords&period;longitude&NewLine; &rcub;&rpar;&semi;&NewLine; this&period;setState&lpar;prevState &equals;&gt&semi; &lbrace;&NewLine; return &lbrace;&NewLine; focusedLocation&colon; &lbrace;&NewLine; &period;&period;&period;prevState&period;focusedLocation&comma;&NewLine; latitude&colon; coords&period;latitude&comma;&NewLine; longitude&colon; coords&period;longitude&NewLine; &rcub;&NewLine; &rcub;&semi;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine;&rcub;&semi;<&sol;code><&sol;pre>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Complete Code&colon;<&sol;span><&sol;strong><&sol;h3>&NewLine;<pre><code>import React&comma; &lbrace; Component &rcub; from "react"&semi;&NewLine;import &lbrace;&NewLine; View&comma;&NewLine; Image&comma;&NewLine; Button&comma;&NewLine; StyleSheet&comma;&NewLine; Text&comma;&NewLine; Dimensions&NewLine;&rcub; from "react-native"&semi;&NewLine;import MapView from "react-native-maps"&semi;&NewLine;&NewLine;export default class App extends Component &lbrace;&NewLine;&NewLine;&NewLine; state &equals; &lbrace;&NewLine;&NewLine; focusedLocation&colon; &lbrace;&NewLine; latitude&colon; 37&period;7900352&comma;&NewLine; longitude&colon; -122&period;4013726&comma;&NewLine; latitudeDelta&colon; 0&period;0122&comma;&NewLine; longitudeDelta&colon;&NewLine; Dimensions&period;get&lpar;"window"&rpar;&period;width &sol;&NewLine; Dimensions&period;get&lpar;"window"&rpar;&period;height &ast;&NewLine; 0&period;0122&NewLine; &rcub;&comma;&NewLine; locationChosen&colon; false&NewLine; &rcub;&NewLine;&NewLine; pickLocationHandler &equals; event &equals;&gt&semi; &lbrace;&NewLine; const coords &equals; event&period;nativeEvent&period;coordinate&semi;&NewLine; this&period;map&period;animateToRegion&lpar;&lbrace;&NewLine; &period;&period;&period;this&period;state&period;focusedLocation&comma;&NewLine; latitude&colon; coords&period;latitude&comma;&NewLine; longitude&colon; coords&period;longitude&NewLine; &rcub;&rpar;&semi;&NewLine; this&period;setState&lpar;prevState &equals;&gt&semi; &lbrace;&NewLine; return &lbrace;&NewLine; focusedLocation&colon; &lbrace;&NewLine; &period;&period;&period;prevState&period;focusedLocation&comma;&NewLine; latitude&colon; coords&period;latitude&comma;&NewLine; longitude&colon; coords&period;longitude&NewLine; &rcub;&comma;&NewLine; locationChosen&colon; true&NewLine; &rcub;&semi;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine; &rcub;&semi;&NewLine;&NewLine; getLocationHandler &equals; &lpar;&rpar; &equals;&gt&semi; &lbrace;&NewLine; navigator&period;geolocation&period;getCurrentPosition&lpar;pos &equals;&gt&semi; &lbrace;&NewLine; const coordsEvent &equals; &lbrace;&NewLine; nativeEvent&colon; &lbrace;&NewLine; coordinate&colon; &lbrace;&NewLine; latitude&colon; pos&period;coords&period;latitude&comma;&NewLine; longitude&colon; pos&period;coords&period;longitude&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&semi;&NewLine; this&period;pickLocationHandler&lpar;coordsEvent&rpar;&semi;&NewLine; &rcub;&comma;&NewLine; err &equals;&gt&semi; &lbrace;&NewLine; console&period;log&lpar;err&rpar;&semi;&NewLine; alert&lpar;"Fetching the Position failed&comma; please pick one manually&excl;"&rpar;&semi;&NewLine; &rcub;&rpar;&NewLine; &rcub;&NewLine;&NewLine; render&lpar;&rpar; &lbrace;&NewLine; let marker &equals; null&semi;&NewLine;&NewLine; if &lpar;this&period;state&period;locationChosen&rpar; &lbrace;&NewLine; marker &equals; &lt&semi;MapView&period;Marker coordinate&equals;&lbrace;this&period;state&period;focusedLocation&rcub; &sol;&gt&semi;&semi;&NewLine; &rcub;&NewLine;&NewLine; return &lpar;&NewLine; &lt&semi;View style&equals;&lbrace;styles&period;container&rcub;&gt&semi;&NewLine; &lt&semi;MapView&NewLine; initialRegion&equals;&lbrace;this&period;state&period;focusedLocation&rcub;&NewLine; region&equals;&lbrace;&excl;this&period;state&period;locationChosen &quest; this&period;state&period;focusedLocation &colon; null&rcub;&NewLine; style&equals;&lbrace;styles&period;map&rcub;&NewLine; onPress&equals;&lbrace;this&period;pickLocationHandler&rcub;&NewLine; ref&equals;&lbrace;ref &equals;&gt&semi; this&period;map &equals; ref&rcub;&NewLine; &gt&semi;&NewLine; &lbrace;marker&rcub;&NewLine; &lt&semi;&sol;MapView&gt&semi;&NewLine; &lt&semi;View style&equals;&lbrace;styles&period;button&rcub;&gt&semi;&NewLine; &lt&semi;Button title&equals;"Locate Me" onPress&equals;&lbrace;this&period;getLocationHandler&rcub; &sol;&gt&semi;&NewLine; &lt&semi;&sol;View&gt&semi;&NewLine; &lt&semi;&sol;View&gt&semi;&NewLine; &rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;const styles &equals; StyleSheet&period;create&lpar;&lbrace;&NewLine; container&colon; &lbrace;&NewLine; width&colon; "100&percnt;"&comma;&NewLine; alignItems&colon; "center"&NewLine; &rcub;&comma;&NewLine; map&colon; &lbrace;&NewLine; width&colon; "100&percnt;"&comma;&NewLine; height&colon; 600&NewLine; &rcub;&comma;&NewLine; button&colon; &lbrace;&NewLine; margin&colon; 8&NewLine; &rcub;&NewLine;&rcub;&rpar;&semi;<&sol;code><&sol;pre>&NewLine;&NewLine;

Exit mobile version