Skip to content
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

Add ability to move map to user-entered coordinates using latitude and longitude text fields and button #166

Merged
merged 3 commits into from
May 15, 2023
Merged
Changes from 2 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
57 changes: 39 additions & 18 deletions ControlRoom/Simulator UI/ControlScreens/LocationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@

import MapKit
import SwiftUI
import CoreLocation

/// Map view to change simulated user's position
struct LocationView: View {
@ObservedObject var controller: SimulatorsController
let simulator: Simulator

@State private var latitudeText = "37.323056"
@State private var longitudeText = "-122.031944"
/// The location that is being simulated
@State private var currentLocation = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 37.323056, longitude: -122.031944),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to use constants here and in lines 18, 19 to easily change those in the future.

e.g.

DEFAULT_LAT = 37.323056
DEFAULT_LNG = -122.031944

18, 19 become:

    @State private var latitudeText = "\(DEFAULT_LAT)"
    @State private var longitudeText = "\(DEFAULT_LNG)"
Suggested change
center: CLLocationCoordinate2D(latitude: 37.323056, longitude: -122.031944),
center: CLLocationCoordinate2D(latitude: DEFAULT_LAT, longitude: DEFAULT_LNG),

Expand Down Expand Up @@ -43,27 +46,45 @@ struct LocationView: View {

var body: some View {
Form {
Text("Move the map wherever you want, then click Activate to update the simulator to match your centered coordinate.")
VStack {
Text("Move the map wherever you want, then click Activate to update the simulator to match your centered coordinate.")
HStack(spacing: 10.0) {
TextField("Latitude", text: $latitudeText)
.textFieldStyle(.roundedBorder)

ZStack {
Map(coordinateRegion: $currentLocation, annotationItems: annotations) { location in
MapMarker(coordinate: CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude), tint: .red)
TextField("Longitude", text: $longitudeText)
.textFieldStyle(.roundedBorder)
}
Button("Update coordinates") {
if let latitude = Double(latitudeText),
let longitude = Double(longitudeText) {
self.currentLocation = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: latitude, longitude: longitude),
span: MKCoordinateSpan(latitudeDelta: 15, longitudeDelta: 15))
}
}
.cornerRadius(5)

Circle()
.stroke(Color.blue, lineWidth: 4)
.frame(width: 20)
}
.padding(.bottom, 10)

HStack {
Text("Coordinates: \(locationText)")
.textSelection(.enabled)
Spacer()
Toggle("Jitter location", isOn: $isJittering)
.toggleStyle(.checkbox)
Button("Activate", action: changeLocation)
ZStack {
Map(coordinateRegion: $currentLocation, annotationItems: annotations) { location in
MapMarker(coordinate: CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude), tint: .red)
}
.cornerRadius(5)

Circle()
.stroke(Color.blue, lineWidth: 4)
.frame(width: 20)
}
.padding(.bottom, 10)
.keyboardShortcut(/*@START_MENU_TOKEN@*/.defaultAction/*@END_MENU_TOKEN@*/)

HStack {
Text("Coordinates: \(locationText)")
.textSelection(.enabled)
Spacer()
Toggle("Jitter location", isOn: $isJittering)
.toggleStyle(.checkbox)
Button("Activate", action: changeLocation)
}
}
}
.tabItem {
Expand Down