Remove Object From Array Using Enumeration - ios

I have an array of dictionaries. Each dictionary contains latitude and longitude so I'm getting the distance of each item from the current user location. If the distance in miles is greater than 20, that particular dictionary should be removed from the array. If the dictionary is not removed from the array, an annotation is created and added to an annotation array which is then used to add annotations to a map once the enumeration is finished. I'm only getting one annotation added when I should be getting three so I know I'm doing something wrong in my enumeration.
func checkDistanceAndAddPins() {
for gym in gyms {
var index = 0
let gymLatitude = gym["latitude"]!!.doubleValue
let gymLongitude = gym["longitude"]!!.doubleValue
let gymLocation = CLLocation(latitude: gymLatitude, longitude: gymLongitude)
let distance = gymLocation.distanceFromLocation(myLocation!)
let distanceInMeters = NSNumber(double: distance)
let metersDouble = distanceInMeters.doubleValue
let miles = metersDouble * 0.00062137
if miles > maxDistance {
gyms.removeAtIndex(index)
} else {
let location = CLLocationCoordinate2D(latitude: gymLatitude, longitude: gymLongitude)
gymAnnotation.title = gym["Name"] as? String
gymAnnotation.subtitle = gym["Address"] as? String
gymAnnotation.coordinate = location
gymAnnotation.gymPhoneNumber = gym["Phone"] as? String
if let website = gym["Website"] as? String {
gymAnnotation.gymWebsite = website
}
gymLocations.append(gymAnnotation)
}
index += 1
}
dispatch_async(dispatch_get_main_queue()) {
self.gymMap.addAnnotations(self.gymLocations)
}
}

Related

How to Make an Array for Annotations

Not all my annotations will show up in my map because I can't make var annotation = MKPoinatAnnotation the same MKPointAnnotation in fetching the CKrecords(annotation), and in getting directions for an annotation. I'm confused on how to make an array for annotations so I can be able to load all my annotations from the CloudKit database and be able to get directions when the annotation is selected.
let annotation = MKPointAnnotation()
let database = CKContainer.default().publicCloudDatabase
var truck: [CKRecord] = []
func fetch() {
let truePredicate = NSPredicate(value: true)
let eventQuery = CKQuery(recordType: "User", predicate: truePredicate)
let queryOperation = CKQueryOperation(query: eventQuery)
queryOperation.recordFetchedBlock = { (record : CKRecord!) in
self.truck.append(record)
self.annotation.title = record["username"] as? String
self.annotation.subtitle = record["hours"] as? String
if let location = record["location"] as? CLLocation {
self.annotation.coordinate = location.coordinate
}
print("recordFetchedBlock: \(record)")
self.mapView.addAnnotation(self.annotation)
}
self.database.add(queryOperation)
}
How I get directions -
#IBAction func getDirections(_ sender: Any) {
let view = annotation.coordinate
print("Annotation: \(String(describing: view ))")
let currentLocMapItem = MKMapItem.forCurrentLocation()
let selectedPlacemark = MKPlacemark(coordinate: view, addressDictionary: nil)
let selectedMapItem = MKMapItem(placemark: selectedPlacemark)
let mapItems = [selectedMapItem, currentLocMapItem]
let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
MKMapItem.openMaps(with: mapItems, launchOptions:launchOptions)
}
I'm assuming the contents of the array change depending on the search query. If that is the case, make an array of type MKPointAnnotation:
var points: [MKPointAnnotation] = []
Then fill the array however you fill it and run it through a loop whereby each iteration adds a point to the map:
for point in points {
mapView.addAnnotation(point)
}
If you have a problem with varying types of annotations, make the array of type CLLocationCoordinate2D and then you can fill the array by accessing MKPointAnnotation.coordinate, for example.
Does this help?

Sort By Distance In TableView

I'm trying to sort the results of my tableView by distance closest to the user. I've separated my distance and function to populate distance to get a better hold of my data manipulation. I feel like it should be simple but, I know I'm missing it.
The distance function is this:
func calculateDistance(userlat: CLLocationDegrees, userLon:CLLocationDegrees, venueLat:CLLocationDegrees, venueLon:CLLocationDegrees) -> Double {
let userLocation:CLLocation = CLLocation(latitude: userlat, longitude: userLon)
let priceLocation:CLLocation = CLLocation(latitude: venueLat, longitude: venueLon)
//let distance = String(format: "%.0f", userLocation.distance(from: priceLocation)/1000)
return userLocation.distance(from: priceLocation)/1000
}
PopulateData function:
func populateData() {
//Pulls TableData for UITableView
DataService.ds.REF_VENUE.observe(.value, with: { (snapshot) in
self.posts = [] // THIS IS THE NEW LINE
if snapshot.exists(){
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshot {
if let snapValue = snap.value as? [String:AnyObject],
let venueLat = snapValue["LATITUDE"] as? Double,
let venueLong = snapValue["LONGITUDE"] as? Double
{
let distance = self.calculateDistance(userlat: self.userLatt, userLon: self.userLonn, venueLat: venueLat, venueLon: venueLong)
if distance <= 2 {
let key = snap.key
let post = Post(postKey: key, postData: snapValue)
self.posts.append(post)
self.posts.sort(by: (posts, distance)) //Where I'm trying to sort
}
}
}
self.getDataForMapAnnotation()
}
self.tableView.reloadData()
}
})
}
I'm not sure if I can even sort an array of dictionaries but, the end goal is to have the tableView show the venues closest to the user. If you have any suggestions, please let me know!
If you added the distance you calculate as a property of your Post class/struct, sorting the posts array by distance would be pretty straightforward. Using the Swift shorthand syntax for closures, your sort function could look something like this:
self.posts.sort {
return $0.distance < $1.distance
}
This would sort the posts array by distance in ascending order.

MapBox swift 3 api

I use MapBox api swift 2.3 and reading geojson. But after swift 3 dont work began to upgrade.
Swift 2.3 examples; its work
for location in locations {
nate2D(latitude: location[1].doubleValue, longitude: location[0].doubleValue)
coordinates.append(coordinate)
}
Swift 3.0 dont work
if let feature = feature as? NSDictionary {
if let geometry = feature["geometry"] as? NSDictionary {
if geometry["type"] as? String == "Polygon" {
var coordinates: [CLLocationCoordinate2D] = []
if let locations = geometry["coordinates"] as? NSArray {
for location in locations {
for i in (0 ..< (location as AnyObject).count)
{
let coordinate = CLLocationCoordinate2D(latitude: ???, longitude: ???)
coordinates.append(coordinate)
}
}
}
let shape = MGLPolygon(coordinates: &coordinates, count: UInt(coordinates.count))
DispatchQueue.main.async(execute: {
[unowned self] in
self.mapView.addAnnotation(shape)
})
}
}
}
enter image description here
It ' s correct answer
for location in locations {
for i in (0 ..< (location as AnyObject).count)
{
let a = locations[0] as? NSArray
let b = a?[i] as? NSArray
var c = b?[0]
print(c);
let coordinate = CLLocationCoordinate2D(latitude: b?[1] as! CLLocationDegrees, longitude: b?[0] as! CLLocationDegrees)
coordinates.append(coordinate)
}
}

Cannot assign value of type '[String]' to type 'String? Swift 2

I am receiving this error message "Cannot assign value of type '[String]' to type 'String?'" associated with this line "point.title = r" and this line "point.subtitle = z". I've tried lots of things like place.description which makes the entire array a string...
//I have multiple arrays, lat, long and pass besides place.
var place = [String]()
//I have four of the chunks of code for each array
let json = try NSJSONSerialization.JSONObjectWithData(jSONData, options: .AllowFragments)
if let dataFile3 = json["data"] as? [[String: AnyObject]] {
for c in dataFile3 {
if let placeName = c["PlaceName"] {
place.append((placeName as? String)!)
for var (i,x) in zip(lat, long) {
for _ in place {
for _ in pass {
print(i)
print(x)
//print(i + ", " + x)
// String to double conversion, can I do this when I set the array as a string?
let lati = NSString(string: i).doubleValue
let longi = NSString(string: x).doubleValue
//I have a list of values array so I need to have a for loop that inputs the value over and over again.
var point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
point.title = place
point.subtitle = pass
mapView.addAnnotation(point)
}}}
//var i and x for loop
}
//viewDidLoad
}
//creates the markers popup for each point with data
func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
// Always try to show a callout when an annotation is tapped.
return true
}
How do I fix the error above?
EDIT:
With Santosh's help this is what I changed...
let point = MGLPointAnnotation()
var i = lat
var x = long
//lat and long is served and then all the r,z points, then another lat, long...need to fix this and it may work.
for (i,x) in zip(lat, long) {
for aPlace in place {
for aPass in pass {
print(i)
print(x)
// String to double conversion, can I do this when I set the array as a string?
let lati = NSString(string: i).doubleValue
let longi = NSString(string: x).doubleValue
point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
point.title = aPlace
print(aPlace)
point.subtitle = aPass
print(aPass)
self.mapView.addAnnotation(point)
}}}
This is running correctly now or at least reporting the correct values but the loop just keeps a loopin...
Try this:
//I have four of the chunks of code for each array
let json = try NSJSONSerialization.JSONObjectWithData(jSONData, options: .AllowFragments)
if let dataFile3 = json["data"] as? [[String: AnyObject]] {
for c in dataFile3 {
if let placeName = c["PlaceName"] {
place.append((placeName as? String)!)
for var (i,x) in zip(lat, long) {
for aPlace in place {
for aPass in pass {
print(i)
print(x)
//print(i + ", " + x)
// String to double conversion, can I do this when I set the array as a string?
let lati = NSString(string: i).doubleValue
let longi = NSString(string: x).doubleValue
//I have a list of values array so I need to have a for loop that inputs the value over and over again.
var point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: lati, longitude: longi)
point.title = aPlace
point.subtitle = aPass
mapView.addAnnotation(point)
}}}
//var i and x for loop
}
//viewDidLoad
}
}

How do I iterate through JSON co-ordinates and build annotations as one function?

I am struggling to get the annotations being placed using JSON data. I have tried iterating the coordinates from the JSON into a new array but when I try pass an array to where I need the coordinates it fails because it cannot take arrays. How can I fix this?
Can anyone help?
Alamofire.request(.GET, "https://demo1991046.mockable.io/score/locations").responseJSON { (responseData) -> Void in
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["users"].arrayObject as? [NSArray] {
self.newArray = (resData as? [NSArray])
}
print("\([self.newArray])")
for var i = 0; i < self.newArray!.count; ++i {
self.longitude.append(self.newArray[i]["lon"] as! String!)
print("longitude: \(self.longitude)")
self.latitude.append(self.newArray[i]["lat"] as! String!)
print("latitude: \(self.latitude)")
}
let doubleLat = self.latitude.map {
Double(($0 as NSString).doubleValue)
}
let doubleLon = self.longitude.map {
Double(($0 as NSString).doubleValue)
}
print("doublelat: \(doubleLat)")
print("doubleLon: \(doubleLon)")
// 1
self.locationManager.delegate = self
// 2
self.locationManager.requestAlwaysAuthorization()
// 3
let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(0.01 , 0.01)
let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: doubleLat, longitude: doubleLon) // <- here is where I get an error: "Cannot convert value of type '[Double]' to expect argument type 'CLLocationDegrees' (aka 'Double")"
// print("lat: \((locationManager.location?.coordinate.latitude)!)")
// print("lon: \((locationManager.location?.coordinate.longitude)!)")
let theRegion:MKCoordinateRegion = MKCoordinateRegionMake(location, theSpan)
self.mapView.setRegion(theRegion, animated: true)
let anotation = MKPointAnnotation()
anotation.coordinate = location
anotation.title = "The Location"
anotation.subtitle = "This is the location !!!"
self.mapView.addAnnotation(anotation)
}
}
I have done soem modifies below to your code
Didn't convert the json to NSArray (by using .array instead of .arrayObject)
moved adding anotation to the map inside the for loop to add all of them.
Moved setting a region to the map out side the for loop and left it to you to set the location you like.
Alamofire.request(.GET, "https://demo1991046.mockable.io/score/locations").responseJSON { (responseData) -> Void in
let swiftyJsonVar = JSON(responseData.result.value!)
// get the users from the json var, no need to convert it to Array
guard let usersJsonArray = swiftyJsonVar["users"].array else {
// users not found in the json
return
}
// the usersJsonArray is array of json which will be much easier for work with.
// No need for 1,2 and 3 to be in the for loop.
// 1
self.locationManager.delegate = self
// 2
self.locationManager.requestAlwaysAuthorization()
// 3
let theSpan:MKCoordinateSpan = MKCoordinateSpanMake(0.01 , 0.01)
for userJson in usersJsonArray {
let longitudeString = userJson["lon"].stringValue
print("longitude: \(longitudeString)")
let latitudeString = userJson["lat"].stringValue
print("latitude: \(latitudeString)")
let doubleLat = Double(latitudeString)
let doubleLon = Double(longitudeString)
print("doublelat: \(doubleLat)")
print("doubleLon: \(doubleLon)")
// by having the next code block inside the for loop you will be able to add all the user locations to the map as anotations.
let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: doubleLat, longitude: doubleLon) // Now should work fine
let anotation = MKPointAnnotation()
anotation.coordinate = location
anotation.title = "The Location"
anotation.subtitle = "This is the location !!!"
self.mapView.addAnnotation(anotation)
} // for usersJson
// you need to figure out the loaction you will set for the mapView region.
let location = .... // set the location you like.
let theRegion:MKCoordinateRegion = MKCoordinateRegionMake(location, theSpan)
self.mapView.setRegion(theRegion, animated: true)
}

Resources