0

我正在使用许多示例代码在 mapsforge 中制作一个简单的新标记。例如,我尝试过这些示例: https ://www.programcreek.com/java-api-examples/?api=org.mapsforge.map.layer.overlay.Marker 或这个: https ://stackoverflow.com/ a/27499732/5720180 但没有创建。谁能帮我在 mapsforge 离线地图上创建一个标记?

这是我的使用代码:

createPositionMarker(35.6505667,51.4465217);

  private void createPositionMarker(double paramDouble1, double paramDouble2){
    final LatLong localLatLong = new LatLong(paramDouble1, paramDouble2);
    org.mapsforge.core.graphics.Bitmap bmp = AndroidGraphicFactory.convertToBitmap(getResources().getDrawable(R.drawable.alerton150));
        MarkerMapsForge positionmarker = new MarkerMapsForge(localLatLong,bmp,0,0 );
   this.mapView.getLayerManager().getLayers().add(positionmarker);
}
4

1 回答 1

0

公共类 MainActivity 扩展 Activity {

private MyLocationNewOverlay mLocationOverlay;
private CompassOverlay mCompassOverlay;
// name of the map file in the external storage
private static final String MAP_FILE = "iran.map";
private MapView mapView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidGraphicFactory.createInstance(this.getApplication());

    this.mapView = new MapView(this);
    setContentView(this.mapView);
    this.mapView.setClickable(true);
    this.mapView.getMapScaleBar().setVisible(true);
    this.mapView.setBuiltInZoomControls(true);
    this.mapView.setZoomLevelMin((byte) 6);
    this.mapView.setZoomLevelMax((byte) 20);

    // create a tile cache of suitable size
    TileCache tileCache = AndroidUtil.createTileCache(this, "mapcache",
            mapView.getModel().displayModel.getTileSize(), 1f,
            this.mapView.getModel().frameBufferModel.getOverdrawFactor());
    Log.i("LOGO", Environment.getExternalStorageDirectory().toString());

    //ask for the permission in android M
    int permission = ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "Permission to record denied");

        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Permission to access the SD-CARD is required for this app to Download PDF.")
                    .setTitle("Permission required");

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int id) {
                    Log.i(TAG, "Clicked");
                    makeRequest();
                }
            });

            AlertDialog dialog = builder.create();
            dialog.show();

        } else {
            makeRequest();
        }
    }
    File test = new File(Environment.getExternalStorageDirectory(), "payam_directory");
    if (!test.exists()) {
        try {
            if (test.mkdir()) {


            } else {

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {

    }
    //  tile renderer layer using internal render theme
    Configuration.getInstance().setUserAgentValue(getPackageName());
    MapDataStore mapDataStore = new MapFile(new File(Environment.getExternalStorageDirectory() + "/payam_directory/" + MAP_FILE));
    TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
            this.mapView.getModel().mapViewPosition, AndroidGraphicFactory.INSTANCE);
    tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.OSMARENDER);


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

    this.mapView.setCenter(new LatLong(37.519101, 45.062364));
    this.mapView.setZoomLevel((byte) 2);


    Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(getResources().getDrawable(R.drawable.m));
    bitmap.incrementRefCount();
    Marker marker = new Marker(new LatLong(37.519101, 45.062364), bitmap, 0, -bitmap.getHeight() / 2) {
        @Override public boolean onTap(LatLong geoPoint, Point viewPosition, Point tapPoint) {
            if (contains(viewPosition, tapPoint)) {
                Toast.makeText(MainActivity.this, "Urmia, payamasli", Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }
    };
    mapView.getLayerManager().getLayers().add(marker);
}

protected void makeRequest() {
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            REQUEST_WRITE_STORAGE);
}

}

于 2018-10-27T19:09:24.407 回答