Hi I am trying to annotate on map after displaying the map. I am able to take in lat and long from user and convert it to Double. But stuck at the annotate on the map part.
I am able to annotate hard coded values but I do not know how to convert it to annotate using user`s input of lat and long.
I struggling to include the init( ...) part into my identifiable portion. I cant seem to get it right.
I found this link from apple but it doesnt work.
https://developer.apple.com/documentation/mapkit/mapmarker#see-also
import MapKit
import SwiftUI
struct IdentifiablePlace: Identifiable {
let id: UUID
let location: CLLocationCoordinate2D
init(id: UUID = UUID(), lat: Double, long: Double) {
self.id = id
self.location = CLLocationCoordinate2D(
latitude: lat,
longitude: long)
}
}
struct PinAnnotationMapView: View {
let place: IdentifiablePlace
@State var region: MKCoordinateRegion
var body: some View {
Map(coordinateRegion: $region,
annotationItems: [place])
{ place in
MapMarker(coordinate: place.location,
tint: Color.purple)
}
}
}
Thanks
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
0
I am trying to implement input textfield for user to enter 'longitude' and 'latitude' to make annotation on the map.
The scenario I want to achieve: When user click 'save' button after filling in both longitude and latitude. The annotation will appear based on the latitude and longitude inputs. I got 2 strings, "savedLong" and "savedLat".
Please advice me if Im on the right track to solving my issue. Thank you!
I get errors when I replace the Longitude and Latitude values with 'savedLong' and 'savedLat', example below is a portion of my current progress,
MyAnnotationItem(
coordinate: CLLocationCoordinate2D(
latitude: savedLat, //instead of numbers that is able to work
longitude: savedLong), //instead of numbers that is able to work
color: .green)]
Code below will save user input for both Latitude and Longitude,
VStack{
TextField("Latitude", text: $textFieldLat)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
.background(Color.gray.opacity(0.3).cornerRadius(10))
.foregroundColor(.red)
.font(.headline)
TextField("Longitude", text: $textFieldLong)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
.background(Color.gray.opacity(0.3).cornerRadius(10))
.foregroundColor(.red)
.font(.headline)
Button(action: {
savedLat = textFieldLat ////<-- saved as latitude to plot
savedLong = textFieldLong////<--- saved as longitude to plot
}, label: {
Text("Save".uppercased())
.padding()
.background(Color.gray.opacity(0.3).cornerRadius(10))
.foregroundColor(.red)
.font(.headline)
})
Text("Lat: "+savedLat+" Long: "+savedLong)
Code below is my current progress and is able to run.
import MapKit
import SwiftUI
struct ContentView: View {
@StateObject private var viewModel = ContentViewModel()
struct AnnotatedItem: Identifiable {
let id = UUID()
var name: String
var coordinate: CLLocationCoordinate2D
}
private var pointsOfInterest = [ //annotate on map
AnnotatedItem(name: "Location A", coordinate:
.init(latitude: 5.76123, longitude: 156.52318))
]
var body: some View {
Map(coordinateRegion: $viewModel.region,
showsUserLocation: true, annotationItems: pointsOfInterest){
item in
MapMarker(coordinate: item.coordinate)
}
.ignoresSafeArea() //full screen
.accentColor(Color(.systemPink)) //color of icon
.onAppear {
viewModel.checkIfLocationServicesIsEnabled()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
enum MapDetails {
static let startingLocation = CLLocationCoordinate2D(latitude: 5.76123, longitude: 156.52318)
static let defaultSpan = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
}
final class ContentViewModel: NSObject, ObservableObject,
CLLocationManagerDelegate {
@Published var region = MKCoordinateRegion(center: MapDetails.startingLocation,
span: MapDetails.defaultSpan)
var locationManager: CLLocationManager?
func checkIfLocationServicesIsEnabled(){
if CLLocationManager.locationServicesEnabled(){
locationManager = CLLocationManager()
locationManager!.delegate = self
}else{
print("Show map is off")
}
}
private func checkLocationAuthorization(){
guard let locationManager = locationManager else {return}
switch locationManager.authorizationStatus{
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .restricted:
print("Location restricted")
case .denied:
print("Location denied")
case .authorizedAlways, .authorizedWhenInUse:
region = MKCoordinateRegion(center: locationManager.location!.coordinate,
span: MapDetails.defaultSpan)
break
@unknown default:
break
}
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
checkLocationAuthorization()
}
}
}
Hi all,
Im doing a API using socket.io. I want to store one of the line from final class into my button action which is " socket.emit("Server Port","Hi server111")". When i declare the " socket.emit("Server Port","Hi server111")".
The code is working and will send the msg when connected to server. What I am trying to do is to do a continuous send to server after connected which I am stuck at.
My last line of code have an error
Value of type '(Int32, Int32, Int32) -> Int32' has no member 'emit'
Thank you for reading.
final class Service: ObservableObject {
private var manager = SocketManager(socketURL: URL(string:"ws://localhost:4005")!, config:[.log(true), .compress])
init(){
let socket = manager.defaultSocket
socket.on(clientEvent: .connect){ (data, ack) in //connect and send/emit
print("Connected")
DispatchQueue.main.async{
socket.emit("Server Port","Hi server111")
}}
struct ContentView: View {
var body: some View {
@ObservedObject var service = Service()
VStack{
Text("Received messages from Node.js")
.font(.largeTitle)
TextField("Type Smth:", text: $test)
Text("Output: \(test)")
Button("Save", action:{
socket.emit("Server Port","Hi server111") // <<--
My Xcode does not seem to recognise the socket.io package uploaded from github. The socket.io package I see on youtube examples have the sub files arrow and different file title from mine. I did the exact same but the uploading seems to be incorrect.
Example:
https://github.com/socketio/socket.io-client-swift
On my Xcode:
Is there any other function out there other than socket.io for iOS (Swift) to Android to communicate via wifi/IP Address?
Thank you
Hi,
I want to import swift packages from GitHub, but my xcode 13.1 do not have the 'swift packages' under the file list as shown an example I watched online. My Xcode file list only have 'packages' instead of 'swift packages'.
Please assist!
Thank you!
I need help communicating between 2 ios app.
I want to send a signal from Macbook to Ipad via Wifi using SwiftUI.
When I press a Button on MacBook SwiftUI,
iPad SwiftUI app will receive the signal and changes the display from red to black.
I could not find any example online.
Hi all,
I need help sending a 1/0 signal from iPad/macbook to iPad/macbook via wifi using SwiftUI. This does not require any memory, just sending a 1/0 signal.
I am trying to do smth similar to embedded system.
On Macbook Swift UI app, when I click the button "Go". It will send a signal via wifi to the Ipad which is also in the swift UI app, to change its display.
The Macbook functions like a controller, when press 'On' on the UI, it will turn on the display on iPad UI.
Cant find any example online regarding this problem.
Thank you!
MacBook 0 ->0 -> 0 iPad Display unchanged
MacBook 1 -> 1 -> 1 iPad Display Changed when receives a signal from Macbook Via Wifi.