从网络 api 加载 google 地图所需的标记,并在相机移动时动态加载其他标记

Load needed markers for google map from web api and load others dynamically as camera move

在我的 android 应用程序中,我想在 google 地图上动态显示标记。我的网站 api 有很多可用的标记位置数据,需要根据用户需要查看的内容加载它们(例如,只获取属于地图那部分的标记,并将其他标记加载为相机以异步模式移动)。

我进行了很多搜索,但找不到任何直接的解决方案,我的问题是实现此类功能的最佳实践和解决方案是什么?有没有为此提供的指南?

P.S:我用集群管理器加载android google地图中的所有标记没有任何问题,但是当标记数量变大时,它也需要很多时间来获取所有这些。

你可以使用Google地图聚类更多的标记data.It会给你有效的Google地图 经验。通过对标记进行聚类,您可以在地图上放置大量标记而不会使地图难以阅读。

这是您可以遵循的官方文档here

示例代码。

// Declare a variable for the cluster manager.
private ClusterManager<MyItem> mClusterManager;

private void setUpClusterer() {
    // Position the map.
    getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));

    // Initialize the manager with the context and the map.
    // (Activity extends context, so we can pass 'this' in the constructor.)
    mClusterManager = new ClusterManager<MyItem>(this, getMap());

    // Point the map's listeners at the listeners implemented by the cluster
    // manager.
    getMap().setOnCameraIdleListener(mClusterManager);
    getMap().setOnMarkerClickListener(mClusterManager);

    // Add cluster items (markers) to the cluster manager.
    addItems();
}

private void addItems() {

    // Set some lat/lng coordinates to start with.
    double lat = 51.5145160;
    double lng = -0.1270060;

    // Add ten cluster items in close proximity, for purposes of this example.
    for (int i = 0; i < 10; i++) {
        double offset = i / 60d;
        lat = lat + offset;
        lng = lng + offset;
        MyItem offsetItem = new MyItem(lat, lng);
        mClusterManager.addItem(offsetItem);
    }
}

通过使用此示例代码,您需要配置 api 数据以根据您的要求进行聚类并在地图上显示标记。

由于大量标记数据显然需要时间加载到集群管理器中,并且还取决于您连接的网络。