New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for click listeners on GeoJSON layers. #286
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d40765e
Add support for click listeners on GeoJSON layers.
stephenmcd 504e1a4
Use toast in GeoJSON clickable layer demo.
stephenmcd 2d6a810
Make GeoJsonLayer.getFeature() type-safe.
stephenmcd 61ab2f0
Fix BiMultiMap.remove() and implement missing methods (clear, clone).
stephenmcd 54a71f5
Better isolation and correct assertion orders in BiMultiMapTest
stephenmcd 5ac809c
Javadoc fixes
stephenmcd 251159e
Make GeoJSON Polylines clickable.
stephenmcd dcf33d6
Make BiMultiMap package private.
stephenmcd File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
83 changes: 83 additions & 0 deletions
83
library/src/com/google/maps/android/geojson/BiMultiMap.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,83 @@ | ||
package com.google.maps.android.geojson; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Extension of HashMap that provides two main features. Firstly it allows reverse lookup | ||
* for a key given a value, by storing a second HashMap internally which maps values to keys. | ||
* Secondly, it supports Collection values, in which case, each item in the collection is | ||
* used as a key in the internal reverse HashMap. It's therefore up to the caller to ensure | ||
* the overall set of values, and collection values, are unique. | ||
* | ||
* Used by GeoJsonRenderer to store GeoJsonFeature instances mapped to corresponding Marker, | ||
* Polyline, and Polygon map objects. We want to look these up in reverse to provide access | ||
* to GeoJsonFeature instances when map objects are clicked. | ||
*/ | ||
/* package */ class BiMultiMap<K> extends HashMap<K, Object> { | ||
|
||
private final Map<Object, K> mValuesToKeys = new HashMap<>(); | ||
|
||
@Override | ||
public void putAll(Map<? extends K, ?> map) { | ||
// put() manages the reverse map, so call it on each entry. | ||
for (Entry<? extends K, ?> entry : map.entrySet()) { | ||
put(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
@Override | ||
public Object put(K key, Object value) { | ||
// Store value/key in the reverse map. | ||
mValuesToKeys.put(value, key); | ||
return super.put(key, value); | ||
} | ||
|
||
public Object put(K key, Collection values) { | ||
// Store values/key in the reverse map. | ||
for (Object value : values) { | ||
mValuesToKeys.put(value, key); | ||
} | ||
return super.put(key, values); | ||
} | ||
|
||
@Override | ||
public Object remove(Object key) { | ||
Object value = super.remove(key); | ||
// Also remove the value(s) and key from the reverse map. | ||
if (value instanceof Collection) { | ||
for (Object valueItem : (Collection) value) { | ||
mValuesToKeys.remove(valueItem); | ||
} | ||
} else { | ||
mValuesToKeys.remove(value); | ||
} | ||
return value; | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
super.clear(); | ||
mValuesToKeys.clear(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public BiMultiMap<K> clone() { | ||
BiMultiMap<K> cloned = new BiMultiMap<>(); | ||
cloned.putAll((Map<K, Object>) super.clone()); | ||
return cloned; | ||
} | ||
|
||
/** | ||
* Reverse lookup of key by value. | ||
* | ||
* @param value Value to lookup | ||
* @return Key for the given value | ||
*/ | ||
public K getKey(Object value) { | ||
return mValuesToKeys.get(value); | ||
} | ||
|
||
} |
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
42 changes: 42 additions & 0 deletions
42
library/tests/src/com/google/maps/android/geojson/BiMultiMapTest.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,42 @@ | ||
package com.google.maps.android.geojson; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class BiMultiMapTest extends TestCase { | ||
|
||
public void testSingle() { | ||
BiMultiMap<String> map = new BiMultiMap<>(); | ||
String key = "foo"; | ||
String value = "bar"; | ||
map.put(key, value); | ||
assertEquals(1, map.size()); | ||
assertEquals(value, map.get(key)); | ||
assertEquals(key, map.getKey(value)); | ||
map.remove(key); | ||
assertEquals(0, map.size()); | ||
assertNull(map.get(key)); | ||
assertNull(map.getKey(value)); | ||
} | ||
|
||
public void testMulti() { | ||
BiMultiMap<String> map = new BiMultiMap<>(); | ||
String key = "foo"; | ||
List<String> values = Arrays.asList("bar", "baz"); | ||
map.put(key, values); | ||
assertEquals(1, map.size()); | ||
assertEquals(values, map.get(key)); | ||
for (String value : values) { | ||
assertEquals(key, map.getKey(value)); | ||
} | ||
map.remove(key); | ||
assertEquals(0, map.size()); | ||
assertNull(map.get(key)); | ||
for (String value : values) { | ||
assertEquals(null, map.getKey(value)); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the rest of the geometries?