google-maps-ios-utils icon indicating copy to clipboard operation
google-maps-ios-utils copied to clipboard

GeoJSON render styles

Open returnchung opened this issue 9 years ago • 8 comments

I have implemented GeoJSON data with google-maps-ios-utils, but how could I config the rendering styles? In the documentation of Google Maps APIs, there is seems no example of "GMUStyles" configuration setting. I tried to setup as following but it's not working on styles change. (geometry features is working well)

----my codes example start here---- geoJsonParser = GMUGeoJSONParser(data: jsonData) geoJsonParser.parse() renderer = GMUGeometryRenderer(map: mapView, geometries: geoJsonParser.features, styles: [GMUStyle(styleID: "111", stroke: UIColor.red, fill: UIColor.red, width: 0.8, scale: 1.0, heading: 1.0, anchor: CGPoint(x: 0, y: 0), iconUrl: "non", title: "title", hasFill: false, hasStroke: true)]) renderer.render() ----my codes example end here----

returnchung avatar Oct 12 '17 11:10 returnchung

GeoJSON does not have a concept of styles like KML so you will need to manually associate the style object to a feature. You could loop through geoJsonParser.features and assign it's .style property to the GMUStyle object.

mountainvat avatar Oct 13 '17 00:10 mountainvat

Thank you @mountainvat, would you help explain how to associate the style object further? I found that each element in geoJsonParser.features.style is get-only property which means I cannot assign values to them. e.g. geoJsonParser.features[i].style.strokeColor = UIColor.red Whether there is a normal style format in geojson text structure so that I can retrieve each .style object directly as the GMUStyle object?

returnchung avatar Oct 13 '17 04:10 returnchung

The GMUStyle object is immutable but the property GMUGeometryContainer.style is not. You can assign that property to the style object (https://github.com/googlemaps/google-maps-ios-utils/blob/master/src/Geometry/Model/GMUGeometryContainer.h#L36). GeoJSON does not support styles in its format (at least at the time of writing the library), that's why you'll have to set them manually.

mountainvat avatar Oct 13 '17 04:10 mountainvat

Yeah, I got it finally. It's really good explanation so that I could understand the model more. Many thanks to @mountainvat.

returnchung avatar Oct 13 '17 05:10 returnchung

Just in case someone else comes across this, I did the following to get it to work:

let path = Bundle.main.path(forResource: "countries", ofType: "json")
let url = URL(fileURLWithPath: path!)
let geoJsonParser = GMUGeoJSONParser(url: url)
geoJsonParser.parse()
        
let style = GMUStyle(styleID: "random", stroke: UIColor.blue, fill: UIColor.orange.withAlphaComponent(0.5), width: 2, scale: 1, heading: 0, anchor: CGPoint(x: 0, y: 0), iconUrl: nil, title: nil, hasFill: true, hasStroke: true)
        
for feature in geoJsonParser.features {
  feature.style = style
}
        
renderer = GMUGeometryRenderer(map: gmapView, geometries: geoJsonParser.features)
renderer.render()

sylvan-d-ash avatar May 22 '18 11:05 sylvan-d-ash

let style = GMUStyle(styleID: "random", stroke: .red, fill: .clear, width: 1, scale: 1, heading: 0, anchor: CGPoint(x: 0, y: 0), iconUrl: nil, title: "Test", hasFill: false, hasStroke: true)

disable fill not working? Plz help

bf38da462219c2479b08

fukemy avatar Sep 12 '18 07:09 fukemy

@fukemy try setting hasFill: true and fill: .clear

sylvan-d-ash avatar Sep 12 '18 08:09 sylvan-d-ash

hello. I fixed it by modifying GMUGeoJSONParser class, just set default for Polyline fill color. Plz help my question: I want to highlight geojson feature on click. I dont know the way to do it

fukemy avatar Sep 12 '18 08:09 fukemy