1

In daily travel, route planning is a very important part. Users want to go to a certain place, get all the routes to the place, and then choose the appropriate route according to the estimated travel time, which is very convenient for travel. There are also a lot of usage scenarios in daily life. In travel apps, suitable travel routes and estimated time of arrival can be planned for users according to the passenger’s destination; in convenience life apps, users can search for surrounding services within a specified range and view The route of each service point; in the takeaway app, reasonable path planning can make it more convenient for riders to take orders and deliver.

This function can be realized by combining HMS Core location service and map service . You can first use the location search capability of the location service to search the surrounding environment through the specified keyword and optional geographic range, and then use the path planning capability of the map service to display the path to the destination. When determining the location, based on various hybrid positioning modes such as GNSS, Wi-Fi, and base stations, it helps the application to obtain location information quickly and accurately. After the location information is obtained, the map service is integrated, and the map data can cover more than 200 countries and regions, support hundreds of languages, and improve the user experience in an all-round way. Not only that, the map service can also provide different route planning schemes and estimated arrival times according to different vehicles and real-time road conditions.

Show results

The map service provides 3 modes of transportation, driving, cycling and walking. According to different means of transportation, several suitable route planning schemes can be quickly calculated, and the distance and estimated arrival time can be given.

different means of transport

Not only that, the map service can also provide two alternative route preferences of short distance and fast speed according to the road condition information, which greatly improves the user experience.

route preferences

Development steps

Preparation for integration

  1. AGC account registration, project creation

1) Register as a developer

Registered address: please click the link

2) Create an application, add sha256, turn on the map/site switch, and download the json file

  1. Integrate Huawei Map Service and Location Service SDK

1) Copy the "agconnect-services.json" file to the application-level root directory

• Configure the Maven repository address of the HMS Core SDK in "allprojects > repositories".

• Configure the Maven repository address of the HMS Core SDK in "buildscript > repositories".

• If the "agconnect-services.json" file is added to the App, you need to add agcp configuration in "buildscript > dependencies".

 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 the following compilation dependencies in "dependencies"

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

3) Add configuration in the file header

 apply plugin: 'com.huawei.agconnect'

4) Configure the signature in android. Copy the signature file generated by generating the signature certificate fingerprint to the app directory of your project, and configure the signature in the "build.gradle" file

 signingConfigs {
    release {
        // 签名证书
            storeFile file("**.**")
            // 密钥库口令
            storePassword "******"
            // 别名
            keyAlias "******"
            // 密钥口令
            keyPassword "******"
            v2SigningEnabled true
        v2SigningEnabled true
    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        debuggable true
    }
    debug {
        debuggable true
    }
}
  1. The main code and functions used in the project

1) Text search: By implementing the textSearch function in the location service, the text content is searched and displayed.

 SearchResultListener<TextSearchResponse> resultListener = new SearchResultListener<TextSearchResponse>() {
    // Return search results upon a successful search.
    @Override
    public void onSearchResult(TextSearchResponse results) {
        List<Site> siteList;
        if (results == null || results.getTotalCount() <= 0 || (siteList = results.getSites()) == null
                || siteList.size() <= 0) {
            resultTextView.setText("Result is Empty!");
            return;
        }

        mFirstAdapter.refresh(siteList);

        StringBuilder response = new StringBuilder("\n");
        response.append("success\n");
        int count = 1;
        AddressDetail addressDetail;
        Coordinate location;
        Poi poi;
        CoordinateBounds viewport;
        for (Site site : siteList) {
            addressDetail = site.getAddress();
            location = site.getLocation();
            poi = site.getPoi();
            viewport = site.getViewport();
            response.append(String.format(
                    "[%s] siteId: '%s', name: %s, formatAddress: %s, country: %s, countryCode: %s, location: %s, poiTypes: %s, viewport is %s \n\n",
                    "" + (count++), site.getSiteId(), site.getName(), site.getFormatAddress(),
                    (addressDetail == null ? "" : addressDetail.getCountry()),
                    (addressDetail == null ? "" : addressDetail.getCountryCode()),
                    (location == null ? "" : (location.getLat() + "," + location.getLng())),
                    (poi == null ? "" : Arrays.toString(poi.getPoiTypes())),
                    (viewport == null ? "" : viewport.getNortheast() + "," + viewport.getSouthwest())));
        }
        resultTextView.setText(response.toString());
        Log.d(TAG, "onTextSearchResult: " + response.toString());
    }

    // Return the result code and description upon a search exception.
    @Override
    public void onSearchError(SearchStatus status) {
        resultTextView.setText("Error : " + status.getErrorCode() + " " + status.getErrorMessage());
    }
};
// Call the place search API.
searchService.textSearch(request, resultListener);

2) Walking path planning: The data callback is realized by calling the api interface of the map service and presented on the map.

Api documentation: please click the link

 NetworkRequestManager.getWalkingRoutePlanningResult(latLng1, latLng2,
        new NetworkRequestManager.OnNetworkListener() {
            @Override
            public void requestSuccess(String result) {
                generateRoute(result);
            }

            @Override
            public void requestFail(String errorMsg) {
                Message msg = Message.obtain();
                Bundle bundle = new Bundle();
                bundle.putString("errorMsg", errorMsg);
                msg.what = 1;
                msg.setData(bundle);
                mHandler.sendMessage(msg);
            }
        });

Learn more details>>

Visit the official website of the Location Services Developer Alliance

Visit the official website of the Map Service Developer Alliance

Get the location services development guidance document

Get the map service development guidance document

Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee

Follow us to know the latest technical information of HMS Core for the first time~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙原生应用,和开发者共同成长。