OSMDroid integration with fragments

So, I was able to better understand how OSMDRoid works ... by extending the Activity.

public class POfflineMapView extends Activity implements LocationListener, MapViewConstants{ private MapView myOpenMapView; private OsmMapsItemizedOverlay mItemizedOverlay; private ResourceProxy mResourceProxy; private OverlayItem overlayItem; private ArrayList<OverlayItem> mItems = new ArrayList<OverlayItem>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext()); setContentView(R.layout.offline_map_activity); myOpenMapView = (MapView) findViewById(R.id.openmapview); myOpenMapView.getTileProvider().clearTileCache(); //.... code continues } } 

However, it’s hard for me to find examples to display using OSMDRoid Maptiles using fragment implementations (like SherlockMapFragment). Does anyone know how to implement this, or can provide a sample implementation guide for me?

I need to do this because I have an Activity container, and when I click on a specific button, I want to make a .replace () fragment to replace the container with an OSMDRoid map fragment, perhaps.

Thanks!

+4
source share
2 answers

Displaying an osmdroid map works the same way, regardless of whether you use it inside an Activity or Fragment. Just put it in your layout file and inflate it in your fragment, or create a MapView directly in the onCreateView method of your fragment.

To create a fragment containing the default map, you can do something like this:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return new MapView(getActivity(), 256); } 

in your fragment class.

If you want to use the layout that your MapView contains, you can do something similar in your fragment:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.offline_map_activity, null); myOpenMapView = v.findViewById(R.id.openmapview); return v; } 
+1
source

Create the onCreateView method in the Snippet (change the RelativeLayout to your layout type):

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { RelativeLayout rl = (RelativeLayout) inflater.inflate(offline_map_activity, container, false); myOpenMapView = (MapView) rl.findViewById(R.id.openmapview); //.... code continues return rl; } 
+1
source

All Articles