这里将SDK映射为反应本机组件

时间:2020-03-19 13:23:39

标签: android react-native

我对Here map sdk有问题。我试图从本机端创建一个组件,并从本机端调用。但是我有这个问题。我无法初始化地图。 ScreenShot

我从仿真器中收到此错误。

这些是我的代码:

HereMapView.java:

public class HereMapView extends MapViewLite {
    private static final String TAG = HereMapView.class.getSimpleName();
    private MapViewLite mapView;

    public HereMapView(ThemedReactContext reactContext) {
        super(reactContext);
        loadMapScene();
    }


    private void loadMapScene() {
        // Load a scene from the SDK to render the map with a map style.
        mapView.getMapScene().loadScene(MapStyle.NORMAL_DAY, new MapScene.LoadSceneCallback() {
            @Override
            public void onLoadScene(@Nullable MapScene.ErrorCode errorCode) {
                if (errorCode == null) {
                    mapView.getCamera().setTarget(new GeoCoordinates(52.530932, 13.384915));
                    mapView.getCamera().setZoomLevel(14);
                } else {
                    Log.d(TAG, "onLoadScene failed: " + errorCode.toString());
                }
            }
        });
    }


    @Override
    public void onPause() {
        super.onPause();
        this.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        this.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.onDestroy();
    }

}

HereMapManager.java:

public class HereMapManager  extends ViewGroupManager<HereMapView> {
    private static final String TAG = HereMapManager.class.getSimpleName();
    static final String REACT_CLASS = "HereMapView";
    public Activity activity;
    public HereMapManager(Activity activity){
        this.activity = activity;
    }
    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Override
    protected HereMapView createViewInstance(ThemedReactContext reactContext) {
        return new HereMapView(reactContext);
    }
}

HereMapModule.java:

public class HereMapModule extends ReactContextBaseJavaModule {
    private static final String TAG = HereMapModule.class.getSimpleName();

    HereMapModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "HereMapView";
    }
    public Activity getActivity() {
        return getCurrentActivity();
    }

    public static void closeQuietly(Closeable closeable) {
        if (closeable == null) return;
        try {
            closeable.close();
        } catch (IOException ignored) {
        }
    }
}

HereMapPackage:

public class HereMapPackage implements ReactPackage {
    protected Activity activity;
    public HereMapPackage() {
    }

    public HereMapPackage(Activity activity) {
        this.activity = activity;
    } // backwards compatibility
    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
        return Arrays.<NativeModule>asList(new HereMapModule(reactContext));
    }

    public List<Class<? extends JavaScriptModule>> createJSModules() {
        return Collections.emptyList();
    }

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        //HereMapManager mapManager = new HereMapManager(reactContext);
        HereMapManager mapManager = new HereMapManager(this.activity);
        return Arrays.<ViewManager>asList(mapManager);
    }
}

然后将软件包添加到: MainAplication.java:

@Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
            packages.add(new HereMapPackage());
          return packages;
        }

具有此功能。

然后我从本机端调用此代码:

App.js:

const HereMapView = requireNativeComponent('HereMapView');
const App: () => React$Node = () => {
  return (
    <SafeAreaView style={styles.screen}>
      <View style={styles.screen}>
        <HereMapView style={{flex : 1}} />
      </View>
    </SafeAreaView>
  );
};

能帮我吗?我为此停留了2个星期。

0 个答案:

没有答案