The Map Java SDK for HarmonyOS provides Java APIs for HarmonyOS 2.0 or later. The map data covers most countries and regions outside the Chinese mainland, and supports multiple languages. Map Kit uses the WGS 84 GPS coordinate system, which can meet most map development requirements outside the Chinese mainland. You can easily add map-related functions in your HarmonyOS app, including:
In this codelab, you can develop map-related apps, for example:
To integrate Map Kit, you must complete the following preparations:




dependencies {
implementation 'com.huawei.hms:maps-harmony:{version}'
}
<!–Allow the app to use the map service bridging capability–>
"allowClassMap": true
b) Declare the following permissions in the reqPermissions block in the config.json file:
<!--Allow the app to use network sockets.-->
ohos.permission.INTERNET
<!--Allow the app to query the network status.-->
ohos.permission.GET_NETWORK_INFO
The sample code is as follows:
{
"module": {
"allowClassMap": true,
"reqPermissions": [
{
"name": "ohos.permission.INTERNET",
"reason": "access internet",
"usedScene": {
"ability": [
"com.mycamera.MainAbility"
],
"when": "always"
}
},
{
"name": "ohos.permission.GET_NETWORK_INFO",
"reason": "GET_NETWORK_INFO",
"usedScene": {
"ability": [
"com.mycamera.MainAbility"
],
"when": "always"
}
}
]
}
}
public void onStart(Intent intent) {
super.onStart(intent);
CommonContext.setContext(this);
MapView mapView = new MapView(this);
// Create a MapView object.
mapView.onCreate();
// Obtain a HuaweiMap object.
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(HuaweiMap huaweiMap) {
HuaweiMap mHuaweiMap = huaweiMap;
mHuaweiMap.setOnMapClickListener(new OnMapClickCallback() {
@Override
public void onMapClick(Object object) {
new ToastDialog(CommonContext.getContext()).setText("onMapClick ").show();
}
});
}
});
// Create a Layout object.
ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
PositionLayout myLayout = new PositionLayout(this);
myLayout.setLayoutConfig(config);
ShapeElement element = new ShapeElement();
element.setShape(ShapeElement.RECTANGLE);
element.setRgbColor(new RgbColor(255, 255, 255));
// Load the MapView object.
myLayout.addComponent(mapView);
super.setUIContent(myLayout);
}
Go to Run > Run ‘module name' in DevEco Studio, click , or press Shift+F10 to run your app. Wait for DevEco Studio to build and install your app on the phone or tablet. The phone or tablet will automatically run your app once the installation is finished.
Open the developed app and check whether it can display a map properly.
Well done. You have successfully completed this codelab and learned how to: