Skip to content

Commit

Permalink
feat(google-maps): Refactored map feature function with optional idPr…
Browse files Browse the repository at this point in the history
…opertyName and styles parameter
  • Loading branch information
A1Jan committed Aug 2, 2023
1 parent b7aa792 commit ce26ce3
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 111 deletions.
2 changes: 1 addition & 1 deletion google-maps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ removePolylines(ids: string[]) => Promise<void>
### addFeatures(...)

```typescript
addFeatures(type: FeatureType, data: any, idPropertyName: string, styles: FeatureStyles) => Promise<string[]>
addFeatures(type: FeatureType, data: any, idPropertyName?: string, styles?: FeatureStyles) => Promise<string[]>
```

| Param | Type |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ class CapacitorGoogleMap(
}
}

fun addFeatures(type: String, data: JSONObject, idPropertyName: String, styles: JSONObject, callback: (ids: Result<List<String>>) -> Unit) {
fun addFeatures(type: String, data: JSONObject, idPropertyName: String?, styles: JSONObject?, callback: (ids: Result<List<String>>) -> Unit) {
try {
googleMap ?: throw GoogleMapNotAvailable()
val featureIds: MutableList<String> = mutableListOf()
Expand All @@ -342,15 +342,18 @@ class CapacitorGoogleMap(
val layer = GeoJsonLayer(googleMap, JSONObject())
val featureLayer = CapacitorGoogleMapsFeatureLayer(layer, it, idPropertyName, styles)
layer.addLayerToMap()
featureIds.add(featureLayer.id)
featureLayers[featureLayer.id] = featureLayer
val id = featureLayer.id
if (id != null) {
featureIds.add(id)
featureLayers[id] = featureLayer
}
callback(Result.success(featureIds))
} catch (e: Exception) {
callback(Result.failure(e))
}
}
} else {
callback(Result.failure(Error("addFeatures: not supported for this feature type")))
callback(Result.failure(InvalidArgumentsError("addFeatures: not supported for this feature type")))
}
}
} catch (e: GoogleMapsError) {
Expand All @@ -375,14 +378,14 @@ class CapacitorGoogleMap(
callback(Result.success(it.boundingBoxFromGeometry()))
}
} catch (e: Exception) {
callback(Result.failure(Error("getFeatureBounds: not supported for this feature type")))
callback(Result.failure(InvalidArgumentsError("getFeatureBounds: not supported for this feature type")))
}
} else {
callback(Result.failure(Error("Could not find feature for feature id $featureId")))
callback(Result.failure(InvalidArgumentsError("Could not find feature for feature id $featureId")))
}
}
} catch(e: Exception) {
callback(Result.failure(Error("Could not find feature layer")))
callback(Result.failure(InvalidArgumentsError("Could not find feature layer")))
}
}

Expand All @@ -392,9 +395,10 @@ class CapacitorGoogleMap(
if (featurelayer != null) {
featurelayer.layer?.removeLayerFromMap()
featureLayers.remove(featureId)
callback(null)
} else {
callback(InvalidArgumentsError("Could not find feature for feature id $featureId"))
}

callback(null)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import java.lang.Exception
class CapacitorGoogleMapsFeatureLayer(
layer: Layer,
feature: Feature,
idPropertyName: String,
styles: JSONObject
idPropertyName: String?,
styles: JSONObject?
) {
var id: String = ""
var id: String? = null
var layer: Layer? = null

init {
Expand All @@ -23,32 +23,39 @@ class CapacitorGoogleMapsFeatureLayer(
for (propertyKey in feature.propertyKeys) {
properties[propertyKey] = feature.getProperty(propertyKey)
}
if (idPropertyName != null) {
id = feature.getProperty(idPropertyName)
}
val feature =
GeoJsonFeature(
feature.geometry,
feature.getProperty(idPropertyName),
id,
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")
if (styles != null) {
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")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,28 +606,30 @@ class CapacitorGoogleMapsPlugin : Plugin() {
call.getObject("data")
?: throw InvalidArgumentsError("feature data is missing")

val idPropertyName =
call.getString("idPropertyName")
?: throw InvalidArgumentsError("feature idPropertyName is missing")
val idPropertyName = call.getString("idPropertyName") ?: null

val styles =
call.getObject("styles")
?: throw InvalidArgumentsError("feature styles missing")
val styles = call.getObject("styles") ?: null

map.addFeatures(
type,
data,
idPropertyName,
styles
) { result ->
val ids = result.getOrThrow()

val jsonIDs = JSONArray()
ids.forEach { jsonIDs.put(it) }

val res = JSObject()
res.put("ids", jsonIDs)
call.resolve(res)
try {
val ids = result.getOrThrow()

val jsonIDs = JSONArray()
ids.forEach { jsonIDs.put(it) }

val res = JSObject()
res.put("ids", jsonIDs)
call.resolve(res)
} catch (e: GoogleMapsError) {
handleError(call, e)
} catch (e: Exception) {
handleError(call, e)
}
}
} catch (e: GoogleMapsError) {
handleError(call, e)
Expand All @@ -650,28 +652,34 @@ class CapacitorGoogleMapsPlugin : Plugin() {
?: throw InvalidArgumentsError("feature id is missing")

map.getFeatureBounds(featureId) { result ->
val featureBounds = result.getOrThrow()

val southwest = JSObject()
southwest.put("lat", featureBounds?.southwest?.latitude)
southwest.put("lng", featureBounds?.southwest?.longitude)

val center = JSObject()
center.put("lat", featureBounds?.center?.latitude)
center.put("lng", featureBounds?.center?.longitude)

val northeast = JSObject()
northeast.put("lat", featureBounds?.northeast?.latitude)
northeast.put("lng", featureBounds?.northeast?.longitude)

val bounds = JSObject()
bounds.put("southwest", southwest)
bounds.put("center", center)
bounds.put("northeast", northeast)

val res = JSObject()
res.put("bounds", bounds)
call.resolve(res)
try {
val featureBounds = result.getOrThrow()

val southwest = JSObject()
southwest.put("lat", featureBounds?.southwest?.latitude)
southwest.put("lng", featureBounds?.southwest?.longitude)

val center = JSObject()
center.put("lat", featureBounds?.center?.latitude)
center.put("lng", featureBounds?.center?.longitude)

val northeast = JSObject()
northeast.put("lat", featureBounds?.northeast?.latitude)
northeast.put("lng", featureBounds?.northeast?.longitude)

val bounds = JSObject()
bounds.put("southwest", southwest)
bounds.put("center", center)
bounds.put("northeast", northeast)

val res = JSObject()
res.put("bounds", bounds)
call.resolve(res)
} catch (e: GoogleMapsError) {
handleError(call, e)
} catch (e: Exception) {
handleError(call, e)
}
}
} catch (e: GoogleMapsError) {
handleError(call, e)
Expand All @@ -694,7 +702,13 @@ class CapacitorGoogleMapsPlugin : Plugin() {
?: throw InvalidArgumentsError("feature id is missing")

map.removeFeature(featureId) {
call.resolve()
try {
call.resolve()
} catch (e: GoogleMapsError) {
handleError(call, e)
} catch (e: Exception) {
handleError(call, e)
}
}
} catch (e: GoogleMapsError) {
handleError(call, e)
Expand Down
8 changes: 2 additions & 6 deletions google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,9 @@ public class CapacitorGoogleMapsPlugin: CAPPlugin, GMSMapViewDelegate {
throw GoogleMapErrors.invalidArguments("feature data is missing")
}

guard let idPropertyName = call.getString("idPropertyName") else {
throw GoogleMapErrors.invalidArguments("feature idPropertyName is missing")
}
let idPropertyName = call.getString("idPropertyName")

guard let styles = call.getObject("styles") else {
throw GoogleMapErrors.invalidArguments("feature styles missing")
}
let styles = call.getObject("styles")

let ids = try map.addFeatures(type: type, data: data, idPropertyName: idPropertyName, styles: styles)

Expand Down
52 changes: 33 additions & 19 deletions google-maps/ios/Plugin/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public class Map {
}
}

func addFeatures(type: String, data: JSObject, idPropertyName: String, styles: JSObject) throws -> [String] {
func addFeatures(type: String, data: JSObject, idPropertyName: String?, styles: JSObject?) throws -> [String] {
var featureIds: [String] = []

DispatchQueue.main.sync {
Expand All @@ -382,32 +382,46 @@ public class Map {
geoJSONParser.parse()

for container in geoJSONParser.features {
if let tempFeature = container as? GMUFeature, let propertes = tempFeature.properties, let featureId = propertes[idPropertyName] as? String {
if let renderer = self.features[featureId] {
renderer.clear()
self.features.removeValue(forKey: featureId)
if let tempFeature = container as? GMUFeature, let propertes = tempFeature.properties {
var featureId: String? = nil
if (idPropertyName != nil) {
featureId = propertes[idPropertyName!] as? String

if (featureId != nil) {
if let renderer = self.features[featureId!] {
renderer.clear()
self.features.removeValue(forKey: featureId!)
}
}
}

let feature = GMUFeature(geometry: tempFeature.geometry, identifier: featureId, properties: tempFeature.properties, boundingBox: nil)

featureIds.append(featureId)

if let stylesData = (styles as [String: Any])[featureId] as? [String: Any] {
if let strokeColor = stylesData["strokeColor"] as? String,
let strokeOpacity = stylesData["strokeOpacity"] as? Double,
let stroke = UIColor.init(hex: strokeColor)?.withAlphaComponent(strokeOpacity),
let fillColor = stylesData["fillColor"] as? String,
let fillOpacity = stylesData["fillOpacity"] as? Double,
let fill = UIColor.init(hex: fillColor)?.withAlphaComponent(fillOpacity) {
let style = GMUStyle(styleID: "styleId", stroke: stroke, fill: fill, width: stylesData["strokeWeight"] as? Double ?? 1, scale: 2, heading: 0, anchor: CGPoint(x: 0, y: 0), iconUrl: nil, title: nil, hasFill: true, hasStroke: true)
feature.style = style
if (featureId != nil) {
featureIds.append(featureId!)
}

if (featureId != nil && styles != nil) {
if let stylesData = (styles! as [String: Any])[featureId!] as? [String: Any] {
if let strokeColor = stylesData["strokeColor"] as? String,
let strokeOpacity = stylesData["strokeOpacity"] as? Double,
let stroke = UIColor.init(hex: strokeColor)?.withAlphaComponent(strokeOpacity),
let fillColor = stylesData["fillColor"] as? String,
let fillOpacity = stylesData["fillOpacity"] as? Double,
let fill = UIColor.init(hex: fillColor)?.withAlphaComponent(fillOpacity)
{
let style = GMUStyle(styleID: "styleId", stroke: stroke, fill: fill, width: stylesData["strokeWeight"] as? Double ?? 1, scale: 2, heading: 0, anchor: CGPoint(x: 0, y: 0), iconUrl: nil, title: nil, hasFill: true, hasStroke: true)
feature.style = style
}
}
}

let renderer = GMUGeometryRenderer(map: self.mapViewController.GMapView, geometries: [feature])
renderer.render()

self.features[featureId] = renderer

if (featureId != nil) {
self.features[featureId!] = renderer
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions google-maps/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ export interface AddFeatureArgs {
id: string;
type: FeatureType;
data: any;
idPropertyName: string;
styles: FeatureStyles;
idPropertyName?: string;
styles?: FeatureStyles;
}

export interface GetFeatureBoundsArgs {
Expand Down
8 changes: 4 additions & 4 deletions google-maps/src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export interface GoogleMapInterface {
addFeatures(
type: FeatureType,
data: any,
idPropertyName: string,
styles: FeatureStyles,
idPropertyName?: string,
styles?: FeatureStyles,
): Promise<string[]>;
getFeatureBounds(featureId: string): Promise<LatLngBounds>;
removeFeature(featureId: string): Promise<void>;
Expand Down Expand Up @@ -382,8 +382,8 @@ export class GoogleMap {
async addFeatures(
type: FeatureType,
data: any,
idPropertyName: string,
styles: FeatureStyles,
idPropertyName?: string,
styles?: FeatureStyles,
): Promise<string[]> {
const res = await CapacitorGoogleMaps.addFeatures({
id: this.id,
Expand Down
Loading

0 comments on commit ce26ce3

Please sign in to comment.