Android版谷歌地图onMa pready店的GoogleMap [英] Android Google Maps onMapReady store GoogleMap

查看:142
本文介绍了Android版谷歌地图onMa pready店的GoogleMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发一个Android应用程序,它使用这个谷歌地图API V2。要使用得到谷歌地图IM的一个实例的<一个href=\"https://developers.google.com/android/reference/com/google/android/gms/maps/MapView.html#getMapAsync(com.google.android.gms.maps.OnMa$p$padyCallback)\"相对=nofollow> onMa pready回调。
在此回调,我得到了谷歌地图的实例,我可以存储这个instace当地的重用,没有得到一个新的我每次都更新地图的时间?或者是有随着时间的推移重用这个实例的任何问题?我只是想获得肯定,没有什么用,我可以进入的问题。

i have an android app in development, which uses this google maps api v2. To get an instance of the google map im using the onMapReady callback. In this callback i get an instance of the google map, can i store this instace local an reuse it, without getting a new one every time i have to update the map? Or is there any issue with reusing this instance over time? I just like to get sure that there is nothing with which i could get into problem.

谢谢您的回答!

推荐答案

是的,你可以存储在谷歌地图的参考实例,并且就像你,如果你叫再使用的GetMap( )而不是 getMapAsync()

Yes, you can store an instance of the Google Map reference, and re-use it just as you would if you called getMap() instead of getMapAsync().

只要确保重新调用 getMapAsync() onResume()如果需要的话,因为常常地图在引用的onPause将成为空()被调用。

Just make sure to re-call getMapAsync() from onResume() if needed, since often the map reference will become null after onPause() is called.

下面是一个简单的例子来说明。这code每个用户点击地图时放置一个标记,你需要一个有效的映射引用事做。还有调用 startActivityForResult()并启动另一个活动的按钮。

Here is a simple example to illustrate. This code places a Marker each time the user taps the map, something you need a valid map reference to do. There is also a button that calls startActivityForResult() and launches another Activity.

下面是code:

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private Marker marker;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        button = (Button) findViewById(R.id.testButton);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MapsActivity.this, TestActivity.class);
                startActivityForResult(i, 100);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 100) {
            Log.d("MyMap", "onActivityResult " + data.getStringExtra("result"));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        Log.d("MyMap", "onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();

        Log.d("MyMap", "onResume");
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {

        if (mMap == null) {

            Log.d("MyMap", "setUpMapIfNeeded");
            ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMapAsync(this);
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Log.d("MyMap", "onMapReady");
        mMap = googleMap;
        setUpMap();
    }

    private void setUpMap() {

        mMap.setMyLocationEnabled(true);
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        mMap.getUiSettings().setMapToolbarEnabled(false);


        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

            @Override
            public void onMapClick(LatLng point) {

                Log.d("MyMap", "MapClick");

                //remove previously placed Marker
                if (marker != null) {
                    marker.remove();
                }

                //place marker where user just clicked
                marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

                Log.d("MyMap", "MapClick After Add Marker");

            }
        });

    }
}

下面是从运行应用程序,点击它一次以放置一个标记,然后按一下按钮,启动第二个活动,返回回到地图的活动,然后再点击地图放置标记所生成的日志。

Here are the resulting logs from running the app, tapping it once to place a Marker, then clicking the button to launch the second Activity, returning back to the map Activity, and then tapping the map again to place Markers.

您可以看到,当的onPause()被称为按一下按钮启动等活动后,地图参考丢失,当 onResume ()叫,这让另一个调用 getMapAsync()。然而,这一切都很好,因为code设置为考虑到这一点。

You can see that when onPause() was called after the button click launches the other Activity, the map reference was lost, as when onResume() was called, it made another call to getMapAsync(). However, it's all fine, because the code is set up to take this into account.

 D/MyMap﹕ onResume
 D/MyMap﹕ setUpMapIfNeeded
 D/MyMap﹕ onMapReady
 D/MyMap﹕ MapClick
 D/MyMap﹕ MapClick After Add Marker
 D/MyMap﹕ onPause
 D/MyMap﹕ onActivityResult ok
 D/MyMap﹕ onResume
 D/MyMap﹕ setUpMapIfNeeded
 D/MyMap﹕ onMapReady
 D/MyMap﹕ MapClick
 D/MyMap﹕ MapClick After Add Marker
 D/MyMap﹕ MapClick
 D/MyMap﹕ MapClick After Add Marker
 D/MyMap﹕ MapClick
 D/MyMap﹕ MapClick After Add Marker
 D/MyMap﹕ MapClick
 D/MyMap﹕ MapClick After Add Marker
 D/MyMap﹕ MapClick
 D/MyMap﹕ MapClick After Add Marker
 D/MyMap﹕ MapClick
 D/MyMap﹕ MapClick After Add Marker

这篇关于Android版谷歌地图onMa pready店的GoogleMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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