Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Add MGLCircle (with radius expressed in physical units) #2167

Open

Description

@incanus

Now that v8 has landed in 0451aca we have a Circle layer type.

Activity

  1. added
    iOS
    on Aug 24, 2015
  2. 1ec5 commented on Aug 24, 2015

    @1ec5
    Contributor

    ref #1740

  3. cowgp commented on Oct 27, 2015

    @cowgp

    @incanus is there a roadmap plan for when MGLCircle will be implemented? Trying to update from the old "RMMapView" based SDK and the lack of replacement for an RMCircle is blocking me.

  4. self-assigned this
    on Oct 28, 2015
  5. incanus commented on Oct 28, 2015

    @incanus
    ContributorAuthor

    Thanks for the ping on this @cowgp. It hasn't been on the roadmap, but thinking this was relatively straightforward, I took a look today in 2167-MGLCircle.

    The Cocoa side was pretty easy, but as it turns out, even though we have support for circles in the renderer and styles as of 0451aca, we don't have support for circle annotations in that API.

    It's an interesting bind, since circles seems like shapes (have alpha & color) but behave like points (have a single coordinate and a scalar icon-like property radius). So the going has been a little more than I expected there, but I think I'm close.

    Stay tuned — will try to push this through soon.

    /cc @friedbunny @jfirebaugh

  6. incanus commented on Oct 28, 2015

    @incanus
    ContributorAuthor

    @cowgp Sorry, I'm gonna have to say we're going to push this out a ways right now — too many higher priorities. If it was as easy as I had initially guessed, with pure Cocoa API, it would have been easy to rationalize, but increasing the surface area of our annotations API right now is not ideal given the other things we're integrating them with.

    Stay tuned; I'd love to get this in, but it's not a priority roadmap right now.

  7. removed their assignment
    on Oct 28, 2015
  8. cowgp commented on Oct 28, 2015

    @cowgp

    @incanus - Thanks so much for looking into it, I appreciate the effort. I do hope it can remain a priority, seems like an important feature from my perspective. That said, totally understand that this update is all new underpinnings and thus there's a lot to build up.

  9. incanus commented on Oct 28, 2015

    @incanus
    ContributorAuthor

    @cowgp Depending on if your needs are truly at runtime, you could possibly use the existing support in the style spec for rendering circles in the meantime? mapbox/mapbox-gl-js#1446 shows an example of how this looks in the style. You could also consider mutating the style JSON and custom setting it at runtime as a workaround.

  10. cowgp commented on Nov 9, 2015

    @cowgp

    @incanus - I looked into the style spec you linked, but no it doesn't seem to be a viable solve for our needs. We have ~45k active users each with one or more geofences that they can set/modify at will. This is accomplished in the existing deprecated SDK by adding an annotation at the center point of the geofence and then in the layerForAnnotation delegate method returning an RMCircle with the correct radius in meters for the fence. It seems to me a clear case where an MGLCircle is necessary to have in the new SDK.

  11. incanus commented on Nov 9, 2015

    @incanus
    ContributorAuthor

    What I meant about mutating the style, though, could be a workaround here. The idea would be that you'd have a parsed representation of the style JSON (as, say, an NSMutableDictionary) to which you could add/modify items in the layers array using the circle primitive. Then, you'd send this out to JSON data with NSJSONSerialization and use it to set the styleJSON in the library.

    Admittedly more work than just having MGLCircle, but an idea.

  12. EasyAsABC123 commented on Nov 19, 2015

    @EasyAsABC123

    Is this still not available with the new release of mapbox studio?

  13. incanus commented on Nov 19, 2015

    @incanus
    ContributorAuthor

    Nope, no further movement here just yet. Workaround is to code circle primitives into your style and/or add them to the style JSON at runtime for now.

  14. 42 remaining items

  15. removed
    archived
    on Jan 7, 2019
  16. jlubeck commented on Feb 7, 2019

    @jlubeck

    Any news on this front? (trying to get the bot not to close it again)

  17. KaneCheshire commented on Feb 7, 2019

    @KaneCheshire

    Given it’s 3 years old, would be nice to have some sort of update on the issue for sure.

  18. julianrex commented on Mar 6, 2019

    @julianrex
    Contributor

    @jlubeck @KaneCheshire just wanted to assure you that this is on our list - and you should start seeing some movement on this issue soon.

  19. KaneCheshire commented on Mar 6, 2019

    @KaneCheshire

    Thanks for the update @julianrex

  20. jlubeck commented on Mar 6, 2019

    @jlubeck

    Great to hear that @julianrex !

  21. captainbarbosa commented on Apr 24, 2019

    @captainbarbosa
    Contributor

    @jlubeck @KaneCheshire - After some internal discussions we are currently planning a major refactor to how shapes are generated to be added to a map. The current plan is to wait until that work is done before proceeding to address this issue. I know this is not ideal, but we believe the upcoming refactoring we’re working on will make adding all sorts of shapes to a map much easier for everyone in the long run. We’ll be sure to post back here as soon as we have more updates.

    If you're looking for a workaround right now, one possibility would be to implement this with turf-swift. There are limitations to this approach though - this is not a true circle, but rather a an n-sided polygon (I just used the default 64 from the Turf.js documentation. Here is an example of my implementation. In addition, you can also update the shape of this circle on-the-fly using an MGLMapViewDelegate method that responds to map position changes.

  22. jlubeck commented on Apr 24, 2019

    @jlubeck

    Thanks for the heads up @captainbarbosa

  23. added
    ⚠️ DO NOT MERGE
    on Jun 11, 2019
  24. KaneCheshire commented on Jul 17, 2019

    @KaneCheshire

    How's the refactoring going? This issue will be 4 years old in just over a month :/

  25. chloekraw commented on Aug 7, 2019

    @chloekraw
    Contributor

    All, thanks for following this ticket. Our refactor is going well, and we are nearing a go/no-go decision on merging #14534 sometime this year after we resolve the remaining considerations.

    Please upvote this comment if #14534 would work for your use case and solve your needs.

    Per its description, the PR implements a new MGLCircle class, analogous to MapKit’s MKCircle or the Google Map SDK’s GMSCircle, which allows the developer to add spherical caps to the map view as overlays or to a shape source as polygons.

  26. jlubeck commented on Feb 10, 2020

    @jlubeck

    Any news on this issue? Looks like it was on good track back in August... And Github is returning a 500 error on the pull request so I can't see if there is progress there... Weird thing is that is the only PR that is returning an error, the others work just fine

    Thanks!

  27. hdmdhr commented on Mar 8, 2021

    @hdmdhr

    Inspired by @incanus's answer in 2017, I came up with another solution, which doesn't requires usage of MGLCircleStyleLayer, but uses mapView(_:viewFor annotation:) -> MGLAnnotationView? delegate method.

    Code below is written in Swift 5, using Mapbox-iOS-SDK 6.1.0, The performance seems ok on a simulator.

    // Code below only covers 1 annotation use case for demo purpose, make changes if you need to support multiple annotations
    class MyMapBoxViewController: UIViewController, MGLMapViewDelegate {
        
        /// Use this stored property to track my circle
        private var circleAnnotationView: MGLAnnotationView?
        private let radiusMeters: Double = 200
        private let divisor: Double = 2
        ...
        
        /// Return a clear image instead of default pin image, so the annotation will be invisible.
        /// if you do not want to hide annotation, comment this method out
        func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
            let clearImage = mapView.dequeueReusableAnnotationImage(withIdentifier: "clear") ??
                .init(image: UIImage(color: .clear)!, reuseIdentifier: "clear")
            return clearImage
        }
        
        /// Return a circular `MGLAnnotationView` for the annotation, use ViewController's stored property `circleAnnotationView` to track it
        func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
            // use lng, lat to create a unique reuse identifier
            let reuseIdentifier = "\(annotation.coordinate.longitude)-\(annotation.coordinate.latitude)"
            
            // reuse or create a circle view around the annotation
            circleAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) ??
                CustomAnnotationView(reuseIdentifier: reuseIdentifier)
            var radiusPoints = radiusMeters / mapView.metersPerPoint(atLatitude: annotation.coordinate.latitude)
            radiusPoints /= divisor  // divide by (2) here so circle doesn't fill the entire screen, please choose your own divisor
            circleAnnotationView?.bounds = .init(origin: .zero, size: .init(width: radiusPoints, height: radiusPoints))
            circleAnnotationView?.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
            
            return circleAnnotationView
        }
        
        /// When map zooms, rescale `circleAnnotationView` based on zoom level
        func mapViewRegionIsChanging(_ mapView: MGLMapView) {
            guard circleAnnotationView != nil else { return }
            var radiusPoints = radiusMeters / mapView.metersPerPoint(atLatitude: mapView.centerCoordinate.latitude)
            radiusPoints /= divisor  // divide by (2) here so circle doesn't fill the entire screen, please choose your own divisor
            circleAnnotationView?.bounds = .init(origin: .zero, size: .init(width: radiusPoints, height: radiusPoints))
        }
        
    }

    Some subclass and extension used in code above

    class CustomAnnotationView: MGLAnnotationView {
        override func layoutSubviews() {
            super.layoutSubviews()
            
            // Use CALayer’s corner radius to turn this view into a circle.
            layer.cornerRadius = bounds.width / 2
            layer.borderWidth = 2
            layer.borderColor = UIColor.textBlue.cgColor
        }
    }
    
    extension UIImage {
    
        /// Use color to init an `UIImage`
        convenience init?(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) {
            let rect = CGRect(origin: .zero, size: size)
            UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
            color.setFill()
            UIRectFill(rect)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            guard let cgImage = image?.cgImage else { return nil }
            self.init(cgImage: cgImage)
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Google Maps parityMapKit parityfeaturegl-iosiOSproduct⚠️ DO NOT MERGE

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions