Can you add a v2 google map to an android app without adding it via an xml file?
Asked
Active
Viewed 105 times
1 Answers
1
Yes you can using the support map fragment as follows:
public class LocationFragment extends SupportMapFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container, savedInstanceState);
return v;
}
}
and you can add this Location fragment to your container as follows:
LocationFragment mapFragment = new LocationFragment();
FragmentTransaction ft = getFragmentManager.beginTransaction();
ft.add(R.id.your_container_id, mapFragment);
ft.commit();

Karan_Rana
- 2,813
- 2
- 26
- 35
-
Sorry, I was expecting something like `MapView map = new MapView(this); lineaLayout.addView(map);`. Maybe you could explain your answer. Thanks. – user1923613 Mar 01 '13 at 18:29
-
1Support Map Fragment is a wrapper around a view of a map to automatically handle the necessary life cycle needs.It holds a mapview within itself – Karan_Rana Mar 01 '13 at 18:34
-
Uhmm, could you tell me how to use this? – user1923613 Mar 01 '13 at 18:45
-
You can also use MapView directly, but you'll need to manage the lifecycle yourself. (Support)MapFragment handles this for you. – Chris Broadfoot Mar 02 '13 at 23:15
-
@chris please read the question heading.he wants map without using xml. – Karan_Rana Mar 03 '13 at 02:51
-
@Karan_Rana I'm not sure what you mean. I wasn't disagreeing with your answer. It was purely a recommendation to use a Fragment over MapView. – Chris Broadfoot Mar 06 '13 at 18:17