Skip to content

Add Interactions API for iOS Tutorial #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
614 changes: 614 additions & 0 deletions ios-interactions/Interactions.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "tinted"
}
],
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions ios-interactions/Interactions/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
85 changes: 85 additions & 0 deletions ios-interactions/Interactions/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import SwiftUI
import MapboxMaps

struct ContentView: View {
@State var selectedPlaces = [StandardPlaceLabelsFeature]()
@State private var selectedPriceLabel: FeaturesetFeature?

var body: some View {
Map(initialViewport: .camera(center: .init(latitude: 40.72, longitude: -73.99), zoom: 11, pitch: 45)) {
StyleImport(id: "new-york-hotels", uri: StyleURI(url: styleURL)!)

TapInteraction(.standardPlaceLabels) { placeLabel, _ in
selectedPlaces.append(placeLabel)
return true
}

ForEvery(selectedPlaces, id: \.id) { placeLabel in
FeatureState(placeLabel, .init(select: true))
}

LongPressInteraction { _ in
selectedPlaces.removeAll()
return true
}

TapInteraction(.featureset("hotels-price", importId: "new-york-hotels")) { priceLabel, _ in
/// Select a price label when it's clicked
selectedPriceLabel = priceLabel
return true
}

if let selectedPriceLabel, let coordinate = selectedPriceLabel.geometry.point?.coordinates {
/// When there's a selected price label, we use it to set a feature state.
/// The `hidden` state is implemented in `new-york-hotels.json` and hides label and icon.
FeatureState(selectedPriceLabel, ["hidden": true])

/// Instead of label we show a callout annotation with animation.
MapViewAnnotation(coordinate: coordinate) {
HotelCallout(feature: selectedPriceLabel)
/// The `id` makes the view to be re-created for each unique feature
/// so appearing animation plays each time.
.id(selectedPriceLabel.id)
}
.variableAnchors([.init(anchor: .bottom)])
}
}
.mapStyle(.standard(
lightPreset: .dawn
))
.ignoresSafeArea()
}
}

private struct HotelCallout: View {
var feature: FeaturesetFeature

@State private var scale: CGFloat = 0.1

var body: some View {
VStack(alignment: .center, spacing: 2) {
Text(feature.properties["name"]??.string ?? "—")
.font(.headline)
.foregroundColor(.black)
Text(feature.properties["price"]??.number.map { "$ \(Int($0))" } ?? "—")
.font(.subheadline)
.foregroundColor(.green)
.fontWeight(.bold)
}
.padding(6)
.background(Color.white.opacity(0.9))
.cornerRadius(8)
.scaleEffect(scale, anchor: .bottom)
.onAppear {
withAnimation(Animation.interpolatingSpring(stiffness: 200, damping: 16)) {
scale = 1.0
}
}
}
}

private let styleURL = Bundle.main.url(forResource: "new-york-hotels", withExtension: "json")!

#Preview {
ContentView()
}
8 changes: 8 additions & 0 deletions ios-interactions/Interactions/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>MBXAccessToken</key>
<string>YOUR_MAPBOX_ACCESS_TOKEN</string>
</dict>
</plist>
10 changes: 10 additions & 0 deletions ios-interactions/Interactions/InteractionsApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SwiftUI

@main
struct InteractionsApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Loading