HMS地图套件未渲染



我正试图在我的项目中使用HMS地图工具包,地图已加载,但从未呈现

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'

构建:渐变:

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"/>

活动:

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证书指纹添加到应用程序信息中,但我不知道我缺少什么?

这个问题可能有不同的原因。请检查如下:

  1. 检查HMS Core(APK(是否需要升级,以及位置权限是否设置为Always
  2. 检查是否在AppGallery Connect中启用了Map Kit API。如果没有,请启用它,下载.json文件以替换代码中现有的文件,然后检查SHA256指纹是否正确
  3. Activity/Fragment的相应方法中调用MapView类的onStart((onStop((onResume(
  4. 在Android版Map SDK 5.0.0.300或更高版本中,在初始化映射之前,必须设置API密钥

(1(在项目的入口类中设置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( 在片段映射视图中设置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);
  1. 目前,HuaweiMap支持两种类型的地图。确定您正在使用的类型。MAP_TYPE_NORMAL:标准地图,显示道路、人工结构和河流等自然特征。MAP_TYPE_NONE:没有任何数据的空映射
  2. 另一个原因可能是您的位置不受支持。有关支持位置的详细信息,请参阅文档

@shirley答案的另一种方法:

请尝试在~/utils/MapUtils.java中编辑您的API_KEY(如果您尝试了华为Codelabs(GitHub(的示例代码(,或者直接在您的入门类中添加API_KEY(这与@shirley的方法相同(,如下所示:

MapsInitializer.setApiKey("Your_API_KEY_Here")

我使用了HMS Core APK版本:5.2.0.303,它运行良好。

如果有帮助,请告诉我。:(

最新更新