地图Android SDK是一套Android系统地图开发调用的接口,地图数据可以覆盖海外绝大多数国家,支持多种语言。地图采用WGS84 GPS坐标系,满足绝大多数海外地图开发的需求。您可以轻松地在自己的Android应用中加入地图相关的功能,包括:
您可通过地图服务SDK可以开发地图相关的App,比如:
集成Map Kit能力,您需要完成以下准备工作:
1. - 登录AppGallery Connect网站,选择"我的项目"。
2. - 在项目列表中找到您的项目,在项目下的应用列表中选择需要开通地图服务的应用。
3. - 前往"项目设置 > API管理"中开启"地图服务"权限。
dependencies {
implementation 'com.huawei.hms:maps:{version}'
}
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.huawei.hms.maps.MapView
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="51"
map:cameraTargetLng="10"
map:cameraZoom="8.5"
map:mapType="normal"
map:uiCompass="true"
map:uiZoomControls="true" />
</LinearLayout>
package com.wz.android.mapdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.huawei.hms.maps.HuaweiMap;
import com.huawei.hms.maps.MapView;
import com.huawei.hms.maps.OnMapReadyCallback;
/**
* map activity entrance class
*/
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final String TAG = "MapViewDemoActivity";
// HUAWEI map
private HuaweiMap hMap;
private MapView mMapView;
private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get mapview instance
mMapView = findViewById(R.id.mapView);
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
}
// please replace "Your API key" with api_key field value in
// agconnect-services.json
MapsInitializer.setApiKey("Your API key");
mMapView.onCreate(mapViewBundle);
// get map instance
mMapView.getMapAsync(this);
}
@Override
public void onMapReady(HuaweiMap map) {
// get map instance in a callback method
Log.d(TAG, "onMapReady: ");
hMap = map;
}
}
Kotlin示例代码如下:
package com.huawei.mapkitsample
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.huawei.hms.maps.CameraUpdateFactory
import com.huawei.hms.maps.HuaweiMap
import com.huawei.hms.maps.MapView
import com.huawei.hms.maps.OnMapReadyCallback
import com.huawei.hms.maps.model.LatLng
import com.huawei.hms.maps.util.LogM
/**
* map activity entrance class
*/
class MapViewDemoActivity : AppCompatActivity(), OnMapReadyCallback {
// HUAWEI map
private var hMap: HuaweiMap? = null
private var mMapView: MapView? = null
companion object {
private const val TAG = "MapViewDemoActivity"
private const val MAPVIEW_BUNDLE_KEY = "MapViewBundleKey"
}
override fun onCreate(savedInstanceState: Bundle?) {
LogM.d(TAG, "onCreate:")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_mapview_demo)
mMapView = findViewById(R.id.mapView)
var mapViewBundle: Bundle? = null
if (savedInstanceState != null) {
mapViewBundle =
savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY)
}
mMapView?.apply {
onCreate(mapViewBundle)
getMapAsync(this@MapViewDemoActivity)
}
}
override fun onMapReady(map: HuaweiMap) {
Log.d(TAG, "onMapReady: ")
hMap = map
}
}
@Override
protected void onStart() {
super.onStart();
mMapView.onStart();
}
@Override
protected void onStop() {
super.onStop();
mMapView.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
protected void onPause() {
mMapView.onPause();
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
Kotlin示例代码如下:
override fun onStart() {
super.onStart()
mMapView?.onStart()
}
override fun onStop() {
super.onStop()
mMapView?.onStop()
}
override fun onDestroy() {
super.onDestroy()
mMapView?.onDestroy()
}
override fun onPause() {
mMapView?.onPause()
super.onPause()
}
override fun onResume() {
super.onResume()
mMapView?.onResume()
}
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
Kotlin示例代码如下:
private fun hasPermissions(context: Context, vararg permissions: String): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
for (permission in permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false
}
}
}
return true
}
使用Android Studio自带的Gradle编译工具,双击"installDebug",即可安装Demo。
打开开发调试版本的地图Demo后,查看地图是否能够展示。
干得好,您已经成功完成了codelab并学到了: