【发布时间】: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