【问题标题】:getting google maps in a tabbed android app在选项卡式 Android 应用程序中获取谷歌地图
【发布时间】:2015-07-06 19:35:44
【问题描述】:

我花了一段时间试图让它工作,但我总是在每个教程的某个地方卡住。简而言之,我正在尝试制作一个选项卡式应用程序,其中一个选项卡是谷歌地图。

我已经修正了所有常见的错误:

我已经通过 SDK 下载了所有相关内容。 我有一个 API 密钥。 我添加了编译com.google.android.gms:play-services:7.5.0 to my dependencies.

我正在尝试关注this code,但它并没有完全适合我,所以我稍微改变了它,但我还是碰壁了。

我当前的错误是java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized。我认为这会在 getMapAsync 中初始化。

这是我的java代码:

public class MapFragment extends Fragment{

    MapView mMapView;
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // inflat and return the layout
        View v = inflater.inflate(R.layout.fragment_location_info, container,
                false);

        mMapView = (MapView) v.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume();// needed to get the map to display immediately

        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                MapsInitializer.initialize(getApplicationContext());
                // latitude and longitude
                double latitude = 17.385044;
                double longitude = 78.486671;


                // create marker
                MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");

                // Changing marker icon
                marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
            }
        });

        //MapsInitializer.initialize(getApplicationContext());

        googleMap = mMapView.getMap();
        // latitude and longitude
        double latitude = 17.385044;
        double longitude = 78.486671;


        // create marker
        MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");

        // Changing marker icon
        marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // adding marker
        googleMap.addMarker(marker);

        CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(17.385044, 78.486671)).zoom(12).build();

        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        // Perform any camera updates here
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
}

【问题讨论】:

  • FWIW,here is a sample app,在ViewPager 的十页中的每一页中都有一个地图,并带有一个选项卡式指示器。
  • @JohnF 下面的答案是否可以帮助您使其正常工作?告诉我!
  • 看起来不错,我没有任何错误消息,但在我的模拟器上运行它时遇到问题。
  • @JohnF 尼斯。我看到了你的另一个问题,看看我在那个问题上发布的链接,看起来你的模拟器可能运行的是旧版本的 Google Play 服务。如果是这种情况,您只需要在模拟器上升级!我发布链接的答案中的代码将提示升级(如果有)。

标签: android google-maps android-fragments google-maps-api-2 android-tabs


【解决方案1】:

看起来您的代码正在进行转换。你同时打电话给getMapAsync()getMap()

只需使用getMapAsync(),在收到onMapReady() 回调之前不要对地图进行任何操作。

另外,您不需要MapsInitializer。来自文档:

使用此类初始化 Google Maps Android API if features 需要在获取地图之前使用。必须调用它,因为有些 BitmapDescriptorFactory 和 CameraUpdateFactory 等类需要 被初始化。

如果您正在使用 MapFragment 或 MapView 并且已经获得了 (非空)通过在这些类中调用 getMap() 的 GoogleMap, 那么你不需要担心这个类。查看示例 应用一些例子。

这里是你的代码修正了一下:

public class MapFragment extends Fragment implements OnMapReadyCallback {

        MapView mMapView;
        private GoogleMap googleMap;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // inflate and return the layout
            View v = inflater.inflate(R.layout.fragment_location_info, container,
                    false);

            mMapView = (MapView) v.findViewById(R.id.mapView);
            mMapView.onCreate(savedInstanceState);

            //mMapView.onResume(); // should not be needed

            mMapView.getMapAsync(this);

            //MapsInitializer.initialize(getApplicationContext());

            // Perform any camera updates here
            return v;
        }

        @Override
        public void onMapReady(GoogleMap gMap) {

            googleMap = gMap;

            mMapView.onResume(); //call this here if you really need to

            //MapsInitializer.initialize(getApplicationContext());
            // latitude and longitude
            double latitude = 17.385044;
            double longitude = 78.486671;

            // create marker
            MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps");

            // Changing marker icon
            marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

            // adding marker
            googleMap.addMarker(marker);

            CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(17.385044, 78.486671)).zoom(12).build();

            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        }
   //.........................

【讨论】:

  • 如果它是一个活动而不是片段,它会是一样的吗?我的意思是,我需要使用 MapView 吗?
  • @ReneLimon 看看我在你的问题中链接到的另一个答案,它可能比这个更适用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
  • 2014-10-29
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 2012-03-16
相关资源
最近更新 更多