android - HMS 核心 map 套件在我的 Android 应用程序上根本无法工作

标签 android huawei-mobile-services

我正在尝试在我的应用程序中使用华为HMS map 套件,我对整体 map 很陌生(无论是来自谷歌还是华为),我遵循了华为提供的文档和代码实验室中的教程并将我的代码放在一起,但是当我运行 map Activity 时,什么也没有出现,我得到的只是一个空白 Activity ,我在整个代码中编写了一些日志语句,并且只记录了其中一个,该语句放在最顶部创建方法。应用程序也不会崩溃。

这是我的代码,它可能有什么问题?


public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
 
    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_map);
 
        Log.i("TAG", "onCreate");
 
        //get mapview instance
        mMapView = findViewById(R.id.mapView);
 
        Bundle mapViewBundle = null;
        if (savedInstanceState != null) {
            mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
        }
 
        MapsInitializer.setApiKey(android.net.Uri.encode("my api key here"));
        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;
    }
 
    @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();
    }
 
    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;
    }
}

在代码之外,我已经确定了我的华为应用程序库连接配置、API key 等,以及 gradle 中所需的依赖项

我检查了我的 minSDK 并确保它也是 19。 仍然..没有任何作用

请帮助我

谢谢

最佳答案

<强>1。为什么会发生这种情况?

华为 map 服务支持的设备为华为手机。如果您使用 Google 手机,请转到步骤 2。

<强>2。怎么做?

如果你

look for a way which allows me to use both maps in the same application by detecting the device type and acting accordingly,

您可以使用G+H 解决方案。使用G+H方法,您可以维护一个代码库,并根据其中之一的可用性来决定是否使用GMS或HMS。

首先检查GMS和HMS是否可用。

public boolean  isGMS(){
        return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == com.google.android.gms.common.ConnectionResult.SUCCESS;
    }
public boolean  isHMS(){
        return HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(this) == com.huawei.hms.api.ConnectionResult.SUCCESS;
}

或者请引用:How to check Google Mobile Services enable in device or not?

其次,进入不同的代码分支,执行不同的 map 初始化任务。

if(isGMS()) {
init Google map and functions
} else if(isHMS()) {
init Huawei map and functions
}

希望对你有帮助!

关于android - HMS 核心 map 套件在我的 Android 应用程序上根本无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63323319/

相关文章:

java - 关闭应用程序时如何断开与设备的连接?

android - 如果我使用 .75 和 .25,为什么布局权重不起作用?

android - intelliJ 奇怪的启动错误

java - 设置SeekBar的宽度使 "swipe to unlock"效果

android - 华为 Android Logcat 不显示日志

java - 华为设备上的文本对齐问题

Java 静态变量变为空

android - 如何使用设备 token 发送下行消息

android - 添加华为套件报错 "Could not find com.huawei.hms:location:6.0.0.302"

cordova - 使用 Cordova 检测设备是否可以访问华为应用程序库