DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Implement Real-time Locating in an Android App

Implement Real-time Locating in an Android App

Making real-time geolocation data a reality.

Jackson Jiang user avatar by
Jackson Jiang
·
Jan. 16, 22 · Java Zone · Tutorial
Like (4)
Save
Tweet
3.21K Views

Join the DZone community and get the full member experience.

Join For Free

Real-time locating is a core function for many apps, allowing them to quickly and accurately locate users' real time locations. Location Kit enables apps to quickly obtain precise user locations and build up global locating capabilities, helping you implement personalized map display and interaction, as well as improve overall location-based service experience.

This article demonstrates how to use Location Kit and Map Kit to implement the real-time locating capability in an app.

Expectations

An app can obtain and display a user's real-time location on the map, especially when the app is launched for the first time. The map display changes in accordance to the user's actual location.

Implementation Principle

An app uses Location Kit to obtain a user's real-time location and uses Map Kit to display the My Location button on the in-app map that the user can tap to determine their real-time location.

Preparations

1. Register as a developer

2. Create an app, add the SHA-256 signing certificate fingerprint, enable Map Kit and Site Kit, and download the agconnect-services.json file of the app.

 3.  Configure the Android Studio project.

(1)   Copy the agconnect-services.json file to the app directory of the project.

•   Go to allprojects > repositories and configure the Maven repository address for the HMS Core SDK.

• Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.

• If the agconnect-services.json file has been added to the app, go to buildscript > dependencies and add the AppGallery Connect plugin configuration.

Groovy
buildscript {
    repositories {
        maven { url 'https://developer.huawei.com/repo/' }
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath 'com.huawei.agconnect:agcp:1.3.1.300'
    }
}

allprojects {
    repositories {
        maven { url 'https://developer.huawei.com/repo/' }
        google()
        jcenter()
    }
}

(2) Add build dependencies in the dependencies block.

Groovy
dependencies {
    implementation 'com.huawei.hms:maps:{version}'
    implementation 'com.huawei.hms:location:{version}'
}

(3) Add the following configuration to the file header.

Groovy
apply plugin: 'com.huawei.agconnect'

(4) Copy the signing certificate generated in Generating a Signing Certificate to the app directory of your project, and configure the signing certificate in Android in the build.gradle file.

Groovy
signingConfigs {
    release {
        // Signing certificate.
            storeFile file("**.**")
            // KeyStore password.
            storePassword "******"
            // Key alias.
            keyAlias "******"
            // Key password.
            keyPassword "******"
            v2SigningEnabled true
        v2SigningEnabled true
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        debuggable true
    }
    debug {
        debuggable true
    }
}

Key Code Implementation

(1) Compile a service to obtain a user's real-time location.

Java
public class LocationService extends Service {
    private final String TAG = this.getClass().getSimpleName();
    List<ILocationChangedLister> locationChangedList = new ArrayList<>();
    // Location
    private FusedLocationProviderClient fusedLocationProviderClient;
    private LocationRequest mLocationRequest;
    private final LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            locationResult.getLocations();
            Log.d(TAG, "onLocationResult: " + locationResult);
            Location location = locationResult.getLocations().get(0);
            Log.w(TAG, "onLocationResult:Latitude " + location.getLatitude());
            Log.w(TAG, "onLocationResult:Longitude " + location.getLongitude());

            for (ILocationChangedLister locationChanged : locationChangedList) {
                locationChanged.locationChanged(new LatLng(location.getLatitude(), location.getLongitude()));
            }
        }

        @Override
        public void onLocationAvailability(LocationAvailability locationAvailability) {
            super.onLocationAvailability(locationAvailability);
            Log.d(TAG, "onLocationAvailability: " + locationAvailability.toString());
        }
    };

    private final MyBinder binder = new MyBinder();

    private final Random generator = new Random();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        Log.i("DemoLog", "TestService -> onCreate, Thread: " + Thread.currentThread().getName());
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("DemoLog",
            "TestService -> onStartCommand, startId: " + startId + ", Thread: " + Thread.currentThread().getName());
        return START_NOT_STICKY;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i("DemoLog", "TestService -> onUnbind, from:" + intent.getStringExtra("from"));
        return false;
    }

    @Override
    public void onDestroy() {
        Log.i("DemoLog", "TestService -> onDestroy, Thread: " + Thread.currentThread().getName());
        super.onDestroy();
    }

    public int getRandomNumber() {
        return generator.nextInt();
    }

    public void addLocationChangedlister(ILocationChangedLister iLocationChangedLister) {
        locationChangedList.add(iLocationChangedLister);
    }

    public void getMyLoction() {
        Log.d(TAG, "getMyLoction: ");
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

        SettingsClient settingsClient = LocationServices.getSettingsClient(this);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        mLocationRequest = new LocationRequest();
        builder.addLocationRequest(mLocationRequest);
        LocationSettingsRequest locationSettingsRequest = builder.build();
        // Location setting
        settingsClient.checkLocationSettings(locationSettingsRequest)
            .addOnSuccessListener(locationSettingsResponse -> fusedLocationProviderClient
                .requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper())
                .addOnSuccessListener(aVoid -> Log.d(TAG, "onSuccess: " + aVoid)))
            .addOnFailureListener(Throwable::printStackTrace);
    }

    public class MyBinder extends Binder {

        public LocationService getService() {
            return LocationService.this;
        }
    }

    public interface ILocationChangedLister {

        /**
         * Update the location information
         *
         * @param latLng The new location information
         */
        public void locationChanged(LatLng latLng);
    }
}

(2) Add a map in the activity to monitor a user's real-time location.

XML
<!--Add a map using the XML layout file:-->

<com.huawei.hms.maps.MapView
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" ></div>
 
<!--Add a map in the activity:-->
mapView.onCreate(null);
mapView.getMapAsync(this);

Tap My Location button to display the current location on the map

Java
@Override
public void onMapReady(HuaweiMap huaweiMap) {
    hMap = huaweiMap;
    hMap.setMyLocationEnabled(true);
}

Bind Location Kit to listen to location changing events

Java
private ServiceConnection conn = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        isBound = true;
        if (binder instanceof LocationService.MyBinder) {
            LocationService.MyBinder myBinder = (LocationService.MyBinder) binder;
            locationService = myBinder.getService();
            Log.i(TAG, "ActivityA onServiceConnected");
            locationService.addLocationChangedlister(iLocationChangedLister);
            locationService.getMyLoction();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        isBound = false;
        locationService = null;
        Log.i(TAG, "ActivityA onServiceDisconnected");
    }
};

Bind the activity to LocationServic

 
private void bindLocationService() {
    Intent intent = new Intent(mActivity, LocationService.class);
    intent.putExtra("from", "ActivityA");
    Log.i(TAG, "-------------------------------------------------------------");
    Log.i(TAG, "bindService to ActivityA");
    mActivity.bindService(intent, conn, Context.BIND_AUTO_CREATE);
}

Process the location changing events in the location changing listener

Java
LocationService.ILocationChangedLister iLocationChangedLister = new LocationService.ILocationChangedLister() {
    @Override
    public void locationChanged(LatLng latLng) {
        Log.d(TAG, "locationChanged: " + latLng.latitude);
        Log.d(TAG, "locationChanged: " + latLng.longitude);
        updateLocation(latLng);
    }
};

Update map view

Java
private void updateLocation(LatLng latLng) {
    mLatLng = latLng;
    hMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 1));
}

Testing the App

You can use a mock location app to change your current location and see how the map view and My Location button alter accordingly.

app Android (robot)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 3 Predictions About How Technology Businesses Will Change In 10 Years
  • Everything I Needed to Know About Observability, I Learned from ‘Bewitched’
  • How to Optimize MySQL Queries for Speed and Performance
  • How To Integrate Third-Party Login Systems in Your Web App Using OAuth 2.0

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo