Site icon LD Talent Blog

How to pick the current location in Google Maps Android and send it via SMS

google maps android SMS

Hire the author: Dennis M

Google Maps Android SMS Image Source unsplash.com.

Here is the GitHub – android-google-maps-SMS link for this project.

Introduction

In this tutorial, we will display and place a marker at the user’s current location in Google Maps for Android and send it via SMS. For doing this, we need to generate a Google Map API key. The process of generating Google Map API is in the tutorial here.

To display the user’s current location we need to implement some interfaces and their callbacks methods in Google Map.

OnMapReadyCallback(): This callback interface invokes when its instance is set on a MapFragment object. The onMapReady(GoogleMap) method of the OnMapReadyCallback interface is in use when the map is ready to use. In the onMapReady(GoogleMap) method we can add markers, listeners, and other attributes.

LocationCallback(): This interface is for receiving a notification when the device location has changed. The abstract method of LocationListener onLocationResult(LocationResult locationResult) is called when the location has changed.

What motivated me in doing this project? “How to pick the current location in Google Maps Android and send it via SMS”. I’m developing an android app that uses google maps and users are required to pick the current location and send it via SMS. This blog post will provide clear and precise instructions on how to implement: the current location in Google Maps and send SMS  in the same android app activity. Let’s see how to do this quickly.

How to Get Started

This guide is a quick start to adding a map to an Android app. Android Studio is the recommended development environment for building an app with the Maps SDK for Android and below are a few resources to get you prepared before starting this project:

Photo Preview

Glossary

Google Maps is a web mapping service developed by Google. It offers satellite imagery, aerial photography, street maps, 360° interactive panoramic views of streets, real-time traffic conditions, and route planning for traveling by foot, car, bicycle, and air, or public transportation.

SmsManager manages SMS operations such as sending data, text, and PDU SMS messages. Get this object by calling the static method getDefault(). To create an instance of SmsManager associated with a specific subscription ID, call getSmsManagerForSubscriptionId(int). This is typically used for devices that support multiple active subscriptions at once.

Steps:

Google Maps API in Android has revealed FusedLocationProviderClient under google API. FusedLocationProviderClient is for interacting with the location of an android device using the fused location provider. (NOTE: To use this feature, GPS must be turned on your device. To programmatically ask the user to turn on GPS, please check the previous article).

So let’s get started with the tutorial for getting the current location and sending SMS.

Step 1: 

Firstly, the FusedLocationProviderClient uses of the Google Play SDK. Therefore, open build.gradle and add the following dependencies as shown below:

  

Sync the project.

Step 2: 

Open activity_maps.xml and add the fragment for map and button for sending SMS as shown below:

Step 3:

Open AndroidManifest.xml, and add the following permissions to your application to use location services.

 

This provides the ability to access location information at coarse and fine granularities. You can remove ACCESS_FINE_LOCATION this will still work with less precision. Note: The permissions model has changed starting in Marshmallow. If your targetSdkVersion >= 23 and you are running on a Marshmallow (or later) device, you may need to enable runtime permissions.

Step 4:

Since we are using dangerous permission of location, so we need to explicitly ask for permission. Also, before getting the current location of the user, override the checkLocationPermission() method and check if permissions are granted or not using the Alert Dialog and if the permission is granted and the GPS is enabled in the device. 

Step 5:

FusedLocationProviderClient takes care of all the connection logic on its own. Therefore we don’t need to initialize GoogleApiClient nor do we need to implement connection callbacks. We only need to initialize the FusedLocationProviderClient as shown below:

Step 6:

Now, we need to define the type of location request that we want i.e. we can set the desired level of accuracy of location, the desired interval of location update, desired priority, etc. All these settings can be by using the LocationRequest data object. So, we can add the below code:

Step 7:

If we want the last known location, we can implement and call the LocationCallback function that is responsible for getting the current location. The following is the code of LocationCallback:

Step 8:

Finally, we will create a new function for sending the device’s location as a link via SMS.

Do not forget to enter the phone number you want to receive the link in the destination address. It must begin with the country code.

Step 9:

After doing all the works and before running the application your full class code should look like the following:

So now run your application and ensure your GPS settings are on. You should see your current location on the google map. If you press the SEND_SMS button, the contact you entered in the previous step will receive a link to your location.

Future Directions

After that, display details of the place (a business or other point of interest) at that location using the Maps SDK for Android, the Places SDK for Android, and the fused location provider in the Google Play services location APIs.

Learning Strategies and Tools

In conclusion, we have learned about how the Google Maps API includes an all-new way to load maps. Activities now have to implement the OnMapReady CallBack interface, which comes with onMapReady() method override that is executed every time we run SupportMapFragment.getMapAsync(OnMapReadyCallback); and the call is successfully completed. This activity code will provide basic functionality for including a Google Map using a SupportMapFragment.

Checking the specific API documentation to determine whether the connection is in use. Therefore, LocationRequest objects are for requesting quality of service for location updates. In conclusion, FusedLocationProviderClient has very strong services.

Reflective Analysis

One of the unique features of mobile applications is location awareness. Mobile users take their devices with them everywhere. Therefore, adding location awareness to your app offers users a more contextual experience. The location APIs available in Google Play services facilitate adding location awareness to your app. For instance with automated location tracking, Geo-fencing, and activity recognition.

In this blog, we learn how to display the changed location of the user in your app. Send SMS using the smsManager() function. We have used the Fused Location API for the same purpose. You can use this concept while making some ride-sharing application.

In conclusion, it took me a total of 20 hours to finish the project and the blog.  Finally, everything is available in this GitHub repository.

Link to the previous post: https://blog.ldtalentwork.com/2020/07/26/how-to-show-enable-location-dialog-without-navigating-to-settings-page-like-google-maps-in-android/.

That’s all for this tutorial!

Hire the author: Dennis M

Exit mobile version