-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Demo clustering with ViewModel (#506)
* Differentiate between initial start and configuration change restore Only move the camera to the starting location when not restoring * Add removeItems() to ClusterManager and Algorithm Avoid locking and unlocking the algorithm in a tight loop when removing many cluster items * Move algorithm lock from ClusterManager to Algorithm Allow the algorithm to handle its own locking, so it can be reused in another ClusterManager instance. Also allow adding/removing items with proper locking outside of ClusterManager. For best ViewModel support.
- Loading branch information
Showing
13 changed files
with
221 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
demo/src/main/java/com/google/maps/android/utils/demo/ClusteringViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2019 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.maps.android.utils.demo; | ||
|
||
import androidx.lifecycle.ViewModel; | ||
import android.content.res.Resources; | ||
|
||
import com.google.android.gms.maps.model.LatLng; | ||
import com.google.maps.android.clustering.algo.NonHierarchicalViewBasedAlgorithm; | ||
import com.google.maps.android.utils.demo.model.MyItem; | ||
|
||
import org.json.JSONException; | ||
|
||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
public class ClusteringViewModel extends ViewModel { | ||
|
||
private NonHierarchicalViewBasedAlgorithm<MyItem> mAlgorithm = new NonHierarchicalViewBasedAlgorithm<>(0, 0); | ||
|
||
NonHierarchicalViewBasedAlgorithm<MyItem> getAlgorithm() { | ||
return mAlgorithm; | ||
} | ||
|
||
void readItems(Resources resources) throws JSONException { | ||
InputStream inputStream = resources.openRawResource(R.raw.radar_search); | ||
List<MyItem> items = new MyItemReader().read(inputStream); | ||
mAlgorithm.lock(); | ||
try { | ||
for (int i = 0; i < 100; i++) { | ||
double offset = i / 60d; | ||
for (MyItem item : items) { | ||
LatLng position = item.getPosition(); | ||
double lat = position.latitude + offset; | ||
double lng = position.longitude + offset; | ||
MyItem offsetItem = new MyItem(lat, lng); | ||
mAlgorithm.addItem(offsetItem); | ||
} | ||
} | ||
} finally { | ||
mAlgorithm.unlock(); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
demo/src/main/java/com/google/maps/android/utils/demo/ClusteringViewModelDemoActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2019 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.maps.android.utils.demo; | ||
|
||
import androidx.lifecycle.ViewModelProviders; | ||
import android.os.Bundle; | ||
import android.util.DisplayMetrics; | ||
import android.widget.Toast; | ||
|
||
import com.google.android.gms.maps.CameraUpdateFactory; | ||
import com.google.android.gms.maps.model.LatLng; | ||
import com.google.maps.android.clustering.ClusterManager; | ||
import com.google.maps.android.utils.demo.model.MyItem; | ||
|
||
import org.json.JSONException; | ||
|
||
public class ClusteringViewModelDemoActivity extends BaseDemoActivity { | ||
private ClusterManager<MyItem> mClusterManager; | ||
private ClusteringViewModel mViewModel; | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
mViewModel = ViewModelProviders.of(this).get(ClusteringViewModel.class); | ||
if (savedInstanceState == null) { | ||
try { | ||
mViewModel.readItems(getResources()); | ||
} catch (JSONException e) { | ||
Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void startDemo(boolean isRestore) { | ||
if (!isRestore) { | ||
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10)); | ||
} | ||
|
||
DisplayMetrics metrics = new DisplayMetrics(); | ||
getWindowManager().getDefaultDisplay().getMetrics(metrics); | ||
mViewModel.getAlgorithm().updateViewSize(metrics.widthPixels, metrics.heightPixels); | ||
|
||
mClusterManager = new ClusterManager<>(this, getMap()); | ||
mClusterManager.setAlgorithm(mViewModel.getAlgorithm()); | ||
|
||
getMap().setOnCameraIdleListener(mClusterManager); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
library/src/main/java/com/google/maps/android/clustering/algo/AbstractAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.google.maps.android.clustering.algo; | ||
|
||
import com.google.maps.android.clustering.ClusterItem; | ||
|
||
import java.util.concurrent.locks.ReadWriteLock; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
||
/** | ||
* Base Algorithm class that implements lock/unlock functionality. | ||
*/ | ||
public abstract class AbstractAlgorithm<T extends ClusterItem> implements Algorithm<T> { | ||
|
||
private final ReadWriteLock mLock = new ReentrantReadWriteLock(); | ||
|
||
@Override | ||
public void lock() { | ||
mLock.writeLock().lock(); | ||
} | ||
|
||
@Override | ||
public void unlock() { | ||
mLock.writeLock().unlock(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.