MapView in Fragment ( Android 4.0 or higher)

天涯浪子 提交于 2019-12-17 19:28:30

问题


I've been looking for a while to find a good tutorial with code example for a MapView in Fragment for ICS.

Anyone have any links?


回答1:


Here is a book's sample application showing how to have a MapView in a Fragment in an API Level 11+ app. It's mostly just a MapActivity. Here are the key bits of the fragment that loads the MapView:

public class MapFragment extends Fragment {
  private MapView map=null;
  private MyLocationOverlay me=null;

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

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    map=new MapView(getActivity(), "0mjl6OufrY-tHs6WFurtL7rsYyEMpdEqBCbyjXg");
    map.setClickable(true);

    map.getController().setCenter(getPoint(40.76793169992044,
                                            -73.98180484771729));
    map.getController().setZoom(17);
    map.setBuiltInZoomControls(true);

    Drawable marker=getResources().getDrawable(R.drawable.marker);

    marker.setBounds(0, 0, marker.getIntrinsicWidth(),
                            marker.getIntrinsicHeight());

    map.getOverlays().add(new SitesOverlay(marker));

    me=new MyLocationOverlay(getActivity(), map);
    map.getOverlays().add(me);

    ((ViewGroup)getView()).addView(map);
  }

  // rest of fragment here
}



回答2:


I have answered to the same question here MapView in a Fragment (Honeycomb)



来源:https://stackoverflow.com/questions/9959072/mapview-in-fragment-android-4-0-or-higher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!