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

Interacting with annotation points in style layer #6505

Closed
rmnblm opened this issue Sep 28, 2016 · 3 comments
Closed

Interacting with annotation points in style layer #6505

rmnblm opened this issue Sep 28, 2016 · 3 comments
Labels
iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling

Comments

@rmnblm
Copy link

rmnblm commented Sep 28, 2016

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.

@1ec5
Copy link
Contributor

1ec5 commented Sep 29, 2016

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.

@1ec5 1ec5 added iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling labels Sep 29, 2016
@rmnblm
Copy link
Author

rmnblm commented Sep 30, 2016

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)
}

@rmnblm
Copy link
Author

rmnblm commented Sep 30, 2016

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
iOS Mapbox Maps SDK for iOS macOS Mapbox Maps SDK for macOS runtime styling
Projects
None yet
Development

No branches or pull requests

2 participants