Buttons in mapsforge

293 views
Skip to first unread message

Medyn

unread,
Sep 23, 2016, 11:49:08 AM9/23/16
to mapsforge-dev
hi ,i'm trying to put button over the mapsforge maps 
But it displays an error 

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) 10);
this.mapView.setZoomLevelMax((byte) 20);
tileCache = AndroidUtil.createTileCache(this, "mapcache",
mapView.getModel().displayModel.getTileSize(), 1f,
this.mapView.getModel().frameBufferModel.getOverdrawFactor());
this.mapView.setCenter(new LatLong(14.499502, 44.423186));
this.mapView.setZoomLevel((byte) 12);

MapDataStore mapDataStore = new MapFile(new File(Environment.getExternalStorageDirectory(), MAP_FILE));
tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
this.mapView.getModel().mapViewPosition, AndroidGraphicFactory.INSTANCE);
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.OSMARENDER);
this.mapView.getLayerManager().getLayers().add(tileRendererLayer);
btnShowLocation = (Button) findViewById(R.id.location);

btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSlocation(MainActivity.this);

// check if GPS enabled
if(gps.canGetLocation()){

double latitude = gps.getLatitude();
double longitude = gps.getLongitude();

// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}

}
});

                                                            FATAL EXCEPTION: main
                                                                                  Process: com.example.lifemakers.mapsforge, PID: 29270
                                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lifemakers.mapsforge/com.example.lifemakers.mapsforge.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View, int)' on a null object reference
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:157)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:148)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5527)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View, int)' on a null object reference
                                                                                      at com.example.lifemakers.mapsforge.MainActivity.start_map(MainActivity.java:57)
                                                                                      at com.example.lifemakers.mapsforge.MainActivity.per(MainActivity.java:156)
                                                                                      at com.example.lifemakers.mapsforge.MainActivity.onCreate(MainActivity.java:47)
                                                                                      at android.app.Activity.performCreate(Activity.java:6272)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:157) 
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                      at android.os.Looper.loop(Looper.java:148) 
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5527) 
                                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

Emux

unread,
Sep 23, 2016, 12:31:17 PM9/23/16
to mapsfo...@googlegroups.com
That code does not help us to see the layout and what is on exception line.

Nevertheless if you want to have the map view and above custom buttons, you can setContentView a RelativeLayout, and add in it the map view and then the buttons.

See map rotation example in Samples how it works.

--
Emux
Message has been deleted

yogesh10446

unread,
Oct 13, 2016, 1:21:50 PM10/13/16
to mapsforge-dev
Rather than programmatically  adding your buttons to the map view, its better that you use the xml files to add other views to the mapview. Its much easier and you can perceive the look of your map in the preview.

activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent">

   
<com.example.mapsforge.views.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" />

   
</com.example.mapsforge.views.RotateView>

   
<LinearLayout
       
android:id="@+id/zoom_controls"
       
android:orientation="vertical"
       
android:layout_alignParentRight="true"
       
android:padding="10dp"
       
android:layout_centerVertical="true"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content">

       
<ImageButton
           
android:id="@+id/map_zoom_in"
           
android:src="@drawable/ic_icon_add_holo_light"
           
android:layout_gravity="end"
           
android:padding="3dp"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content" />

       
<ImageButton
           
android:id="@+id/map_zoom_out"
           
android:src="@drawable/ic_icon_minus_holo_light"
           
android:layout_gravity="end"
           
android:layout_marginTop="5dp"
           
android:padding="3dp"
           
android:layout_width="wrap_content"
           
android:layout_height="wrap_content" />

   
</LinearLayout>


   
<TextView
       
android:id="@+id/attribution"
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
       
android:layout_alignParentBottom="true"
       
android:layout_marginBottom="1dp"
       
android:gravity="center"
       
android:singleLine="true"
       
android:visibility="gone"
       
android:text="Map data © OpenStreetMap contributors"
       
android:textColor="#000"
       
android:textSize="4pt" />

   
<com.example.mapsforge.views.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_alignParentStart="true"
       
android:layout_margin="5dp" />

</RelativeLayout>


And then in the activity, as always


@Override
protected void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
   
//create tht graphic factory required by the mapsforge library at the start of the activity
   
AndroidGraphicFactory.createInstance(getApplication());
    setContentView
(R.layout.activity_map_with_drawer);
   
//get necessary views
   
this.mapview = (MapView) findViewById(R.id.mapView);
   
//Other lines of code
   
ImageButton zoomIn = (ImageButton)findViewById(R.id.map_zoom_in);
   
...
   
...
   
...

}
Reply all
Reply to author
Forward
0 new messages