【问题标题】:Google Maps Android API extremely bad performanceGoogle Maps Android API 性能极差
【发布时间】:2018-02-10 03:52:10
【问题描述】:

我目前正在尝试编写一个使用标记在地图上显示多个位置的应用程序。即使只使用相对“小”的一组标记(我已经看到人们想要绘制超过 100k 的 StackOverflow 问题),我的性能也很糟糕,而且我的地图需要几秒钟才能移动。

我还有一张我的 GPU 分析的屏幕截图,我想知道是否有人能理解为什么这些调用如此之高:

我尝试使用 DDMS 分析我的应用程序,但这似乎没有给我任何确凿的答案。

我使用的代码如下:

tab_map.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/city_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize" />
</FrameLayout>

LocationFragment.java

/* a whole list of imports and package definition */

/**
 * Created by Ruben on 05-08-17.
 */

public class LocationsFragment extends Fragment
        implements OnMapReadyCallback {
    private static final String TAG = LocationsFragment.class.getSimpleName();

    private LocationsFragment instance;

    private City city;
    private List<Marker> markers;
    private List<Location> locations;

    private Target iconTarget;

    private GoogleMap map;
    private SupportMapFragment mapFragment;
    private BitmapDescriptor markerIcon;
    private Bitmap markerIconBitmap;

    private ApiService apiService;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab_map, null);
    }

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

        // Set up the view
        instance = this;

        mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.city_map);
        apiService = RestClient.getClient(getContext()).create(ApiService.class);
        city = getArguments().getParcelable("city");
        markers = new ArrayList<>();

        iconTarget = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                markerIconBitmap = bitmap;
                updateMarkerIcons();
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        };

        // Start async processes
        // Load Google map
        mapFragment.getMapAsync(this);

        // Load locations from API
        getLocations();

        // Load custom marker for city
        getCityMarker();

    }

    private void getCityMarker() {
        Picasso
                .with(getContext())
                .load(city.getMarker().getUrl())
                .resize(ScreenUtils.convertDIPToPixels(getContext(), 24), ScreenUtils.convertDIPToPixels(getContext(), 24))
                .into(iconTarget);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        this.map = map;

        if(markerIconBitmap == null) {
            markerIcon = BitmapDescriptorFactory.fromAsset("default1.png");
        }else{
            markerIcon = BitmapDescriptorFactory.fromBitmap(markerIconBitmap);
        }

        if (ContextCompat.checkSelfPermission(this.getContext(), "ACCESS_FINE_LOCATION") == PackageManager.PERMISSION_GRANTED) {
            map.setMyLocationEnabled(true);
        } else {
            Toast.makeText(instance.getContext(), getString(R.string.no_location_permission), Toast.LENGTH_LONG).show();
        }

        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(city.getCenter().getLat(), city.getCenter().getLong()), 12));

        setMarkers();
    }

    public void getLocations() {
        Call<LocationsResponse> call = apiService.getLocations(city.getId());
        call.enqueue(new Callback<LocationsResponse>() {
            @Override
            public void onResponse(Call<LocationsResponse> call, Response<LocationsResponse> response) {
                if(response.code() != 200) {
                    Toast.makeText(instance.getContext(), getString(R.string.retrieving_locations) + "1", Toast.LENGTH_LONG).show();
                    return;
                }

                Log.d(TAG, "Received locations: " + response.body().getLocations().size());
                locations = response.body().getLocations();

                setMarkers();
            }

            @Override
            public void onFailure(Call<LocationsResponse> call, Throwable t) {
                t.printStackTrace();
                Toast.makeText(instance.getContext(), getString(R.string.retrieving_locations) + "1", Toast.LENGTH_LONG).show();
            }
        });
    }

    public void setMarkers() {
        Log.d(TAG, "Calling setMarkers");
        if(map != null && locations != null) {
            // Remove all the old markers
            if(!markers.isEmpty()) {
                int numMarkers = markers.size();
                for(int i = 0; i < numMarkers; i++) {
                    markers.get(i).remove();
                }
                markers.clear();
            }

            // Create a new marker for each location
            int numLocations = locations.size();
            Log.d(TAG, "Setting markers for " + numLocations + " locations");
            for(int i = 0; i < numLocations; i++) {
                Location location = locations.get(i);
                markers.add(map.addMarker(
                    new MarkerOptions()
                        .position(new LatLng(location.getPosition().getLat(), location.getPosition().getLong()))
                        //.icon(markerIcon)
                        //.title(location.getName())
                        //.snippet(getString(R.string.location_marker_text, NumberFormat.getInstance().format(location.getStones())))
                ));
            }
        }
    }

    public void updateMarkerIcons() {
        Log.d(TAG, "Calling updateMarkerIcons");
        if(map != null && markerIconBitmap != null) {
            markerIcon = BitmapDescriptorFactory.fromBitmap(markerIconBitmap);
        }

        if(map != null && !markers.isEmpty()) {
            int numMarkers = markers.size();
            for(int i = 0; i < numMarkers; i++) {
                Marker marker = markers.get(i);
                //marker.setIcon(markerIcon);
            }
        }
    }

    @Override
    public void onDestroyView() {
        Picasso.with(this.getContext()).cancelRequest(iconTarget);

        mapFragment.onDestroyView();

        super.onDestroyView();
    }

    public void updateLocations() {
        if(locations != null) {
            getLocations();
        }
    }

    public void setCity(City city) {
        this.city = city;
        getCityMarker();
    }
}

如您所见,我取消了一些我为标记设置.snippet.icon 的行的注释,因为我怀疑这可能是问题所在。可悲的是,这似乎不是问题。

使用断点,我试图确认我设置/编辑标记的所有函数都没有在它们应该被调用之后被调用,我发现这绝对不是这种情况。设置标记后,不会在此文件中执行任何其他代码。我所有的其他活动似乎都运行良好,所以我开始相信这是与谷歌地图有关的问题。值得注意的是,当我离开标记时,即不再处于屏幕边界内时,性能还不错,滚动看起来很流畅。

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    抱歉,回答晚了,您可能已经找到了解决方案,但它可能对其他人有所帮助。

    你考虑过Clusters吗?

    您遇到的问题是,在地图上渲染所有这些标记需要大量资源,并且每次视图移动时 api 都会重新渲染。

    当许多引脚非常靠近时,您无法真正将它们彼此区分开来。默认情况下,集群将根据地图上的缩放对它们进行分组,以优化渲染和可视化。 如果默认行为或外观不符合您的口味,您可以轻松覆盖它。

    希望我能帮上忙。

    【讨论】:

      猜你喜欢
      • 2012-07-04
      • 2011-08-13
      • 1970-01-01
      • 2015-09-18
      • 1970-01-01
      • 2023-03-31
      • 2016-05-21
      • 2012-07-27
      • 2013-03-24
      相关资源
      最近更新 更多