Huawei Developers
Published in

Huawei Developers

Finding Nearby Hospitals With HMS

This article is about finding the nearest hospitals of your location by using Huawei’s Location, Map and Site kit.

We will get our current location with Location Kit and find the nearest hospitals by using site kit and show them on map with map kit. Lets get information about these kits.

Location Kit:

HUAWEI Location Kit combines the GPS, Wi-Fi, and base station location functionalities into your app to build up global positioning capabilities, allowing you to provide flexible location-based services targeted at users around the globe. Currently, it provides three main capabilities: fused location, activity identification and geofence. You can call one or more of these capabilities as needed.

Map Kit:

HUAWEI Map Kit is an SDK for map development. It covers map data of more than 200 countries and regions, and supports dozens of languages. With this SDK, you can easily integrate map-based functions into your apps. With using Map Kit, markers or icons can be added to the map.

Site Kit:

With HUAWEI Site Kit, your app can provide users with convenient and secure access to diverse, place-related services. By Huawei Site Kit, you can search places and nearby places.

To able to using these kits, you need follow some instructions.

1- Register as a Huawei Developer

To use these kits, register to Huawei website. You can find information in this link →

2- Activating kits in AppGallery Connect

After successfully register process, create an app and press manage the API’s button in AppGallery Connect.

In this page, enable the map kit and site kit.

3- Generate a Signing Certificate Fingerprint

In AppGallery Connect, press General Information button.

In below this page, there is a SHA-256 certificate fingerprint area box.

To fill this area, you need to open a new project in Android Studio and press Gradle button in the top right corner. In tasks, android and double clicking to signingReport will generate this SHA-256 key.

This key can be seen in console. After you generated this key, you need to copy and paste to the SHA-256 certificate fingerprint.

Also, copy the signature file generated in Generating a Signing Certificate Fingerprint to the app directory of your project and configure the signature in the build.gradle file.

4- Downloading agconnect-services.json file

In General Information tab, below the page you can see the agconnect-services.json file. In order to use these kits, you need to download this file.

After downloading agconnect-services.json file, paste the to the app root directory.

5- Adding Dependencies

Configure of the Maven repository address for the HMS SDK is required for using these kits.

AppGallery Connect plug-in dependency has to be added to top of the file.

Add kits dependencies in build.gradle file in app directory.

6- Permissions:

To use map kit location and site kit there are some permissions are necessary. Add these permissions in manifest file.

After all these steps, we can begin to coding!

Development Process:

There are two activities in this project. In first activity (HospitalLocation Activity), we get the location and getting the input value of the distance by the user. In second activity(Map Activity), we show the nearest hospitals as the user required.

XML Structure of the HospitalLocationActivity:

View of the layout file

Java File of the HospitalLocation Activity:

In this activity, we are expecting a value from the user to get the hospital distances and getting the user location information.

  • Checking Permissions and Getting User Location

First, we are checking if the user granted the permissions or not by checkPermissions method. After that we get the location by getLocationData method if the user granted these permissions.

With setting mLocationRequest.setInterval(30000), we get the location data for nearly every 3 seconds. With that option, we can get the nearest hospitals even user changes his/her location.

By setting LocationRequest.PRIORITY_HIGH_ACCURACY, we get the user location as correct as possible. There are some options that can be more battery saver in this parameter.

  • Getting Nearest Hospitals

With this method, we set the radius distance by the input of the user and get the nearest hospitals with their name, latitude and longitude values.

  • Send hospitals information to second Activity

After that we got hospitals data, we send to the second activity to show them on map.

XML Structure of the Map Activity:

In order to use Map Kit in Activity, we need to use MapView.

Java File of the Map Activity:

In this activity, we get user location and the nearest hospitals latitude and longitude values. We show the hospitals on the map.

  • Getting Data From First Activity and Creating MapView
  • Showing Hospitals on the Map

We search the hospitals and drawing on the map with the help of MarkerOptions object. We can get the hospital locations, titles and icons with this object.

Lets Try with an Example!

In this example I searched 5 kms distance, but you can search up to 50 kms by the power of Site Kit.

As it be can seen, we could able to find the nearest hospitals up to 5 kms.

We could easily integrate and used location,map and site kit. With the help of these kits, we could able to find nearest hospitals to us.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store