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

Jua*_*ros 4 maps android google-maps fragment

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

这是加载谷歌地图的片段

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;
}

}
Run Code Online (Sandbox Code Playgroud)

也许我的智能手机不够好,它是 BQ aquaris E4。

小智 5

您正在同步加载地图,尝试异步加载,谷歌地图有一个可以使用的 Map Ready 回调。

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));

}
}
Run Code Online (Sandbox Code Playgroud)


Sud*_*han 5

添加生命周期方法并调用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();
}
Run Code Online (Sandbox Code Playgroud)