我有一張地圖,我在其上放置標記。如果將有超過10000個標記,我怎麼能將這些標記分組?我發現只有本地JSON的例子,但我有一個從服務器經緯度信息的形式:如何將這些標記分組的時間超過10000?
[
{
"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) {
}
}
https://developers.google.com/maps/documentation/android-api/utility/marker-clustering – CommonsWare
的是,但當地json –
請詳細解釋**,**爲什麼你認爲JSON是否本地是重要的。例如,您可以將10,000條輸入的JSON文件下載到設備中。那時,它也是本地的。 – CommonsWare