问题描述
背景
我已成功地将新地图 api v2.0 添加到 ViewPager 并且地图加载正常.我正在使用 FragmentViewPager.但是,当我尝试添加标记时遇到了麻烦,因为我无法按标签获取片段,因为我使用的 ViewPager 来自兼容性库并在 XML 中定义,这对我隐藏"了标签.
I have successfuflly added the new map api v2.0 to a ViewPager and the map loads fine. I am using the FragmentViewPager. I run into trouble however when trying to add markers as I cannot get the Fragment by tag as the ViewPager I am using is from the compatibility library and defined in XML, and this "hides" the tag from me.
问题
如何将带有标记 SupportMapFragment
的新 Map v2 加载到使用 FragmentPagerAdapter
的 ViewPager
中.
How can I load the new Map v2 with markers SupportMapFragment
into a ViewPager
that uses a FragmentPagerAdapter
.
我的尝试
因此,我认为可以通过将 SupportMapFragment 扩展为执行以下操作的新类来创建一个优雅的解决方案:
So in my nievety I thought could create an elegant solution by extending the SupportMapFragment into a new class that does the following:
- 添加一个采用
GoogleMapOptions
和List<MarkerOptions>
的新构造函数.这有效地调用了仅采用GoogleMapsOptions
的现有构造函数,然后将我的标记列表存储在新类的私有属性中. - 覆盖
GetMap()
- 检查它是否为空,如果不是,则将我们之前加载到地图上的标记添加到地图并最终返回.
- Add a new constructor that takes
GoogleMapOptions
andList<MarkerOptions>
. This effectively calls the existing constructor that just takesGoogleMapsOptions
and then store my list of markers inside a private property of the new class. - Override
GetMap()
- check if it is null and if it is not add the markers to the map that we loaded earlier on to the map and finally return it.
我的想法是我的扩展可以高度重复使用,并抽象出与一个地方相关的任何地图,并允许我的活动通过 newInstance(myGoogleMapOptions, myMarkers)
创建新的 MapFragment.
My thoughts were that my extension could be highly reusable and abstracts anything map related to one place and allows my activities to create the new MapFragment by newInstance(myGoogleMapOptions, myMarkers)
.
好的,这就是问题所在.第 1 部分似乎工作正常,但是在调试期间我无法调用重写的 GetMap() 方法.事实上,我已经尝试过覆盖 Fragment 拥有的大多数常用方法,但它们都没有被调用.SupportMapFragmemt 有什么奇怪的地方吗,我试图找到它的来源,但无济于事.
Ok so the issue. Part 1 seems to work fine however during debugging I cannot get the overridden GetMap() method to be called. In fact I have tried overriding most of the usual methods that fragments have and none of them get called. Is there something weird going on with the SupportMapFragmemt, I tried to find the source for it to no avail to check for myself.
更新
我意识到为什么我的被覆盖的方法没有被调用.这是因为我返回的是标准的 SupportMapFragment
而不是 MyMapFragment
.也就是说,我不能从静态方法调用 super,也不能毫无例外地从基类转换为派生类.
I realised why I wasn't getting my overridden methods called. It was because I was returning the standard SupportMapFragment
rather than MyMapFragment
. That said I cannot call super from a static method and I cannot cast from a base class to a derived class without exception.
编辑这里是课堂
public class MyMapFragment extends SupportMapFragment {
private static List<MarkerOptions> mMarkers;
public static SupportMapFragment newInstance(GoogleMapOptions options, List<MarkerOptions> markers)
{
SupportMapFragment fragment = newInstance(options);
mMarkers = markers;
return fragment;
}
//have tried Overriding several methods including getMap()...
@Override
public void onResume ()
{
GoogleMap mMap = super.getMap();
//add the markers
if(mMap != null)
{
for(MarkerOptions marker : mMarkers)
{
mMap.addMarker(marker);
}
}
}
}
推荐答案
就这样试了,效果很好.
Just tried like this and it works.
public class MyMapFragment extends SupportMapFragment {
private List<MarkerOptions> mMarkers;
public static MyMapFragment create(GoogleMapOptions options, ArrayList<MarkerOptions> markers) {
MyMapFragment fragment = new MyMapFragment();
Bundle args = new Bundle();
args.putParcelable("MapOptions", options); //obtained by decompiling google-play-services.jar
args.putParcelableArrayList("markers", markers);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hacky and ugly but it works
ArrayList<Parcelable> list = getArguments().getParcelableArrayList("markers");
mMarkers = new ArrayList<MarkerOptions>(list.size());
for (Parcelable parcelable : list) {
mMarkers.add((MarkerOptions) parcelable);
}
}
@Override
public void onResume() {
super.onResume();
GoogleMap mMap = super.getMap();
//add the markers
if (mMap != null) {
for (MarkerOptions marker : mMarkers) {
mMap.addMarker(marker);
}
}
}
}
这篇关于在 Viewpager 中映射 v2 SupportMapFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!