使用 SupportMapFragment 时,我如何知道地图已准备好使用? [英] How do I know the map is ready to get used when using the SupportMapFragment?
问题描述
在 onCreate
方法中,我使用 SupportMapFragment
来显示地图.
In the onCreate
method, I am making use of the SupportMapFragment
to show a map.
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
为此,我想添加一个标记.问题是当对 getMap
的调用为空时,我什么时候可以再试一次?是否有我可以注册的活动,或者我的方法本身是错误的?
In conjunction to this, I would like to add a marker. The problem is when the call to getMap
is null, when can I try again? Is there an event I can register for or is my approach in and of itself wrong?
mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
if(mMap == null)
//what do I do here?
地图实际上显示在手机上,但我似乎没有运气获得添加标记的参考.
The map is in fact displaying on the phone however I appear to be having no luck in obtaining the reference to add markers.
更新:
我通过构造函数创建 SupportMapFragment
的原因是典型的 setContentView
崩溃并且不起作用.这让我陷入了无法在 onCreate
方法中获取引用的困境,因为当时我实际上正在创建 SupportMapFragment
.在进一步调查中,我的 setContentView
问题似乎是没有将 Google-play-services jar 和模块/src 设置为整个项目的一部分的副产品.完成这两项操作后,setContentView
现在可以工作,我可以通过 getMap()
获得参考.
The reason I was creating the SupportMapFragment
via the constructor is because the typical setContentView
was crashing and did not work. This put me in the predicament where I could not obtain my reference in the onCreate
method since I was in fact creating the SupportMapFragment
at that time. In further investigation, it appears my setContentView
issue was a byproduct of not having both the Google-play-services jar AND the module/src set up as part of the overall project. Upon doing BOTH of these, setContentView
now works and I can obtain the reference via getMap()
as I would expect.
lots.xml...
lots.xml...
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
LotsActivity.java...
LotsActivity.java...
public class LotsActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lots);
GoogleMap mMap;
mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
if(mMap == null)
//this should not occur now
}
推荐答案
问题是当getMap的调用为null时,我什么时候可以再试一次?
The problem is when the call to getMap is null, when can I try again?
这取决于问题的性质.
如果通过布局中的
元素设置SupportMapFragment
,则可以在getMap()
中成功调用getMap()
代码>onCreate().但是,如果您通过构造函数创建 SupportMapFragment
,那就太快了 -- GoogleMap
尚不存在.您可以扩展 SupportMapFragment
并覆盖 onActivityCreated()
,因为届时 getMap()
已准备就绪.
If you set up the SupportMapFragment
via the <fragment>
element in the layout, you can call getMap()
successfully in onCreate()
. But, if you create the SupportMapFragment
via the constructor, that's too soon -- the GoogleMap
does not yet exist. You can extend SupportMapFragment
and override onActivityCreated()
, as getMap()
is ready by then.
但是,getMap()
也可以返回null
来解决更大的问题,例如未安装 Google Play 服务.您需要使用诸如 GooglePlayServicesUtil.isGooglePlayServicesAvailable()
之类的东西来检测这种情况并按照您的意愿进行处理.
However, getMap()
can also return null
for a bigger problem, such as Google Play Services not being installed. You would need to use something like GooglePlayServicesUtil.isGooglePlayServicesAvailable()
to detect this condition and deal with it however you wish.
这篇关于使用 SupportMapFragment 时,我如何知道地图已准备好使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!