我正在尝试使用带有标签栏的Actionbarsherlock,在其中一个点击中我想要一张地图。 我已经使用谷歌地图v2在sherlockfragment中成功创建了一个地图,但我希望以编程方式向地图添加标记。
public class Map_fragment extends SherlockFragment {
static final LatLng Cph = new LatLng(55.702355,12.436523);
private final static String MAP_TAG = "map";
private GoogleMap mMap;
SupportMapFragment mMapFragment;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
SupportMapFragment map = (SupportMapFragment) (getSherlockActivity().getSupportFragmentManager().findFragmentByTag(MAP_TAG));
if(map==null){
Log.d("MAP", "NULL");
}
if(map!=null){
Log.d("MAP", " " + map);
Log.d("GETMAP", " " + map.getMap());
}
//GoogleMap mMap = map.getMap();
//mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Cph, 14));
return inflater.inflate(R.layout.mapv2_layout, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
public void setupMap(){
Log.d("SUCCESS" , "SUCCESS");
}
}
我尝试了其他网站上的许多不同建议和堆栈溢出的帖子,但地图始终为空。
我在其他人的建议之后尝试过onCreate和onAttach,但没有运气。
我已经尝试了很长时间,我即将脱掉头发:(
答案 0 :(得分:0)
我通过手动将SherlockMapFragment类添加到ActionBarSherlock库来解决它,在此处找到http://code.google.com/p/opendatakit/source/browse/src/com/actionbarsherlock/app/SherlockMapFragment.java?spec=svn.tables.35872cc920b5668de433c6fc81efc86c4970bac1&repo=tables&r=0c1b0212e5b8bb5af8ce2b8e9357cd30e3870084
然后用以下几行我终于获得了非空的“gMap”
SupportMapFragment map;
map = (SupportMapFragment) (getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.map));
gMap = map.getMap();