HMS Map Kit不渲染?
HMS Map Kit is Not Rendering?
我正在尝试在我的项目中使用 HMS Map Kit,地图已加载但从未渲染
Gradle:应用程序
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.huawei.agconnect:agconnect-core:1.4.1.300'
implementation 'com.huawei.hms:maps:5.0.2.300'
}
apply plugin: 'com.huawei.agconnect'
构建:Gradle:
repositories {
google()
jcenter()
maven { url 'http://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {url 'http://developer.huawei.com/repo/'}
}
清单:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name=
"com.huawei.appmarket.service.commondata.permission.GET_COMMON_DATA"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Activity :
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final String MAP_VIEW_BUNDLE_KEY = "MapViewBundleKey";
private HuaweiMap hMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
}
MapView mapView = findViewById(R.id.mapView);
mapView.onCreate(mapViewBundle);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(HuaweiMap huaweiMap) {
hMap = huaweiMap;
hMap.setMyLocationEnabled(true);
hMap.getUiSettings().setMyLocationButtonEnabled(true);
hMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.864716, 2.349014), 10));
hMap.setOnMapLoadedCallback(new HuaweiMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
Log.i("==========>", "[Map] Loaded.");
}
});
}
}
请注意:我下载了 agconnect-services.json 文件并将其添加到项目中,并将 SHA-256 证书指纹也添加到应用程序信息中,但我不知道我缺少什么?
这个问题可能有不同的原因。请检查如下:
- 检查HMS Core(APK)是否需要升级,定位权限是否设置为Always.
- 检查是否在AppGallery Connect中启用了Map Kit API。如果没有,启用它,下载.json文件替换代码中已有的,然后检查SHA256指纹是否正确。
- 调用 onStart()、onStop()、onResume()、onPause()、onDestroy()、onLowMemory()、onSaveInstanceState(Bundle outState) MapView class的方法在Activity/Fragment.[=43=的相应方法中]
- 在 Android 的 Map SDK 5.0.0.300 或更高版本中,您必须在初始化地图之前设置一个 API 键。
(1) 在项目入口class设置API键
// 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) 在Fragment或MapView.
中设置API键
@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);
- 目前HuaweiMap支持两种地图。确定您使用的类型。
MAP_TYPE_NORMAL:标准地图,显示道路、人工建筑物和河流等自然景观。
MAP_TYPE_NONE: 没有任何数据的空地图。
- 另一个原因可能是您的位置不受支持。有关支持位置的详细信息,请参阅docs。
@shirley 回答的另一种方法:
尝试在 ~/utils/MapUtils.java 中编辑您的 API_KEY
(以防您尝试 中的示例代码Huawei Codelabs (GitHub)) 或直接在你的入口class中添加你的API_KEY
(这与@shirley 的做法相同)像这样:
MapsInitializer.setApiKey("Your_API_KEY_Here")
我用的是HMS Core APK版本:5.2.0.303,没问题
如果有帮助,请告诉我。 :)
我正在尝试在我的项目中使用 HMS Map Kit,地图已加载但从未渲染
Gradle:应用程序
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.huawei.agconnect:agconnect-core:1.4.1.300'
implementation 'com.huawei.hms:maps:5.0.2.300'
}
apply plugin: 'com.huawei.agconnect'
构建:Gradle:
repositories {
google()
jcenter()
maven { url 'http://developer.huawei.com/repo/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {url 'http://developer.huawei.com/repo/'}
}
清单:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name=
"com.huawei.appmarket.service.commondata.permission.GET_COMMON_DATA"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Activity :
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final String MAP_VIEW_BUNDLE_KEY = "MapViewBundleKey";
private HuaweiMap hMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
}
MapView mapView = findViewById(R.id.mapView);
mapView.onCreate(mapViewBundle);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(HuaweiMap huaweiMap) {
hMap = huaweiMap;
hMap.setMyLocationEnabled(true);
hMap.getUiSettings().setMyLocationButtonEnabled(true);
hMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(48.864716, 2.349014), 10));
hMap.setOnMapLoadedCallback(new HuaweiMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
Log.i("==========>", "[Map] Loaded.");
}
});
}
}
请注意:我下载了 agconnect-services.json 文件并将其添加到项目中,并将 SHA-256 证书指纹也添加到应用程序信息中,但我不知道我缺少什么?
这个问题可能有不同的原因。请检查如下:
- 检查HMS Core(APK)是否需要升级,定位权限是否设置为Always.
- 检查是否在AppGallery Connect中启用了Map Kit API。如果没有,启用它,下载.json文件替换代码中已有的,然后检查SHA256指纹是否正确。
- 调用 onStart()、onStop()、onResume()、onPause()、onDestroy()、onLowMemory()、onSaveInstanceState(Bundle outState) MapView class的方法在Activity/Fragment.[=43=的相应方法中]
- 在 Android 的 Map SDK 5.0.0.300 或更高版本中,您必须在初始化地图之前设置一个 API 键。
(1) 在项目入口class设置API键
// 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) 在Fragment或MapView.
中设置API键 @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);
- 目前HuaweiMap支持两种地图。确定您使用的类型。 MAP_TYPE_NORMAL:标准地图,显示道路、人工建筑物和河流等自然景观。 MAP_TYPE_NONE: 没有任何数据的空地图。
- 另一个原因可能是您的位置不受支持。有关支持位置的详细信息,请参阅docs。
@shirley 回答的另一种方法:
尝试在 ~/utils/MapUtils.java 中编辑您的 API_KEY
(以防您尝试 中的示例代码Huawei Codelabs (GitHub)) 或直接在你的入口class中添加你的API_KEY
(这与@shirley 的做法相同)像这样:
MapsInitializer.setApiKey("Your_API_KEY_Here")
我用的是HMS Core APK版本:5.2.0.303,没问题
如果有帮助,请告诉我。 :)