如何获得GoogleMap参考- Android谷歌地图API



我很难使用谷歌地图Android API

的情况是:我有一个父片段,它持有对MapFragment的引用。我的目标只是在地图上添加一些标记。

MapFragment总是在onViewCreated方法中返回null,这是预期的,因为MapFragment布局在那一刻可能还没有完全加载。

void onViewCreated(){
   GoogleMap sMap = mapFragment.getMap(); //<---- always return null
   // add markers
   //.....
}

我试图把MapFragment.getMap()在一个AysnkTask和使用while循环试图保持对地图的引用,但它不起作用。出现一个奇怪的异常。

void onViewCreated(){
           AsyncTask<Object, Object, GoogleMap> loadInstruction = new AsyncTask<Object, Object, GoogleMap>(){
            @Override
            protected GoogleMap doInBackground(Object... params) {
                GoogleMap sMap = mapFragment.getMap();
                while(sMap == null) {
                    sMap = mapFragment.getMap();
                }
                return sMap;
            }
            @Override
            protected void onPostExecute(GoogleMap result) {
                //add Markers 
                                    //......
            }
        };
           loadInstruction.execute();

异常

06-23 11:28:05.754: I/dalvikvm(4129): Jit: resizing JitTable from 4096 to 8192
06-23 11:28:05.754: I/dalvikvm(4129): Jit: resizing JitTable from 4096 to 8192
06-23 11:28:05.764: E/AndroidRuntime(4129): FATAL EXCEPTION: main
06-23 11:28:05.764: E/AndroidRuntime(4129): Process: com.wenchao.map, PID: 4129
06-23 11:28:05.764: E/AndroidRuntime(4129): java.lang.NullPointerException
06-23 11:28:05.764: E/AndroidRuntime(4129):     at kbh.a(Unknown Source)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at mbi.a(Unknown Source)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at lxn.a(Unknown Source)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at lxm.a(Unknown Source)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at fnb.onTransact(SourceFile:107)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at   android.os.Binder.transact(Binder.java:361)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
 06-23 11:28:05.764: E/AndroidRuntime(4129):    at com.google.android.gms.maps.SupportMapFragment$a.onCreateView(Unknown Source)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at com.google.android.gms.dynamic.a$4.b(Unknown Source)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at com.google.android.gms.dynamic.a.a(Unknown Source)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at android.os.Handler.handleCallback(Handler.java:733)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at android.os.Looper.loop(Looper.java:136)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at android.app.ActivityThread.main(ActivityThread.java:5001)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at java.lang.reflect.Method.invokeNative(Native Method)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at java.lang.reflect.Method.invoke(Method.java:515)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-23 11:28:05.764: E/AndroidRuntime(4129):     at dalvik.system.NativeStart.main(Native Method)
06-23 11:28:06.914: I/Process(4129): Sending signal. PID: 4129 SIG: 9

因为getMap()不能保证在任何时候都返回一个map引用,也没有钩子方法或某个地方可以让我注册一个回调。和后台异步任务似乎不允许,你如何确定何时调用getMap()并获得一个地图引用?

包含下面的onCreateView片段,这将解决您的问题,

   SupportMapFragment mFragment;
        mFragment = new SupportMapFragment() {
            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                if ((map = mFragment.getMap()) != null) {
                    setUpMap();
                }
            }
        };

一旦地图准备好了,SuppotMapFragment的onActivityCreated将在那里被调用,你可以设置你的地图。

希望能有所帮助

Google map v2 android方法getMap()返回null

解决方案在上面的链接为我工作。虽然我没有找到文档说getMap()将会在onActivityCreated中返回一个有用的地图引用,但调用getMap似乎是可行的。

相关内容

  • 没有找到相关文章

最新更新