2015-11-15 44 views
0

我有一張地圖,我在其上放置標記。如果將有超過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) { 
     } 
    } 
+1

https://developers.google.com/maps/documentation/android-api/utility/marker-clustering – CommonsWare

+0

的是,但當地json –

+0

請詳細解釋**,**爲什麼你認爲JSON是否本地是重要的。例如,您可以將10,000條輸入的JSON文件下載到設備中。那時,它也是本地的。 – CommonsWare

回答

1

使用ClusterManager類谷歌地圖這樣的。它會顯示在集羣中的標記,而不是向他們展示不同的

+0

您介意爲此添加一個示例嗎? – wmk

+0

wmk,下一個我的評論的解決方案 –

0

我的解決方案

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) { 
     } 
    } 
相關問題