Simple path planning case sharing

Outline of this article

  1. Project Background

  2. Integration preparation

  3. Main code

  4. Achievement display

1. Function points used in this project:

  Map Service (Map Kit) provides you with a set of SDK for map development and call. The map data covers more than 200 countries and regions and supports hundreds of languages. It is convenient for you to easily integrate map-related functions in your application and improve users in all directions. Experience.

  Keyword search: Inquire about places such as tourist attractions, businesses, and schools through designated keywords and optional geographic scopes.

  Path planning: It is a set of walking, cycling, driving path planning and driving distance calculation interfaces provided in the form of HTTPS. It returns path query data in JSON format and provides path planning capabilities.

Two, integration preparation

  1. AGC account registration, project creation

(1) Register as a developer

Registered address:

https://developer.huawei.com/consumer/cn/service/josp/agc/index.html?ha_source=hms1

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

  1. Integrated Map + Site SDK

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

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

  • Configure the Maven warehouse 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
    }
}

Three, the main code and functions used in the project

  1. Text search: Search and display text content by implementing the textSearch function in Site Kit.
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);
  1. Walking path planning: Data callback is realized by calling the API interface in Map Kit and displayed on the map. Click to get API documentation .
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);
            }
        });

4. Project achievement display

Insert picture description here

For more details, please refer to:
Huawei Developer Alliance official website:https://developer.huawei.com/consumer/cn/hms?ha_source=hms1
Get the development guide document:https://developer.huawei.com/consumer/cn/doc/development?ha_source=hms1 To
participate in the developer discussion, please go to the Reddit community:https://www.reddit.com/r/HuaweiDevelopers/ To
download the demo and sample code, please go to Github:https://github.com/HMS-Core to
solve integration problems, please go to Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest


Original link:
https://developer.huawei.com/consumer/cn/forum/topic/0202436666167160224?fid=18
Author: pepper

Guess you like

Origin blog.51cto.com/14772288/2587577