The Simplest Way to Cluster Your Photos by Location

Emine İNAN
Huawei Developers
Published in
4 min readMar 8, 2023
Photo by Kelsey Knight on Unsplash

Introduction

Hi everyone👋

In this article, through the PhotographyApp application, I will explain how to cluster your photos by location, how to view the clusters on the map, and how to display these clustered photos by clicking on the cluster.

Contents

Introducing the PhotographyApp

1- Integration of Map Kit

2 -Clustering Photos by Location

3 -Viewing Clusters on the Map

4 -Displaying Clustered Photos

Conclusion

PhotographyApp

PhotographyApp is a reference application for HMS Kits to phones running with the android based HMS Service. PhotographyApp basically is an app that has everything you could want from a photo app.

PhotographyApp can display photos on a map with clustering. In this way, you can easily find and view where your photos were taken. You can view photos by date by clicking on the cluster.

About Map Kit

Map Kit is an SDK for map development. It covers map data of more than 200 countries and regions and supports over 70 languages. With this SDK, you can easily integrate map-based functions into your apps.

1. Integration of Map Kit

  1. Create an app in App Gallery Connect and integrate the App Gallery Connect SDK into your app. For details, you can follow the preparations codelab: Preparations for Integrating HUAWEI HMS Core
  2. Enable Map Kit on the Manage APIs tab page.
AppGallery Connect

3. Add build dependencies to the dependencies part.

app-level gradle file

2. Clustering Photos by Location

  1. Get permission from the user to access photos in the memory of the phone. Add the following permission to your AndroidManifest.xml file.
AndroidManifest.xml

2. Get permission from the user to access photos using readExternalStoragePermissionResultLauncher in HomeFragment.

HomeFragment.kt

3. Get permission from the user to access the location of the photos. Add the following permission to your AndroidManifest.xml file.

AndroidManifest.xml

4. Get permission from the user to access the photos' location by using accessMediaLocationPermissionResultLauncher in HomeFragment.

HomeFragmnet.kt

5. Reach the photos from the phone’s memory by using the getAllImagesFromSdCard function in HomeViewModel.

HomeViewModel.kt

6. Get the full bytes of the photo and pass the URI of the photo by calling setRequireOriginal() from your MediaStore object. You can examine this code block in the getAllImagesFromSdCard function in HomeViewModel.

HomeViewModel.kt

7. Get location information from the photos using the Exifınterface library in HomeViewModel.

HomeViewModel.kt

8. Create a class called GeoDegreeConverter to convert GPS values from Exifınterface into values that can be used on the map.

HomeViewModel.kt

9. Pass the path and location information of images to PhotoModel in the getAllImagesFromSdCard function and add it to the allPhotoModels list in HomeViewModel.

HomeViewModel.kt

3 . Viewing Clusters on the Map

  1. Add the Huawei map to the fragment_home.xml file.
fragment_home.xml

2. Set the API key in the entry class of your project.

PhotoApplication.kt

3. Add the configuration for calling MapView to the HomeFragment file.

HomeFragment.kt

4. Add the lifecycle methods of MapView in HomeFragment.

HomeFragment.kt

5. In order to show the photos for which you previously accessed location information on the map, use the loadPhotosWhichHasLocation function in HomeFragment.

HomeFragment.kt

6. Call the setClustersToMap function to set the clusters on the map in HomeFragment.

HomeFragment.kt

4 . Displaying Clustered Photos

1. Navigate the user to the ClusteredPhotosFragment to view the photos of the clicked cluster in HomeFragment.

HomeFragment.kt

2. Add the RecyclerView to the fragment_clustered_photos.xml file.

fragment_clustered_photos.xml

3. Create the ClusteredPhotosAdapter class.

ClusteredPhotosAdapter.kt

4. Display the photos according to their dates, which we have previously obtained location information through the Recyclerview and Adapter, in the ClusteredPhotosFragment.

ClusteredPhotosFragment.kt

Output

Location-Based Photo Clustering
Photo Displaying

Conclusion

In this article, I explained to you how to display the photos on a map with clustering and to display the photos of these clusters by date. I hope it was a useful article.

Thanks for reading, if you have any questions or comments please feel free to add them below.

--

--

Emine İNAN
Huawei Developers

Android Developer by day, Android Developer by night. @Huawei