Custom controls over mapview

313 views
Skip to first unread message

Alain Márquez Pérez

unread,
May 25, 2016, 9:39:40 AM5/25/16
to mapsforge-dev
Hello everyone,I'm a rookie on this, need some help with my layout, this is it... plz helppp
my goal is to insert a floating button over the map view, or inserting another custom controls, can't find any help abt this....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:src="@android:drawable/ic_menu_mylocation" />

<org.mapsforge.map.android.view.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

</RelativeLayout>
what can i do
im using mapsforge 0.6 version
in the design view looks like this,
but when i build the app the floating button or any other control doesn't show...

Auto Generated Inline Image 1
Auto Generated Inline Image 2

Emux

unread,
May 25, 2016, 9:46:10 AM5/25/16
to mapsfo...@googlegroups.com
Have a look at our Android Samples app, there is a layout with custom controls on top of map.

Regarding your layout, try putting the button after you add the map view in xml.

--
Emux

Alain Márquez Pérez

unread,
May 25, 2016, 9:55:51 AM5/25/16
to mapsforge-dev
hi emux thanks for help, but i cant reach the samples git it will help me a lot, i dont know what happens, can i get them by somewhere else??

Emux

unread,
May 25, 2016, 9:58:40 AM5/25/16
to mapsfo...@googlegroups.com
What do you mean?

GitHub is accessible, you can check Samples also via browser:
https://github.com/mapsforge/mapsforge/tree/master/mapsforge-samples-android

--
Emux

Alain Márquez Pérez

unread,
May 25, 2016, 10:27:14 AM5/25/16
to mapsforge-dev
i reached there at last...
i adapted the layout sample and still the same, what can i do???
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<org.mapsforge.samples.android.rotation.RotateView
android:id="@+id/rotateView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<org.mapsforge.map.android.view.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</org.mapsforge.samples.android.rotation.RotateView>

<org.mapsforge.samples.android.scalebar.MapScaleBarView
android:id="@+id/mapScaleBarView"
        android:layout_width="wrap_content"
android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp" />

<ImageButton
android:id="@+id/zoomOutButton"
        android:layout_width="wrap_content"
android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@android:color/transparent"
android:src="@drawable/radiobutton_desactivado" />

<ImageButton
android:id="@+id/zoomInButton"
        android:layout_width="wrap_content"
android:layout_height="wrap_content"
        android:layout_above="@id/zoomOutButton"
android:layout_alignParentRight="true"
android:background="@android:color/transparent"
android:src="@drawable/radiobuton_activado" />

</RelativeLayout>

and my code(u helped me on this...)
package com.example.alain.entrecalles;

import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;

import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
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.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {

// name of the map file in the external storage
private static final String MAPFILE = "cuba.map";

private MyLocationOverlay myLocationOverlay;
private TileCache tileCache;
private TileRendererLayer tileRendererLayer;
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(false);
this.mapView.getMapZoomControls().setZoomLevelMin((byte) 10);
this.mapView.getMapZoomControls().setZoomLevelMax((byte) 20);



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

// tile renderer layer using internal render theme
MapDataStore mapDataStore = new MapFile(getMapFile());
this.tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
this.mapView.getModel().mapViewPosition, false, 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);

Drawable drawable = getResources().getDrawable(R.drawable.marker_red);
Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
// create the overlay and tell it to follow the location
this.myLocationOverlay = new MyLocationOverlay(this, mapView.getModel().mapViewPosition, bitmap);
this.mapView.getLayerManager().getLayers().add(this.myLocationOverlay);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_scrolling, menu);


return true;
}


@Override
protected void onResume() {
super.onResume();
this.mapView.getModel().mapViewPosition.setCenter(new LatLong(21.847037, -78.75886));
this.mapView.getModel().mapViewPosition.setZoomLevel((byte) 12);

this.myLocationOverlay.setSnapToLocationEnabled(true);
this.myLocationOverlay.enableMyLocation(true);

}

@Override
protected void onStart() {
super.onStart();
}

@Override
protected void onDestroy() {
this.mapView.destroyAll();
AndroidGraphicFactory.clearResourceMemoryCache();
super.onDestroy();
}


private File copyFileToSdcard(int resid, String filename, String sdcardDirectoryName) {
Log.d("HAL", "copyFileToSdcard()");
String dirpath = Environment.getExternalStorageDirectory().getPath() + "/" + sdcardDirectoryName;
File dir = new File(dirpath);
if (dir.exists() == false) {
Log.d("HAL", "Creando directorio " + dir.getAbsolutePath());
dir.mkdir();
}

File file = new File(dirpath, filename);
try {
InputStream is = getResources().openRawResource(resid);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
// No fue posible crear el ficher0. ¿Está montado el sdcard?
Log.w("ExternalStorage", "Error de escritura " + file, e);
return null;
}
return file;
}

private File getMapFile() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

String filepath = Environment.getExternalStorageDirectory().getPath() + "/maps/cuba.map";
File file = new File("/mnt/sdcard/mapas/" + MAPFILE);
File mapfile = new File(filepath);
try {
if (mapfile.exists() == false) {
mapfile = copyFileToSdcard(R.raw.cuba, "cuba.map", "maps");
}

} catch (Exception e) {
Log.e("HAL", "Error" + e.getMessage());

}
return mapfile;
}



}


Emux

unread,
May 25, 2016, 12:13:04 PM5/25/16
to mapsfo...@googlegroups.com
- You don't need the RotateView and the MapScaleBarView.
They're meant for the specific Samples example with the (external) map rotation.
You can use only the MapView and the buttons.

- The issue arises because you want to use an xml layout, but you still create a MapView by code and set it as content view, thus skipping the xml.


this.mapView = new MapView(this);
setContentView(this.mapView);

You would need something like (put your xml name):

setContentView(R.layout.main_view);
this.mapView = (MapView) findViewById(R.id.mapView);

--
Emux

Alain Márquez Pérez

unread,
May 25, 2016, 1:11:44 PM5/25/16
to mapsforge-dev
Now it worked... i'll download the samples git to study... thanks emux

Reply all
Reply to author
Forward
0 new messages