【问题标题】:HMS Map Kit is Not Rendering?HMS Map Kit 不渲染?
【发布时间】:2020-10-21 14:20:26
【问题描述】:

我正在尝试在我的项目中使用 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'

构建: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"/>

活动:

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

【问题讨论】:

  • 你好@Ali Adil,你能分享你的布局xml代码吗?地图和日志的屏幕截图会很有帮助:) (顺便说一下,正确的文件应该是 agconnect-services.json,你重命名了吗?)
  • 另外,您没有提到的一件事 - 您的 AppGallery Connect 控制台中是否启用了 Map Kit API?

标签: android kotlin huawei-mobile-services huawei-map-kit


【解决方案1】:

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

  1. 检查HMS Core(APK)是否需要升级,定位权限是否设置为Always
  2. 检查AppGallery Connect中是否启用了Map Kit API。如果没有,开启它,下载.json文件替换代码中已有的,然后检查SHA256指纹是否正确。
  3. 调用onStart()onStop()onResume()onPause()、MapView类对应方法中的strong>onDestroy()、onLowMemory()onSaveInstanceState(Bundle outState)方法活动/片段
  4. 在 Android 的 Map SDK 5.0.0.300 或更高版本中,您必须在初始化地图之前设置 API 密钥。

(1)在你的项目入口类中设置API key。

    // 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) 在FragmentMapView中设置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. 另一个原因可能是您的位置不受支持。有关支持的位置的详细信息,请参阅docs

【讨论】:

    【解决方案2】:

    @shirley 回答的另一种方法:

    尝试在 ~/utils/MapUtils.java 中编辑您的 API_KEY(如果您尝试了 Huawei Codelabs (GitHub) 中的示例代码)或直接添加您的API_KEY 在你的入口班(这与@shirley 的方法相同)是这样的:

    MapsInitializer.setApiKey("Your_API_KEY_Here")
    

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

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

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      相关资源
      最近更新 更多