簡單的路徑規划案例分享


本文大綱

  1. 項目背景
  2. 集成准備
  3. 主要代碼
  4. 成果展示

一、本項目用到的功能點:

地圖服務(Map Kit)給您提供一套地圖開發調用的SDK,地圖數據覆蓋超過200個國家和地區,支持數百種語言,方便您輕松地在應用中集成地圖相關的功能,全方位提升用戶體驗。

關鍵字搜索:通過指定的關鍵字和可選的地理范圍,查詢諸如旅游景點、企業和學校之類的地點。

路徑規划: 是一套以HTTPS形式提供的步行、騎行、駕車路徑規划以及行駛距離計算接口,通過JSON格式返回路徑查詢數據,提供路徑規划能力。

二、集成准備

1. AGC賬號注冊,項目創建

  1. 注冊成為開發者

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

在這里插入圖片描述

  1. 創建應用,添加sha256,開啟map/site開關,下載json文件

在這里插入圖片描述

2. 集成Map + Site SDK

  1. 將“agconnect-services.json”文件拷貝到應用級根目錄下
  • 在“allprojects > repositories”中配置HMS Core SDK的Maven倉地址。
  • 在“buildscript > repositories”中配置HMS Core SDK的Maven倉地址。
  • 如果App中添加了“agconnect-services.json”文件則需要在“buildscript > dependencies”中增加agcp配置。
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()
     }
 }
  1. 在“dependencies ”中添加如下編譯依賴
dependencies {
     implementation 'com.huawei.hms:maps:{version}'
     implementation 'com.huawei.hms:site:{version}'
 }
  1. 在文件頭添加配置
apply plugin: 'com.huawei.agconnect'
  1. 在android中配置簽名。將生成簽名證書指紋生成的簽名文件復制到您工程的app目錄下,並在“build.gradle”文件中配置簽名。
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. 文本搜索:通過實現Site Kit中的textSearch功能實現文本內容搜索並展示出來。
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. 步行路徑規划:通過調用Map Kit 中的API接口實現數據回調並呈現在地圖上。點擊可獲取API文檔。
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);
            }
        });

四、項目成果展示

在這里插入圖片描述

欲了解更多詳情,請參閱:
華為開發者聯盟官網:https://developer.huawei.com/consumer/cn/hms?ha_source=hms1
獲取開發指導文檔:https://developer.huawei.com/consumer/cn/doc/development?ha_source=hms1
參與開發者討論請到Reddit社區:https://www.reddit.com/r/HuaweiDevelopers/
下載demo和示例代碼請到Github:https://github.com/HMS-Core
解決集成問題請到Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest


原文鏈接:https://developer.huawei.com/consumer/cn/forum/topic/0202436666167160224?fid=18

原作者:胡椒


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM