【问题标题】:Loading Google Maps is too slow in Android Application在 Android 应用程序中加载谷歌地图太慢了
【发布时间】:2023-03-21 10:06:01
【问题描述】:

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

这是加载谷歌地图的片段

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。

【问题讨论】:

  • 您的智能手机是罪魁祸首..尝试在其他设备上测试您的应用..
  • 好的,谢谢您的快速回复

标签: android google-maps maps fragment


【解决方案1】:

您正在同步加载地图,请尝试异步加载,Google 地图有一个 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));

}
}

【讨论】:

  • 我已经按照你告诉我的方式加载了,现在它似乎更快了。我接受你的回答。非常感谢。
【解决方案2】:

添加生命周期方法和调用 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2013-07-03
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2019-01-05
    • 2014-10-29
    相关资源
    最近更新 更多