【问题标题】:How could I group these markers when there will be more than 10000 of them?当这些标记超过 10000 个时,我该如何对它们进行分组?
【发布时间】:2015-11-15 16:22:04
【问题描述】:

我有一张地图,我在上面放置了标记。当这些标记超过 10000 个时,我该如何对它们进行分组?我只找到了本地 JSON 的示例,但我有来自服务器的 latlng 格式:

[
  {
    "name": "hereName",
    "adr": "hereAdress",
    "latlng": [
      44.444444444,
      55.555555555
    ]
  }
]

代码:

private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
    if (map == null) {
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        map = mapFragment.getMap();
        map.getUiSettings().setZoomControlsEnabled(true);
        map.setMyLocationEnabled(true);
        map.getUiSettings().setMyLocationButtonEnabled(true);

        if (map != null) {
            new MarkerTask().execute();
        }
    }
}

private class MarkerTask extends AsyncTask<Void, Void, String> {
    private static final String SERVICE_URL = http://exm.com/latlng.php";

    @Override
    protected String doInBackground(Void... args) {
        HttpURLConnection conn = null;
        final StringBuilder json = new StringBuilder();
        try {
            // Connect to the web service
            URL url = new URL(SERVICE_URL);
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                json.append(buff, 0, read);
            }
        } catch (IOException e) {
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return json.toString();
    }

    @Override
    protected void onPostExecute(String json) {
        try {
            // De-serialize the JSON string into an array of city objects
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObj = jsonArray.getJSONObject(i);

                LatLng latLng = new LatLng(
                        jsonObj.getJSONArray("latlng").getDouble(0),
                        jsonObj.getJSONArray("latlng").getDouble(1)
                );

                // Create a markers
                map.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
                        .title(jsonObj.getString("name"))
                        .snippet(jsonObj.getString("adr"))
                        .position(latLng));
            }
        } catch (JSONException e) {
        }
    }

【问题讨论】:

  • 是的,但是有本地json
  • 详细解释,为什么您认为 JSON 是否是本地的很重要。例如,您可以将包含 10,000 个条目的 JSON 文件下载到设备。在这一点上,它也是本地的。
  • Json 会在服务器上不断更新。我觉得每次在本地下载和使用都不是很好
  • 那么您将主要依靠自己。现有的集群解决方案之所以有效,是因为它们可以访问所有位置。您的解决方案需要以某种方式“聚集”位置,而 没有所有位置。据推测,这将需要您的服务器自己提供集群,并为 JSON 提供有关集群的信息,然后您将通过一种或另一种方式呈现自己。

标签: java php android json google-maps


【解决方案1】:

为此使用 Google 地图的 ClusterManager 类。它会在一个集群中显示标记,而不是以不同的方式显示它们

【讨论】:

  • wmk,解决方法在下我的评论
【解决方案2】:

我的解决方案

private GoogleMap map;
private ClusterManager<MyItem> mClusterManager;
double lat;
double lng;

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

private void setUpMapIfNeeded() {
    if (map == null) {
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        map = mapFragment.getMap();
        map.getUiSettings().setZoomControlsEnabled(true);
        map.setMyLocationEnabled(true);
        map.getUiSettings().setMyLocationButtonEnabled(true);

        mClusterManager = new ClusterManager<MyItem>(this, mapFragment.getMap());
        mapFragment.getMap().setOnCameraChangeListener(mClusterManager);
        mapFragment.getMap().setOnMarkerClickListener(mClusterManager);

        if (map != null) {
          new MarkerTask().execute();
        }
    }
}

private class MarkerTask extends AsyncTask<Void, Void, String> {

    private static final String SERVICE_URL = http://exm.com/latlng.php";

    @Override
    protected String doInBackground(Void... args) {
        HttpURLConnection conn = null;
        final StringBuilder json = new StringBuilder();
        try {
            // Connect to the web service
            URL url = new URL(SERVICE_URL);
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                json.append(buff, 0, read);
            }
        } catch (IOException e) {
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return json.toString();
    }

    @Override
    protected void onPostExecute(String json) {
        try {
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObj = jsonArray.getJSONObject(i);

                List<MyItem> items = new ArrayList<MyItem>();
                lat = jsonObj.getJSONArray("latlng").getDouble(0);
                lng = jsonObj.getJSONArray("latlng").getDouble(1);

                items.add(new MyItem(lat, lng));
                mClusterManager.addItems(items);
           }
        } catch (JSONException e) {
        }
    }

【讨论】:

    猜你喜欢
    • 2021-03-22
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    相关资源
    最近更新 更多