Issue
I want to instantiate a MapView programmatically. I'm using Osmdroid. I wrote this, but map is not displayed (with or without setting the parameters)! What's wrong?
LinearLayout contentLayout = (LinearLayout) findViewById(R.id.contentLayout);
m_mapView = new MapView(this, 10);
org.osmdroid.views.MapView.LayoutParams mapParams = new org.osmdroid.views.MapView.LayoutParams(
org.osmdroid.views.MapView.LayoutParams.MATCH_PARENT,
org.osmdroid.views.MapView.LayoutParams.MATCH_PARENT,
null, 0, 0, 0);
m_mapView.setBuiltInZoomControls(true);
m_mapView.setMultiTouchControls(false);
mapController = this.m_mapView.getController();
mapController.setZoom(MAP_DEFAULT_ZOOM);
m_mapView.getController().setCenter( new GeoPoint(MAP_DEFAULT_LATITUDE, MAP_DEFAULT_LONGITUDE));
m_mapView.invalidate();
contentLayout.addView(m_mapView,mapParams);
Solution
I really want to help you because your post helped me with my problem displaying an osmdroid map programmatically: I was adding the map view to a linear layout as you did, but was not including the layout parameters as you did, and the map was coming up blank. Thank you!
I think your problem is that you did not specify a tile source. Here's the code I used, and it successfully drew the map. It's identical to yours except for adding a tile source, replacing your constants with values, and for changing your variable name mapController to m_mapController, which I assume was defined the same was as your mapView. I just didn't want anyone to think it was defined within the method and not shown.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout contentLayout = (LinearLayout) findViewById(R.id.contentLayout);
m_mapView = new MapView(this, 10);
m_mapView.setTileSource(TileSourceFactory.MAPNIK);
org.osmdroid.views.MapView.LayoutParams mapParams = new org.osmdroid.views.MapView.LayoutParams(
org.osmdroid.views.MapView.LayoutParams.MATCH_PARENT,
org.osmdroid.views.MapView.LayoutParams.MATCH_PARENT,
null, 0, 0, 0);
m_mapView.setBuiltInZoomControls(true);
m_mapView.setMultiTouchControls(false);
m_mapController = this.m_mapView.getController();
m_mapController.setZoom(15);
m_mapView.getController().setCenter( new GeoPoint(51496994, -134733));
m_mapView.invalidate();
contentLayout.addView(m_mapView, mapParams);
}
I used this helpful page when I started with osmdroid, and it makes a good sanity check when I'm off in the weeds with my own modifications.
I hope this helps... thanks again for the hint you provided me with!
Answered By - strangeluck
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.