Site icon C1CTech

Android Get Current Location Using Fused Location Provider

<p>In this article&comma; we will talk about what a fused location provider is and how to use it to get the current location using a sample Android application&period; We need not explicitly choose either <a href&equals;"https&colon;&sol;&sol;www&period;seeworldgps&period;com&sol;">GPS provider<&sol;a> or Network location provider&comma; as the &OpenCurlyDoubleQuote;<span style&equals;"color&colon; &num;008000&semi;"><strong>Fused Location Provider<&sol;strong><&sol;span>” automatically chooses the underlying technology and gives the best location as per the need&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Fused Location Provider<&sol;span><&sol;strong><&sol;h3>&NewLine;<ul>&NewLine;<li>The fused location provider is one of the location APIs in Google Play services that intelligently combines different signals to provide the location information that your app needs&period;<&sol;li>&NewLine;<li>It manages the underlying location technologies&comma; such as GPS provider and Wi-Fi&comma; and provides a simple API so that you can specify requirements at a high level&comma; like high accuracy or low power&period;<&sol;li>&NewLine;<li>It also optimizes the device&&num;8217&semi;s use of battery power&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">FusedLocationProviderClient<&sol;span><&sol;strong><&sol;h3>&NewLine;<p class&equals;"p1">FusedLocationProviderClient is the main entry point for interacting with the fused location provider&period; With this&comma; your app no longer needs to manually manage connections to Google Play Services through <strong><span style&equals;"color&colon; &num;008000&semi;">GoogleApiClient<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p class&equals;"p1">Advantages with FusedLocationProviderClient &colon;<&sol;p>&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">It takes the complete connection logic under the hood&period; The user no longer needs to initialize GoogleApiClient nor does he need to manage the connection logic&period;<&sol;li>&NewLine;<li class&equals;"li1">It returns the result as a Task object which is easy to manage and share&period;<&sol;li>&NewLine;<li class&equals;"li1">User need not wait until the connection is established to request for a Location&period; When requesting for the current location the API call automatically waits until the connection is established thereby minimizing the chances of an IllegalStateException&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Note<&sol;strong><&sol;span> &colon; <strong>It’s recommended to use Google Play services version 11&period;6&period;0 or higher&comma; which includes bug fixes for this class&period;<&sol;strong><&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span style&equals;"color&colon; &num;000080&semi;">getLastLocation&lpar;&rpar;<&sol;span> <&sol;strong><&sol;span>&colon; <span style&equals;"color&colon; &num;000000&semi;">getLastlocation&lpar;&rpar;<&sol;span> is one of the method provided by <b><span style&equals;"color&colon; &num;008000&semi;">FusedLocationProviderClient<&sol;span> c<&sol;b>lass which returns the best most recent location currently available&period;The precision of the location returned by this call is determined by the permission setting you put in your app manifest&period;<&sol;p>&NewLine;<p>The getLastLocation&lpar;&rpar; method returns a <strong><span style&equals;"color&colon; &num;008000&semi;">Task<&sol;span><&sol;strong> that you can use to get a Location object with the latitude and longitude coordinates of a geographic location&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;000080&semi;">onMapready&lpar;&rpar;<&sol;span>&colon; <&sol;span><&sol;strong><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;000000&semi;">This method is c<&sol;span><&sol;span><span style&equals;"color&colon; &num;000000&semi;">alled when the map is ready to be used&period;<&sol;span><&sol;p>&NewLine;<p class&equals;"p1"><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;000080&semi;">OnMapReadyCallback<&sol;span>&colon; <&sol;span><&sol;strong>Callback interface for when the map is ready to be used&period;<&sol;p>&NewLine;<p>Once an instance of this interface is set on a <strong><span style&equals;"color&colon; &num;008000&semi;">MapFragment<&sol;span><&sol;strong> or <strong><span style&equals;"color&colon; &num;008000&semi;">MapView<&sol;span><&sol;strong> object&comma; the <strong><span style&equals;"color&colon; &num;008000&semi;">onMapReady&lpar;GoogleMap&rpar;<&sol;span><&sol;strong> method is triggered when the map is ready to be used and provides a non-null instance of <strong><span style&equals;"color&colon; &num;008000&semi;">GoogleMap<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>Let&&num;8217&semi;s create an example that shows how to get the current device location using <strong>FusedLocationProviderClient<&sol;strong>&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Android Project<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>1 &period; Create a new project <span style&equals;"color&colon; &num;008000&semi;"><strong>GetCurrentLocationOnMap<&sol;strong><&sol;span> in Android Studio from <span style&equals;"color&colon; &num;008000&semi;"><strong>File &Implies; New Project<&sol;strong><&sol;span> and fill the project details&period;<&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Getting the Google Maps API key<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>To integrate google map in your application firstly you have to generate the API key in <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;console&period;developers&period;google&period;com&sol;">Google API Console<&sol;a><&sol;strong><&sol;span> &period;<&sol;p>&NewLine;<p>2 &period; Go to  <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Google API Console<&sol;strong><&sol;span> and generate a new API key corresponding to your <strong><span style&equals;"color&colon; &num;008000&semi;">SHA1<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;008000&semi;">application package name<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>3 &period; Open <strong><span style&equals;"color&colon; &num;008000&semi;">AndroidManifest&period;xml<&sol;span><&sol;strong> and add the generated API key to your project above the   &lt&semi;&sol;application &gt&semi; tag as shown below &colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>AndroidManifest&period;xml<&sol;strong> <&sol;span><&sol;p>&NewLine;<pre>&lt&semi;application&NewLine; android&colon;allowBackup&equals;"true"&NewLine; android&colon;icon&equals;"&commat;mipmap&sol;ic&lowbar;launcher"&NewLine; android&colon;label&equals;"&commat;string&sol;app&lowbar;name"&NewLine; android&colon;roundIcon&equals;"&commat;mipmap&sol;ic&lowbar;launcher&lowbar;round"&NewLine; android&colon;supportsRtl&equals;"true"&NewLine; android&colon;theme&equals;"&commat;style&sol;AppTheme"&gt&semi;&NewLine; &lt&semi;activity android&colon;name&equals;"&period;MainActivity"&gt&semi;&NewLine; &lt&semi;intent-filter&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"android&period;intent&period;action&period;MAIN" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;category android&colon;name&equals;"android&period;intent&period;category&period;LAUNCHER" &sol;&gt&semi;&NewLine; &lt&semi;&sol;intent-filter&gt&semi;&NewLine; &lt&semi;&sol;activity&gt&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;"> &lt&semi;meta-data<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> android&colon;name&equals;"com&period;google&period;android&period;geo&period;API&lowbar;KEY"<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> android&colon;value&equals;"YOUR API KEY"&sol;&gt&semi;<&sol;span><&sol;strong>&NewLine;&NewLine;&lt&semi;&sol;application&gt&semi;<&sol;pre>&NewLine;<p>4 &period; Open <strong><span style&equals;"color&colon; &num;008000&semi;">build&period;gradle<&sol;span><&sol;strong> and add the following dependencies as shown below &colon;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">build&period;gradle<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>dependencies &lbrace;&NewLine; implementation fileTree&lpar;dir&colon; 'libs'&comma; include&colon; &lbrack;'&ast;&period;jar'&rsqb;&rpar;&NewLine; &NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;"> implementation 'com&period;google&period;android&period;gms&colon;play-services-maps&colon;17&period;0&period;0'<&sol;span><&sol;strong>&NewLine; &NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> implementation 'com&period;google&period;android&period;gms&colon;play-services-location&colon;17&period;0&period;0'<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;pre>&NewLine;<h4 id&equals;"permissions"><strong><span style&equals;"color&colon; &num;000080&semi;">Specify app permissions<&sol;span><&sol;strong><&sol;h4>&NewLine;<div class&equals;"paragraph">&NewLine;<p>5 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span> and add the following permissions to your application to use location services&period;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"ulist">&NewLine;<ul>&NewLine;<li>INTERNET<&sol;li>&NewLine;<li>ACCESS&lowbar;FINE&lowbar;LOCATION<&sol;li>&NewLine;<li>ACCESS&lowbar;COARSE&lowbar;LOCATION<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span><&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;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;manifest xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; package&equals;"com&period;example&period;getcurrentlocationonmap"&gt&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;"> &lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;ACCESS&lowbar;FINE&lowbar;LOCATION" &sol;&gt&semi;<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;ACCESS&lowbar;COARSE&lowbar;LOCATION" &sol;&gt&semi;<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;INTERNET" &sol;&gt&semi;<&sol;span><&sol;strong>&NewLine;&NewLine; &lt&semi;application&NewLine; android&colon;allowBackup&equals;"true"&NewLine; android&colon;icon&equals;"&commat;mipmap&sol;ic&lowbar;launcher"&NewLine; android&colon;label&equals;"&commat;string&sol;app&lowbar;name"&NewLine; android&colon;roundIcon&equals;"&commat;mipmap&sol;ic&lowbar;launcher&lowbar;round"&NewLine; android&colon;supportsRtl&equals;"true"&NewLine; android&colon;theme&equals;"&commat;style&sol;AppTheme"&gt&semi;&NewLine; &lt&semi;activity android&colon;name&equals;"&period;MainActivity"&gt&semi;&NewLine; &lt&semi;intent-filter&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"android&period;intent&period;action&period;MAIN" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;category android&colon;name&equals;"android&period;intent&period;category&period;LAUNCHER" &sol;&gt&semi;&NewLine; &lt&semi;&sol;intent-filter&gt&semi;&NewLine; &lt&semi;&sol;activity&gt&semi;&NewLine;&NewLine; &lt&semi;meta-data&NewLine; android&colon;name&equals;"com&period;google&period;android&period;geo&period;API&lowbar;KEY"&NewLine; android&colon;value&equals;"YOUR API KEY"&sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;application&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;manifest&gt&semi;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>6 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span> and add the fragment for the map as shown below&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;fragment&NewLine; xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;map"&NewLine; android&colon;name&equals;"com&period;google&period;android&period;gms&period;maps&period;SupportMapFragment"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"com&period;example&period;getcurrentlocationonmap&period;MainActivity" &sol;&gt&semi;&NewLine;<&sol;pre>&NewLine;<p>7 &period; As of Android <strong><span style&equals;"color&colon; &num;008000&semi;">Marshmallow<&sol;span><&sol;strong> Location permissions need to be explicitly approved by the user before the app begins to collect device location&period;<&sol;p>&NewLine;<p>8 &period; We had already mentioned that <strong><span style&equals;"color&colon; &num;008000&semi;">FusedLocationProviderClient<&sol;span><&sol;strong> takes care of all the connection logic on its own&period; Therefore we don’t need to initialize <strong><span style&equals;"color&colon; &num;008000&semi;">GoogleApiClient<&sol;span><&sol;strong> nor do we need to implement connection callbacks&period; We only need to initialize the FusedLocationProviderClient as shown below&colon;<&sol;p>&NewLine;<pre>fusedLocationProviderClient &equals; LocationServices&period;getFusedLocationProviderClient&lpar;this&rpar;&semi;<&sol;pre>&NewLine;<p>9 &period; Once we have <strong><span style&equals;"color&colon; &num;008000&semi;">FusedLocationProviderClient<&sol;span><&sol;strong> we can fetch the current location using the <strong><span style&equals;"color&colon; &num;008000&semi;"><em>getLastLocation<&sol;em>&lpar;&rpar;<&sol;span><&sol;strong> &period; It returns a Task object which represents an asynchronous operation&period;<&sol;p>&NewLine;<pre>Task task &equals; fusedLocationProviderClient&period;getLastLocation&lpar;&rpar;&semi;<&sol;pre>&NewLine;<p>10 &period; We will add the success callback listener to the <strong><span style&equals;"color&colon; &num;008000&semi;">Task<&sol;span><&sol;strong> object which will be invoked once the connection is established and the location is fetched&period;<&sol;p>&NewLine;<pre>task&period;addOnSuccessListener&lpar;new OnSuccessListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onSuccess&lpar;Location location&rpar; &lbrace;&NewLine; if &lpar;location &excl;&equals; null&rpar;&NewLine; currentLocation &equals; location&semi;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; currentLocation&period;getLatitude&lpar;&rpar; &plus; "&comma;" &plus;&NewLine; currentLocation&period;getLongitude&lpar;&rpar;&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; SupportMapFragment supportMapFragment &equals; &lpar;SupportMapFragment&rpar; getSupportFragmentManager&lpar;&rpar;&period;findFragmentById&lpar;R&period;id&period;map&rpar;&semi;&NewLine; supportMapFragment&period;getMapAsync&lpar;MainActivity&period;this&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&rpar;&semi;<&sol;pre>&NewLine;<p>11 &period; You must have noticed we add a null check for location in <strong><span style&equals;"color&colon; &num;008000&semi;">OnSuccessListener<&sol;span><&sol;strong>&period; This is to avoid crashes in some scenarios in which the location can be null&period; Below are some of these scenarios<&sol;p>&NewLine;<ul>&NewLine;<li class&equals;"graf graf--li graf-after--p">GPS provider is turned off in the device settings&period;<&sol;li>&NewLine;<li id&equals;"fa85" class&equals;"graf graf--li graf-after--li">Location was never recorded on the devices&period;  This could be the case of a new device or a device that has been restored to factory settings&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Complete Code<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>12 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span> and add the below code&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;333399&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;getcurrentlocationonmap&semi;&NewLine;&NewLine;import android&period;Manifest&semi;&NewLine;import android&period;content&period;pm&period;PackageManager&semi;&NewLine;import android&period;location&period;Location&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;widget&period;Toast&semi;&NewLine;import com&period;google&period;android&period;gms&period;location&period;FusedLocationProviderClient&semi;&NewLine;import com&period;google&period;android&period;gms&period;location&period;LocationServices&semi;&NewLine;import com&period;google&period;android&period;gms&period;maps&period;CameraUpdateFactory&semi;&NewLine;import com&period;google&period;android&period;gms&period;maps&period;GoogleMap&semi;&NewLine;import com&period;google&period;android&period;gms&period;maps&period;OnMapReadyCallback&semi;&NewLine;import com&period;google&period;android&period;gms&period;maps&period;SupportMapFragment&semi;&NewLine;import com&period;google&period;android&period;gms&period;maps&period;model&period;LatLng&semi;&NewLine;import com&period;google&period;android&period;gms&period;maps&period;model&period;MarkerOptions&semi;&NewLine;import com&period;google&period;android&period;gms&period;tasks&period;OnSuccessListener&semi;&NewLine;import com&period;google&period;android&period;gms&period;tasks&period;Task&semi;&NewLine;import androidx&period;annotation&period;NonNull&semi;&NewLine;import androidx&period;core&period;app&period;ActivityCompat&semi;&NewLine;import androidx&period;fragment&period;app&period;FragmentActivity&semi;&NewLine;&NewLine;public class MainActivity extends FragmentActivity implements OnMapReadyCallback &lbrace;&NewLine;&NewLine;&NewLine; Location currentLocation&semi;&NewLine; FusedLocationProviderClient fusedLocationProviderClient&semi;&NewLine; private static final int REQUEST&lowbar;CODE &equals; 200&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; fusedLocationProviderClient &equals; LocationServices&period;getFusedLocationProviderClient&lpar;this&rpar;&semi;&NewLine; fetchLastlocation&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; private void fetchLastlocation&lpar;&rpar; &lbrace;&NewLine;&NewLine; if &lpar;ActivityCompat&period;checkSelfPermission&lpar;this&comma; Manifest&period;permission&period;ACCESS&lowbar;FINE&lowbar;LOCATION&rpar; &excl;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&rpar; &lbrace;&NewLine;&NewLine;&NewLine; ActivityCompat&period;requestPermissions&lpar;this&comma; new String&lbrack;&rsqb;&lbrace;Manifest&period;permission&period;ACCESS&lowbar;FINE&lowbar;LOCATION&rcub;&comma; REQUEST&lowbar;CODE&rpar;&semi;&NewLine; return&semi;&NewLine; &rcub;&NewLine;&NewLine; Task task &equals; fusedLocationProviderClient&period;getLastLocation&lpar;&rpar;&semi;&NewLine; task&period;addOnSuccessListener&lpar;new OnSuccessListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onSuccess&lpar;Location location&rpar; &lbrace;&NewLine; if &lpar;location &excl;&equals; null&rpar;&NewLine; currentLocation &equals; location&semi;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; currentLocation&period;getLatitude&lpar;&rpar; &plus; "&comma;" &plus;&NewLine; currentLocation&period;getLongitude&lpar;&rpar;&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; SupportMapFragment supportMapFragment &equals; &lpar;SupportMapFragment&rpar; getSupportFragmentManager&lpar;&rpar;&period;findFragmentById&lpar;R&period;id&period;map&rpar;&semi;&NewLine; supportMapFragment&period;getMapAsync&lpar;MainActivity&period;this&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onMapReady&lpar;GoogleMap googleMap&rpar; &lbrace;&NewLine; LatLng latLng &equals; new LatLng&lpar;currentLocation&period;getLatitude&lpar;&rpar;&comma; currentLocation&period;getLongitude&lpar;&rpar;&rpar;&semi;&NewLine; MarkerOptions markerOptions &equals; new MarkerOptions&lpar;&rpar;&period;position&lpar;latLng&rpar;&period;title&lpar;"You are here"&rpar;&semi;&NewLine; googleMap&period;animateCamera&lpar;CameraUpdateFactory&period;newLatLng&lpar;latLng&rpar;&rpar;&semi;&NewLine; googleMap&period;animateCamera&lpar;CameraUpdateFactory&period;newLatLngZoom&lpar;latLng&comma; 15&rpar;&rpar;&semi;&NewLine; googleMap&period;addMarker&lpar;markerOptions&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onRequestPermissionsResult&lpar;int requestCode&comma; &commat;NonNull String&lbrack;&rsqb; permissions&comma; &commat;NonNull int&lbrack;&rsqb; grantResults&rpar; &lbrace;&NewLine;&NewLine; switch &lpar;requestCode&rpar; &lbrace;&NewLine; case REQUEST&lowbar;CODE&colon;&NewLine;&NewLine; if &lpar;grantResults&period;length &gt&semi; 0 &amp&semi;&amp&semi; grantResults&lbrack;0&rsqb; &equals;&equals; PackageManager&period;PERMISSION&lowbar;GRANTED&rpar; &lbrace;&NewLine; fetchLastlocation&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; break&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>When you run your app it will look like this &colon;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1268" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1572340388&period;png" alt&equals;"Screenshot&lowbar;1572340388" width&equals;"309" height&equals;"550" &sol;>      <img class&equals;"alignnone wp-image-1267" style&equals;"color&colon; var&lpar;--color-text&rpar;&semi;" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2019&sol;10&sol;Screenshot&lowbar;1572340433&period;png" alt&equals;"Screenshot&lowbar;1572340433" width&equals;"310" height&equals;"551" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>I hope this article will help you in understanding how to use <span style&equals;"color&colon; &num;008000&semi;"><strong>Fusedlocationprovider<&sol;strong><&sol;span> to get the current location of your device&period;&NewLine;

Exit mobile version