在 Android 应用程序中加载 Google 地图太慢

Loading Google Maps is too slow in Android Application

在我的 Android 应用程序中,我有一个带有按钮的片段。单击按钮后,我将加载另一个带有 MapView 的片段。一切正常,但问题是,单击前一个片段的按钮后,带有 Google 地图的片段至少需要 0.5 秒才能启动。你知道另一种加载 google 地图而不卡住 Fragment 事务的方法吗?

这是加载 Google 地图的片段

public class DetalleRuta extends android.support.v4.app.Fragment {

private GoogleMap googleMap;
private MapView mapView;

public DetalleRuta() {
    // Required empty public constructor
}

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

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

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_detalle_ruta, container, false);

    //Inicio el mapa
    mapView = (MapView)v.findViewById(R.id.mapa);
    mapView.onCreate(savedInstanceState);

    googleMap = mapView.getMap();
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);


    return v;
}

}

可能是我的手机不够好,是BQ aquaris E4

您正在同步加载地图,请尝试异步加载,Google 地图有一个地图就绪回调可供您使用。

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.app.Activity;
import android.os.Bundle;

public class MapPane extends Activity implements OnMapReadyCallback {

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

    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
    LatLng sydney = new LatLng(-33.867, 151.206);

    map.setMyLocationEnabled(true);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

}
}

添加生命周期方法和调用 mapView 的生命周期方法对我有用!

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

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

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

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