Intermediate: Integration of HMS Location Kit in Unity Game

Manoj Kumar
4 min readNov 27, 2021

--

Overview

In this article, I will create a demo game and integrate Huawei Location Kit. I will display the user’s current location in coordinates and also user can share his location to other users in game.

Location Service Introduction

Location Kit allows user enabling their game to get quick and accurate user locations, and expand global positioning capabilities by using GPS, Wi-Fi and base station locations.

Currently, it provides three main capabilities: fused location, activity identification and geofence. You can call one or more of these capabilities as needed.

  1. Fused location
  2. Activity identification
  3. Geofence

Prerequisite

  1. Unity Engine (Installed in system)
  2. Huawei phone
  3. Visual Studio 2019
  4. Android SDK & NDK (Build and Run)

App Gallery Integration process

  1. Sign In and Create or Choose a project on AppGallery Connect portal.

2. Navigate to Project settings and download the configuration file.

Game Development

1. Create new game in Unity.

2. Download HMS Unity Plugin from below site.

https://github.com/EvilMindDevs/hms-unity-plugin/releases

3. Open Unity Engine and import downloaded HMS Plugin.

Choose Assets > Import Package > Custom Package

4. Provide the App ID and other details from agconnect-service.json file and click configure Manifest.​​​

5. Create Script

LocationManager.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LocationManager : MonoBehaviour
{
static FusedLocationProviderClient fusedLocationProviderClient;
static LocationRequest locatinoRequest;
public Text latitude;
public Text longitude;
private void Awake()
{
//AdListener add = new AdListener(); TestClass receiver = new TestClass();
BroadcastRegister.CreateLocationReceiver(receiver);
Debug.LogError("RegisterReceiver--->");
locatinoRequest = LocationRequest.create();
locatinoRequest.setInterval(10000);
locatinoRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(locatinoRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
Activity act = new Activity(); fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(act);
SettingsClient settingsClient = LocationServices.getSettingsClient(act);
settingsClient.checkLocationSettings(locationSettingsRequest)
.addOnSuccessListener(new OnSuccessListenerTemp(this))
.addOnFailureListener(new OnFailureListenerTemp());
Debug.LogError("RegisterReceiver request send--->");
}
class OnSuccessListenerTemp : OnSuccessListener
{
private RegisterReceiver registerReceiver;
public OnSuccessListenerTemp(RegisterReceiver registerReceiver)
{
this.registerReceiver = registerReceiver;
}
public override void onSuccess(AndroidJavaObject arg0)
{
Debug.LogError("onSuccess 0--->");
fusedLocationProviderClient.requestLocationUpdates(locatinoRequest, new OnLocationCallback(this.registerReceiver), Looper.getMainLooper())
.addOnSuccessListener(new OnReqSuccessListenerTemp())
.addOnFailureListener(new OnReqFailureListenerTemp())
;
}
}; class OnReqSuccessListenerTemp : OnSuccessListener
{
public override void onSuccess(AndroidJavaObject arg0)
{
Debug.LogError("onSuccess");
}
}; class OnReqFailureListenerTemp : OnFailureListener
{
public override void onFailure(Exception arg0)
{
Debug.LogError("onFailure");
}
}
class OnLocationCallback : LocationCallback
{
private RegisterReceiver registerReceiver;
public OnLocationCallback(RegisterReceiver registerReceiver)
{
this.registerReceiver = registerReceiver;
}
public override void onLocationAvailability(LocationAvailability arg0)
{
Debug.LogError("onLocationAvailability");
} public override void onLocationResult(LocationResult locationResult)
{
Location location = locationResult.getLastLocation();
HWLocation hWLocation = locationResult.getLastHWLocation();
Debug.LogError("onLocationResult found location"); if (location != null)
{
Debug.LogError("getLatitude--->" + location.getLatitude() + "<-getLongitude->" + location.getLongitude());
//latitude.text = "Latitude-->" + location.getLatitude();
//longitude.text = "Longitude-->" + location.getLongitude() ;
//RegisterReceiver.this.updateData(location);
registerReceiver.updateData(location);
}
if (hWLocation != null)
{
string country = hWLocation.getCountryName();
string city = hWLocation.getCity();
string countryCode = hWLocation.getCountryCode();
string dd = hWLocation.getPostalCode();
Debug.LogError("country--->" + country + "<-city->" + city + "<-countrycode->" + countryCode + "<-postal code->" + dd);
}
else
{
Debug.LogError("onLocationResult found location hWLocation is null--->");
}
}
}
private void updateData(Location location)
{
latitude.text = "Latitude-->" + location.getLatitude();
longitude.text = "Longitude-->" + location.getLongitude();
}
class OnFailureListenerTemp : OnFailureListener
{
public override void onFailure(Exception arg0)
{
Debug.LogError("onFailure--->");
}
}
// Start is called before the first frame update
void Start()
{
} // Update is called once per frame
void Update()
{
}
}

App Build Result

Tips and Tricks

  • HMS plugin v1.2.0 supports 7 kits and installed HMS Core (APK) 3.0.0.300 or later.
  • It is recommended that the geofence radius should be minimum of 200 meters. Precision cannot be assured if the geofence radius is less than 200 meters.

Conclusion

In this article, we have learned how to integrate Huawei Location in Unity Based Game. After completely read this article user can get their location in coordinates and share his/her location to other users in the game.

Thanks for reading this article. Be sure to like and comment to this article, if you found it helpful. It means a lot to me.

References

HMS Docs:

https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/introduction-0000001050706106

--

--

Manoj Kumar
Manoj Kumar

No responses yet