HMS Map Kit is Not Rendering?

17.9k Views Asked by At

I'm Trying to use HMS Map Kit in my project, the map is loaded but its never rendering

Gradle:app

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'

Build: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/'}

    }

Manifest :

 <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.");

            }
        });



    }
}

Please not : that i downloaded and added the agconnect-services.json file to the project and added the SHA-256 certificate fingerprint to the App informations also, but i don't know what Im missing ?

2

There are 2 best solutions below

0
On BEST ANSWER

There may be different causes of this problem. Please check as follows:

  1. Check whether HMS Core (APK) needs to be upgraded and whether the location permission is set to Always.
  2. Check whether the Map Kit API is enabled in AppGallery Connect. If not, enable it, download the .json file to replace the existing one in the code, and then check whether the SHA256 fingerprint is correct.
  3. Call the onStart(), onStop(), onResume(), onPause(), onDestroy(), onLowMemory(), and onSaveInstanceState(Bundle outState) methods of the MapView class in corresponding methods of Activity/Fragment.
  4. In the Map SDK 5.0.0.300 or later for Android, you must set an API key before initializing the map.

(1) Set the API key in the entrance class of your project.

    // 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) Set the API key in Fragment or MapView.

    @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. Currently, HuaweiMap supports two types of maps. Determine the type that you are using. MAP_TYPE_NORMAL: standard map, which shows roads, artificial structures, and natural features such as rivers. MAP_TYPE_NONE: empty map without any data.
  2. Another cause may be that your location is not supported. For details about supported locations, please refer to docs.
1
On

Another approach to @shirley's answer:

Try editing your API_KEY in ~/utils/MapUtils.java (in case you tried the Sample Code from Huawei Codelabs (GitHub)) or directly add your API_KEY in your entrance class (which is the same as @shirley's approach) like this:

MapsInitializer.setApiKey("Your_API_KEY_Here")

I used HMS Core APK Version: 5.2.0.303 and it worked fine.

Let me know if that helped. :)