How to Build a Geographically-aware, Location-based Android Dating App

I built an app to rig the chances of me getting a date.

With over a  call.

Calculating Distance in Realtime

We’ll be getting the data in the form of an array. The first two elements of the array are the IDs of the user and the last two elements are the location of the user who initiated the request. The first element is the ID of the initiator, and the second is a possible swipe candidate. Once we finish the calculation, we’ll send the ID of the unique user and the distance they are from the initiator.

https://medium.com/media/4ad05d6503091223d5782f1573db624f/href

The result of this function will look like this:

{ "ID": "Unique User ID", "distance": 5 }

How to Swipe Through Users on the Android App

To start off, create an empty Android Studio project with Kotlin support checked.

Next, look at the dependencies we’re going to add to our app-level Gradle file to ensure our application runs smoothly.

The first dependency is the PubNub SDK, which will help us publish and subscribe to the logic we just created. Related to the PubNub SDK, we’ll also need our Publish and Subscribe keys. You can get your publish and subscribe keys by going through the quick setup below.

The other dependencies needed are for the visual component of our application — the swiping functionality.

https://medium.com/media/89ab8ca822b34dc35f39880eb927b78e/href

Creating the User Interface

First, we’ll adjust our activity_main.xml to accommodate for our swiping feature that’ll be initialized in our MainActivity.kt file.

https://medium.com/media/fc5492a5b8e5a9064348302c36650eb6/href

Next, we’ll create each profile card’s UI, as well as the overlay on each of them, taking into consideration whether the user is swiping to the left or right.

https://medium.com/media/ed1c1d39330e09087bc843f2f213c5e1/href

That’s it for the UI, now let’s cover the backend.

Integrating the Application Logic

For our application to be complete we’ll be creating four separate files. The first file we’re going to need is a class that will act as an object for each profile and will contain the related information.

https://medium.com/media/00152ace16494a6697fa49b89e042e6c/href

Next, we’re going to create a file that will have some helper functions to update our collection of profiles.

https://medium.com/media/86a19881e944dff978a60b47280fdcbd/href

Now, we can load each profile into the frontend. We’ll do this within a class called the CardStackAdapter.

https://medium.com/media/e4a6fa9d3c8db8d3b4553446ddf8e18e/href

Stitching Everything Together

We can head over to the MainActivity.kt file to see how everything fits together.

Let’s have a quick look at the onCreate and onStart methods.

https://medium.com/media/6550e866b183600ec0d9450bb9d2fdc2/href

We can break down everything that’s happening into three things.

First, we’ll get the location of the device using Fused Location. Next, we’ll subscribe to a channel with the same name as our device ID, since all the possible people we can swipe on are published to that channel. Lastly, in the onStart, we’ll be publishing the date related to the device, just like the ID and Location. The reason we publish in the onStart and not the onCreate is because we won’t be able to get all the information we need to publish until the activity starts.

With that, let’s add all the features and using your pub/sub keys (they’re in your Admin Dashboard), in our MainActivity. In the end, our file will look like this:

https://medium.com/media/eb6713bf8996cab3d9b535966473e2bf/href

Let’s run the app! In either an emulator or on a device, you can see the swiping functionality, as well as the user’s distance from you.

Nice work! Want to explore more features and ideas around mobile dating apps? Check out our realtime dating apps overview, and see how you can power cross-platform, fast, and secure dating apps at global scale with PubNub’s chat APIs and messaging infrastructure.

Originally published at www.pubnub.com.


How to Build a Geographically-aware, Location-based Android Dating App was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.