TabLayout中的片段:Google地图未更新

时间:2017-01-31 16:48:27

标签: android google-maps android-fragments

我将TabLayout变成片段。每个标签包含另一个片段。其中一个片段包含一个地图。我第一次选择地图选项卡时,会加载地图,但不会使用我的long和lat值更新地图。从第二次加载并正确更新地图。

import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class GpsTab extends MapFragment implements
        OnMapReadyCallback {
    private boolean needsInit = false;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (savedInstanceState == null) {
            needsInit = true;
        }

        getMapAsync(this);
    }

    @Override
    public void onMapReady(final GoogleMap map) {
        if (needsInit) {
            CameraUpdate center =
                    CameraUpdateFactory.newLatLng(new LatLng(72.9655446,
                            -47.3458168));
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(16);

            map.moveCamera(center);
            map.animateCamera(zoom);
        }

        addMarker(map, 72.9655446, -47.3458168, R.string.marker_name ,R.string.marker_address);

    }

    private void addMarker(GoogleMap map, double lat, double lon,
                           int title, int snippet) {
        map.addMarker(new MarkerOptions().position(new LatLng(lat, lon))
                .title(getString(title))
                .snippet(getString(snippet)));
    }
}

0 个答案:

没有答案