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.

Interacting with annotation points in style layer #6505

Closed

Description

@rmnblm

Platform: iOS 10, Swift 3
Mapbox SDK version: 3.4.0-alpha.4

First: Let me thank the Mapbox community. I'm using Mapbox since more than a week and may have seen just the tip of the iceberg, still learning a lot of things everyday, and I'm really impressed about the fast responsiveness of this community.

While I'm currently building an app for a client using the Mapbox iOS SDK, knowing that there a lot of features to come in the future (especially runtime styling), I often miss a few things that'd be great for future releases (and that's why I'm always writing down my thoughts and ideas which can help improve Mapbox for iOS. Because of the lack of documentation for the newest version of Mapbox iOS SDK, I first check in code, if there is a possible solution to my problem, if not, I check if there's a planned feature/issue written down in the repo, if not, I'm going to write an issue).

Now to my question: Is there a way to interact with annotation points in a style layer coming from a style URL? Because public func mapView(_ mapView: MGLMapView, didSelect annotation: MGLAnnotation) isn't called when I touch a point in a style layer.

Activity

  1. 1ec5 commented on Sep 29, 2016

    @1ec5
    Contributor

    Unlike annotations, style layers don’t come with interactivity and callout views out of the box. Instead, you have to build it yourself: install your own tap gesture recognizer, call MGLMapView.visibleFeatures(at:styleLayerIdentifiers:), and position your own custom callout view over the feature’s coordinate (using convert(_:toPointTo:). Hide the callout view in mapView(_:regionWillChange:), or update its location continuously in mapView(_:regionIsChanging:) and mapView(_:regionDidChange:).

    I know this sounds like a lot of work. #6181 would make annotations more like style layers, in the process adding a lot of annotation-like functionality to style layers in general. That’s quite a ways off, however. #6515 would make the gesture recognition part a little easier, and we could add an API for displaying callout views for non-annotations as part of #4392. Meanwhile, under the hood, annotations are already implemented using style layers to some extent; once #6097 lands, you’ll be able to use an annotation but otherwise treat it like part of a style layer, more or less.

  2. added
    iOS
    macOS
    on Sep 29, 2016
  3. rmnblm commented on Sep 30, 2016

    @rmnblm
    Author

    Works perfectly, thanks for your help @1ec5. Here's how I did it (for others):

    override func viewDidLoad() {
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        mapView.addGestureRecognizer(tapGestureRecognizer)
    
        mapView.styleURL = URL(string: "YOUR_STYLE_URL")
        let center = CLLocationCoordinate2D(latitude: 37.383925, longitude: -122.07135)
        mapView.setCenter(center, zoomLevel: 11.0, animated: false)
    }
    
    func handleTap(recognizer: UITapGestureRecognizer) {
        let location = recognizer.location(in: mapView)
        let touchableSquare = squareFrom(location: location)
        for feature in mapView.visibleFeatures(in: touchableSquare) {
            print(feature.attributes)
        }
    }
    
    private func squareFrom(location: CGPoint) -> CGRect {
        let length = 50.0
        return CGRect(x: Double(location.x - CGFloat(length / 2)), y: Double(location.y - CGFloat(length / 2)), width: length, height: length)
    }
    
  4. rmnblm commented on Sep 30, 2016

    @rmnblm
    Author

    Maybe found a bug with the solution above, see issue #6538

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

    iOSmacOSruntime styling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions