如何用華為位置服務實現搜索位置返回父子節點信息


當你在使用位置搜索時,父子節點信息可以幫助您解鎖更豐富的位置結果,讓您的出行更便捷。比如當你在搜索機場不僅會返回機場的信息,還會返回航站樓、停車場、出入口等信息,為用戶提供更場景化的位置結果,滿足用戶探索大型場所的需求。

本文介紹了如何使用華為位置服務實現搜索位置返回父子節點信息,以下是具體開發步驟

1. 開發前准備

您需要完成必要的開發准備工作,同時請確保您的工程中已經配置HMS Core SDK的Maven倉地址,並且完成了本服務的SDK集成。

1.1 在項目級build.gradle文件中配置maven倉地址。

buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://developer.huawei.com/repo/' }
    }
    //配置AGC插件
    dependencies {
        classpath "com.android.tools.build:gradle:3.3.2"
    }
}
allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://developer.huawei.com/repo/' }
    }
}

1.2 在應用級build.gradle中引入SDK。

ependencies {

    implementation 'com.huawei.hms:site:4.0.0.202'

}

2. 開發步驟

2.1 創建搜索服務。

SearchService searchService = SearchServiceFactory.create(this, Utils.getApiKey());

2.2 開發者創建查詢結果處理類

“SearchResultListener”,該類實現SearchResultListener 接口,使用此類中的“onSearchResult(TextSearchResponse results)”方法獲取查詢結果並實現具體業務。

SearchResultListener<TextSearchResponse> resultListener = new SearchResultListener<TextSearchResponse>() {
    @Override
    public void onSearchResult(TextSearchResponse results) {

        Log.d(TAG, "onTextSearchResult: " + results.toString());
        List<Site> siteList;
        if (results == null || results.getTotalCount() <= 0 || (siteList = results.getSites()) == null || siteList.size() <= 0) {
            resultTextView.setText("Result is Empty!");
            return;
        }

       
        for (Site site : siteList) {

            // 開發者根據需要處理數據結果

            ....

 

            // 子節點數據
                        if ((site.getPoi() != null)) {
                ChildrenNode[] childrenNodes = poi.getChildrenNodes();

                // 開發者根據需要處理數據結果

                ....


            }
        }
    }

    @Override
    public void onSearchError(SearchStatus status) {
        resultTextView.setText("Error : " + status.getErrorCode() + " " + status.getErrorMessage());
    }
};

2.3 創建TextSearchRequest類,配置請求參數。

TextSearchRequest request = new TextSearchRequest();
String query = "Josep Tarradellas Airport";
request.setQuery(query);
Double lat = 41.300621;
Double lng = 2.0797638;
request.setLocation(new Coordinate(lat, lng));
// 設置獲取子節點信息
request.setChildren(true);

2.4 設置請求結果處理器,實現請求與結果處理器的綁定。

searchService.textSearch(request, resultListener);

這樣就實現了搜索位置返回父子節點信息 ,下面是實現效果圖,展示下此功能的強大吧。

欲了解更多詳情,請參閱:

華為開發者聯盟官網:https://developer.huawei.com/consumer/cn/hms/huawei-sitekit/?ha_source=hms1

獲取開發指導文檔:https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/android-sdk-introduction-0000001050158571?ha_source=hms1

參與開發者討論請到Reddit社區:https://www.reddit.com/r/HMSCore/

下載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/0201435810623210160?fid=18
作者:胡椒


免責聲明!

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



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