1

尝试为我的 react-native android 应用程序创建本机模块。下面的代码是我使用 mapsforge 库渲染/显示地图的本机 java。

我需要帮助,我需要一个函数来返回应用程序的实例,如果可能的话,它位于扩展 LinearLayout 的类中。我需要调用该函数作为AndroidGraphicFactory.createInstance(InstanceOfTheApplication). 你认为我在做什么是可能的?

import android.content.Context;
import android.location.LocationManager;
import android.os.Environment;
import android.os.StrictMode;
import android.widget.LinearLayout;

import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.android.layers.MyLocationOverlay;
import org.mapsforge.map.android.util.AndroidUtil;
import org.mapsforge.map.android.view.MapView;
import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.model.MapViewPosition;
import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme;

import java.io.File;

public class CustomMapView extends LinearLayout {

    private Context context;
    private MapView mapView;
    private TileCache tileCache;
    private TileRendererLayer tileRendererLayer;
    private MyLocationOverlay myLocationOverlay;
    private MapViewPosition mapViewPosition;
    private LocationManager locationManager;
    private static final String map = "philippines.map";

    public CustomMapView(Context context) {

        super(context);

        // set context
        this.context = context;

        // need help here ...
        AndroidGraphicFactory.createInstance(this.getApplication());

        // inflate map.xml | setContentView
        inflate(this.context, R.layout.map, this);

        // create map layout
        create();

        // start map
        start();

    }

    /**
     * Create map layout
     * and controls.
     */
    private void create() {

        this.mapView = (MapView) findViewById(R.id.mapView);
        this.mapView.setClickable(true);
        this.mapView.getMapScaleBar().setVisible(true);
        this.mapView.setBuiltInZoomControls(true);
        this.mapView.getMapZoomControls().setZoomLevelMin((byte) 10);
        this.mapView.getMapZoomControls().setZoomLevelMax((byte) 20);

        // create a tile cache of suitable size
        this.tileCache = AndroidUtil.createTileCache(
            context, 
            "mapcache", 
            mapView.getModel().displayModel.getTileSize(), 
            1f, 
            this.mapView.getModel().frameBufferModel.getOverdrawFactor()
        );

    }

    /**
     * Start map
     */
    private void start() {

        this.mapView.getModel().mapViewPosition.setCenter(new LatLong(14.788405, 121.069563));
        this.mapView.getModel().mapViewPosition.setZoomLevel((byte) 12);

        // tile renderer layer using internal render theme
        MapDataStore mapDataStore = new MapFile(getMapFile());

        this.tileRendererLayer = new TileRendererLayer(
                tileCache,
                mapDataStore,
                this.mapView.getModel().mapViewPosition,
                false,
                true,
                true,
                AndroidGraphicFactory.INSTANCE
        );

        tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.OSMARENDER);

        // only once a layer is associated with a mapView the rendering starts
        this.mapView.getLayerManager().getLayers().add(tileRendererLayer);

    }

    /**
     * Get map file
     * 
     * @return
     */
    private File getMapFile() {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        // absolute path of internal Downloads folder
        String dirDownloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
        File file = new File(dirDownloads + "/" + map);

        return file;

    }

}
4

0 回答 0