HMS Core-关于Map Kit和Site Kit中API密钥的用法 地图套件网站套件

问题描述

背景

我试图仅使用HMS来构建类似于Google Map应用程序的地图应用程序。我证明只有HMS才能做到。

在构建应用程序时,我遇到了一个问题。当然我终于解决了。但是我想分享我的经验。

目标功能

发送API密钥

地图套件和网站套件需要

API密钥。以下是使用官方文档中描述的API密钥的方法

地图套件

方法1

// In the entrance class (inherited from android.app.Application) of the app,// call the setApiKey method in the overridden onCreate() method. 
public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // Set the API key.
        MapsInitializer.setApiKey("Your API Key");
    }
}

方法2

// Send API Key in Fragment or in MapView
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG,"onCreate: ");
        super.onCreate(savedInstanceState);
        // Set the API key before calling setContentView.
        MapsInitializer.setApiKey("Your API Key");
        setContentView(R.layout.basic_demo);
    }

基本上,只需运行MapsInitializer.setApiKey(“ Your API Key”)

网站套件

// Declare a SearchService object.
private SearchService searchService; 
// Instantiate the SearchService object.
searchService = SearchServiceFactory.create(this,"API key");

测试结果(失败)

以下是结果。

地图能够显示

但是,关键字搜索无效。

原因

原因是Site Kit的API密钥。 Map Kit使用的API密钥未经编码,但是Site Kit需要编码的API密钥。

测试结果(成功)

使用编码的API密钥,然后将其发送到Site Kit,如下所示。

searchService = SearchServiceFactory.create(this,URLEncoder.encode("API key","utf-8"));

显示关键字搜索结果

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)