3

Background

I tried to use only HMS to build a map app similar to Google Map app. I proved that only HMS is enough to do it.

When I was building the app, I faced a problem. Of course I solved it at last. But I want to share my experience.

Target Function

Send API Key

API Key is need for Map Kit and Site Kit. The following is how to use API key described in official document.

Map Kit

Method 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");
    }
}

Method 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);
    }

Basically, just run MapsInitializer.setApiKey("Your API Key")

Site Kit

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

Test Result (Failed)

The following is the result.

Map is able to show.

However, keyword search is not working.

Reason

The reason is the API key of Site Kit. Map Kit uses API key without encoding, but Site Kit needs encoded API key.

Test Result (Succeeded)

Using encoded API key and send it to Site Kit as following.

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

Keyword search result is displayed.

Raymond
  • 61
  • 1

0 Answers0