我怎么知道地图是准备使用SupportMapFragment时习惯? [英] How do I know the map is ready to get used when using the SupportMapFragment?

查看:167
本文介绍了我怎么知道地图是准备使用SupportMapFragment时习惯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的onCreate 的方法,我正在使用的 SupportMapFragment 来显示地图。

  SupportMapFragment片段=新SupportMapFragment();
    getSupportFragmentManager()的BeginTransaction()
            。新增(android.R.id.content,片段).commit();
 

在结合这一点,我想补充一个标记。问题是,当调用的GetMap 为空,我什么时候能再试一次吗?有一个事件,我可以注册或我的做法本身并不对?

  MMAP =((SupportMapFragment)(getSupportFragmentManager()findFragmentById(R.id.map))。)的GetMap()。
    如果(MMAP == NULL)
        //我该怎么办吗?
 

地图实际上是显示在手机上,但是我似乎有在获得基准添加标记没有运气。

更新:

我创建的原因 SupportMapFragment 通过构造是因为典型的的setContentView 已崩溃并没有工作。这让我在predicament在那里我能不能获得我在的onCreate 的方法,因为我实际上创造参考 SupportMapFragment 在那个时候。在进一步的调查,似乎我的的setContentView 的问题是没有这两个谷歌播放-服务JAR和模块/ src目录设置为整个项目的一部分的副产品。一旦做这两个,的setContentView 现在的工作,我能获得通过的GetMap参考()因为我期望的那样。

lots.xml ...

 < XML版本=1.0编码=UTF-8&GT?;
<片段的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
          机器人:ID =@ + ID /图
          机器人:名称=com.google.android.gms.maps.SupportMapFragment
          机器人:layout_width =match_parent
          机器人:layout_height =match_parent/>
 

LotsActivity.java ...

 公共类LotsActivity扩展FragmentActivity {
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.lots);

        GoogleMap的MMAP;
        MMAP =((SupportMapFragment)(getSupportFragmentManager()findFragmentById(R.id.map))。)的GetMap()。
        如果(MMAP == NULL)
            //这个应该不是现在发生
    }
 

解决方案

编辑: 的GetMap 是德$ P $现在pcated

  

现在的问题是,当调用的GetMap是空的,我什么时候可以再试一次?

这取决于问题的性质

如果您设置了 SupportMapFragment 通过<片断> 在布局中的元素,你可以叫的GetMap()成功的onCreate()。但是,如果你创建 SupportMapFragment 通过构造,这是太早了 - 在 GoogleMap的不存在。您可以扩展 SupportMapFragment 并覆盖 onActivityCreated(),因为的GetMap()是准备好即可。

不过,的GetMap()可以的的回报为一个更大的问题,如谷歌播放服务没有安装。您将需要使用像 GooglePlayServicesUtil.isGooglePlayServicesAvailable()来检测这种情况并处理它,但是你的愿望。

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();

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.

UPDATE:

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...

<?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...

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
    }

解决方案

EDIT: getMap is deprecated now

The problem is when the call to getMap is null, when can I try again?

That depends upon the nature of the problem.

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆