为什么不在android mapsforge地图中显示任何标记?

时间:2018-05-21 09:22:54

标签: android marker mapsforge

我正在使用许多示例代码在mapsforge中创建一个简单的新标记。 例如,我试过这些样本: https://www.programcreek.com/java-api-examples/?api=org.mapsforge.map.layer.overlay.Marker 或者这个: https://stackoverflow.com/a/27499732/5720180 但没有创造。 任何人都可以帮我在mapsforge离线地图上创建标记吗? 谢谢你提前。

这是我的使用代码:

randomQuote = () => {
  this.setState({
    num: Math.floor(Math.random() * this.state.quotes.length)
  })
}

1 个答案:

答案 0 :(得分:0)

公共类MainActivity扩展了活动{

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);
}

}