-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(google-maps): Implemented map feature functions (i.e. GeoJSON su…
…pport)
- Loading branch information
Showing
12 changed files
with
749 additions
and
3 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
68 changes: 68 additions & 0 deletions
68
...droid/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMapsFeatureLayer.kt
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,68 @@ | ||
package com.capacitorjs.plugins.googlemaps | ||
|
||
import android.graphics.Color | ||
import com.google.maps.android.data.Feature | ||
import com.google.maps.android.data.Layer | ||
import com.google.maps.android.data.geojson.GeoJsonFeature | ||
import com.google.maps.android.data.geojson.GeoJsonLayer | ||
import org.json.JSONObject | ||
import java.lang.Exception | ||
|
||
class CapacitorGoogleMapsFeatureLayer( | ||
layer: Layer, | ||
feature: Feature, | ||
idPropertyName: String, | ||
styles: JSONObject | ||
) { | ||
var id: String = "" | ||
var layer: Layer? = null | ||
|
||
init { | ||
(feature as? GeoJsonFeature)?.let { | ||
val properties: HashMap<String, String> = hashMapOf() | ||
for (propertyKey in feature.propertyKeys) { | ||
properties[propertyKey] = feature.getProperty(propertyKey) | ||
} | ||
val feature = | ||
GeoJsonFeature( | ||
feature.geometry, | ||
feature.getProperty(idPropertyName), | ||
properties, | ||
null | ||
) | ||
id = feature.id | ||
this.layer = layer | ||
|
||
val featureLayer = (layer as GeoJsonLayer); | ||
featureLayer.addFeature(feature) | ||
|
||
try { | ||
featureLayer.defaultPolygonStyle.strokeColor = | ||
processColor(styles.getStyle("strokeColor"), styles.getStyle("strokeOpacity")) | ||
featureLayer.defaultPolygonStyle.strokeWidth = styles.getStyle("strokeWeight") | ||
featureLayer.defaultPolygonStyle.fillColor = | ||
processColor(styles.getStyle("fillColor"), styles.getStyle("fillOpacity")) | ||
featureLayer.defaultPolygonStyle.isGeodesic = styles.getStyle("geodesic") | ||
featureLayer.defaultLineStringStyle.color = | ||
featureLayer.defaultPolygonStyle.strokeColor | ||
featureLayer.defaultLineStringStyle.isGeodesic = | ||
featureLayer.defaultPolygonStyle.isGeodesic | ||
} catch (e: Exception) { | ||
throw InvalidArgumentsError("Styles object contains invalid values") | ||
} | ||
} | ||
} | ||
|
||
private fun processColor(hex: String, opacity: Double): Int { | ||
val colorInt = Color.parseColor(hex) | ||
|
||
val alpha = (opacity * 255.0).toInt() | ||
val red = Color.red(colorInt) | ||
val green = Color.green(colorInt) | ||
val blue = Color.blue(colorInt) | ||
|
||
return Color.argb(alpha, red, green, blue) | ||
} | ||
|
||
private fun <T> JSONObject.getStyle(key: String) = this.getJSONObject(id).get(key) as T | ||
} |
Oops, something went wrong.