Intermediate: Integration of HMS Location Kit in Unity Game

  1. Fused location
  2. Activity identification
  3. Geofence
  1. Unity Engine (Installed in system)
  2. Huawei phone
  3. Visual Studio 2019
  4. Android SDK & NDK (Build and Run)
  1. Sign In and Create or Choose a project on AppGallery Connect portal.
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()
{
}
}
  • 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.

--

--

--

Android Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Kotlin vs Java Example: Part - 1 | Erselan Khan

Google Releases Android 13 Beta Update 3.2 to Fix More Bugs

Abstract & Test Rendering Logic of State in Android

How to handle FCM notifications in Flutter AFTER they arrive?

Linux VM based Setup for Android Automation

Reverse engineering and modifying an Android game (.apk) — CTF

Flutter App Stack Widget Detailed Explanation With Example

Intermediate: Find Doctor Near to Me using Huawei Kits (Account, Crash and Analytics) in Android…

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
Manoj Kumar

Manoj Kumar

Android Developer

More from Medium

How to upgrade materials to URP in Unity 2021.2 and above

How to make a Mario Kart game with Unity

Sci-fi Lab — Collider

Loam Sandbox Post-Launch Roadmap