Swift Local Search MapView - ios

I'm currently integrating local search into my iOS app, and I'm running into some problems. Everything is building fine with no errors, but the search function is just not happening. I've ran it on the simulator and on my iOS Device, and the function doesn't seem to be running. I've checked my console log and nothing is going through.
italic*Edit: I've fixed the problem with the method not being called, but I'm now getting the following error. I can paste the whole call stack if needed
"searchText:]: unrecognized selector sent to instance 0x7ff2f1c1c3b0
2015-02-16 21:10:26.818 THINKStatus[60477:1795955] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[THINKStatus.loggedinViewController searchText:]: unrecognized selector sent to instance 0x7ff2f1c1c3b0'"
Here is the code for my view controller:
import Foundation
import UIKit
import MapKit
class loggedinViewController : UIViewController, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var searchText: UITextField!
var matchingItems: [MKMapItem] = [MKMapItem]()
let service = "tgsLogin"
let userAccount = "tgsLoginUser"
let key = "RandomKey"
#IBAction func loggedinActionButton(sender: AnyObject) {
let error = Locksmith.deleteDataForUserAccount("tgsLoginUser", inService: "tgsLogin")
self.performSegueWithIdentifier("logoutViewSegue", sender: self)
}
#IBAction func textFieldReturn(sender: AnyObject) {
sender.resignFirstResponder()
mapView.removeAnnotations(mapView.annotations)
self.performSearch()
}
func performSearch() {
matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler({(response:
MKLocalSearchResponse!,
error: NSError!) in
if error != nil {
println("Error occured in search: \(error.localizedDescription)")
} else if response.mapItems.count == 0 {
println("No matches found")
} else {
println("Matches found")
for item in response.mapItems as [MKMapItem] {
println("Name = \(item.name)")
println("Phone = \(item.phoneNumber)")
self.matchingItems.append(item as MKMapItem)
println("Matching items = \(self.matchingItems.count)")
var annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
self.mapView.addAnnotation(annotation)
}
}
})
}
override func viewDidLoad() {
super.viewDidLoad()
mapView.showsUserLocation = true
mapView.delegate = self
}
}
app delegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var locationManager: CLLocationManager?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
locationManager = CLLocationManager()
locationManager?.requestWhenInUseAuthorization()
return true
}

The error message suggests that you may be somewhere using searchText as if it was a function. I don't see that in your code, so there are several possibilities:
You have some other code that uses it that you did not post
The binary is not in sync with the source code above
There is a bug in the framework
I would focus on the first two. Try reviewing all error messages to see if there is any indication of line in the code that fails. Try cleaning the project and rebuilding. Try searching for "searchText" in all your source files...

This is the code that worked for me. The only differences I see is some question marks instead of exclamation points. Let me know if this works for you.
#IBAction func textFieldReturn(sender: AnyObject) {
sender.resignFirstResponder()
mapView.removeAnnotations(mapView.annotations)
self.performSearch()
}
func performSearch() {
matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler({
(response: MKLocalSearchResponse?,error: NSError?) in
if error != nil {
print("Error occured in search: \(error!.localizedDescription)")
} else if response!.mapItems.count == 0 {
print("No matches found")
} else {
print("Matches found")
for item in response!.mapItems {
print("Name = \(item.name)")
print("Phone = \(item.placemark)")
self.matchingItems.append(item as MKMapItem)
print("Matching items = \(self.matchingItems.count)")
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
annotation.subtitle = address
self.mapAnnotations.append(annotation)
self.mapView.addAnnotation(annotation)
}
}
})
}

Related

Data from GeoJSON URL end point not appearing on Map using MKAnnotations

I am creating my first IOS app and am not a developer and am really stuck with Map Annotations.
I am trying to get Fire data from a GeoJSON URL end point and display the fires as Annotations on a Map using URLSession and a custom MKAnnotationView and custom fire Pins.
The problem is the Annotations with the GeoJSON Fire data from the URL end point are not appearing on the Map, although data is being returned by the URL session. However, if I manually create a single Fire annotation it is appearing correctly on the map with the custom pin.
Any help would be immensely appreciated, I have spent days trying to figure this out :(
Here is the ViewController.Swift file
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
var locationManager:CLLocationManager!
var lat = Double()
var lon = Double()
var fires: [Fire] = []
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.register(FireMarkerView.self,forAnnotationViewWithReuseIdentifier:
MKMapViewDefaultAnnotationViewReuseIdentifier)
if let url = URL(string: "https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/Active_Fires/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson") {
URLSession.shared.dataTask(with: url) {data, response, error in
if let data = data {
do {
let features = try MKGeoJSONDecoder().decode(data)
.compactMap { $0 as? MKGeoJSONFeature }
let validWorks = features.compactMap(Fire.init)
self.fires.append(contentsOf: validWorks)
print([self.fires])
}
catch let error {
print(error)
}
}
}.resume()
}
//This code works an annotation appears correctly on map
/* let fire = Fire(
title: "Ford Fire",
incidentShortDescription: "Hwy 35",
incidentTypeCategory: "WF",
coordinate: CLLocationCoordinate2D(latitude: 37.7993, longitude: -122.1947))
mapView.addAnnotation(fire)*/
mapView.addAnnotations(fires)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
determineMyCurrentLocation()
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedAlways , .authorizedWhenInUse:
mapView.showsUserLocation = true
followUserLocation()
locationManager.startUpdatingLocation()
break
case .notDetermined , .denied , .restricted:
locationManager.requestWhenInUseAuthorization()
break
default:
break
}
switch manager.accuracyAuthorization {
case .fullAccuracy:
break
case .reducedAccuracy:
break
default:
break
}
}
func followUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: 4000, longitudinalMeters: 4000)
mapView.setRegion(region, animated: true)
}
}
func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
let userLocation = locations.first! as CLLocation
lat = userLocation.coordinate.latitude
lon = userLocation.coordinate.longitude
let region = MKCoordinateRegion.init(center: location.coordinate, latitudinalMeters: 400000, longitudinalMeters: 400000)
self.mapView.setRegion(region, animated: true)
// Call stopUpdatingLocation() to stop listening for location updates,
// other wise this function will be called every time when user location changes.
// Need a solution for this.
manager.stopUpdatingLocation()
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}
Here is the Model Class, Fire.swift
import Foundation
import MapKit
class Fire: NSObject, MKAnnotation {
let title: String?
let incidentShortDescription: String?
let incidentTypeCategory: String?
let coordinate: CLLocationCoordinate2D
init(
title: String?,
incidentShortDescription: String?,
incidentTypeCategory: String?,
coordinate: CLLocationCoordinate2D
) {
self.title = title
self.incidentShortDescription = incidentShortDescription
self.incidentTypeCategory = incidentTypeCategory
self.coordinate = coordinate
super.init()
}
init?(feature: MKGeoJSONFeature) {
// 1
guard
let point = feature.geometry.first as? MKPointAnnotation,
let propertiesData = feature.properties,
let json = try? JSONSerialization.jsonObject(with: propertiesData),
let properties = json as? [String: Any]
else {
return nil
}
// 3
title = properties ["IncidentName"] as? String
incidentShortDescription = properties["IncidentShortDescription"] as? String
incidentTypeCategory = properties["IncidentTypeCategory"] as? String
coordinate = point.coordinate
super.init()
}
var subtitle: String? {
return (incidentTypeCategory)
}
var image: UIImage {
guard let name = incidentTypeCategory else {
return #imageLiteral(resourceName: "RedFlame")
}
switch name {
case "RX":
return #imageLiteral(resourceName: "YellowFlame")
default:
return #imageLiteral(resourceName: "RedFlame")
}
}
Here is the custom MKAnnotation Class: FileMarkerView.swift
import Foundation
import MapKit
class FireMarkerView: MKAnnotationView {
override var annotation: MKAnnotation? {
willSet {
guard let fire = newValue as? Fire else {
return
}
canShowCallout = true
calloutOffset = CGPoint(x: -5, y: 5)
rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
image = fire.image
}
}
}
URLSession.shared.dataTask is an asynchronous task, meaning it calls its callback function at some indeterminate time in the future. Code executed outside of its callback (the { }) will end up getting called before the data task has actually completed. Right now, you're setting the annotations outside of that callback.
To solve this, you need to set the annotations inside of that callback function. So, where you have print([self.fires]), you can do:
DispatchQueue.main.async {
self.mapView.addAnnotations(self.fires)
}
The DispatchQueue.main.async is to make sure that an update to the UI gets called on the main thread (the URL task may return on a different thread).

I get this error when i run my app. Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Whenever I run my app, it always crashes anywhere it states currentUserID. Not sure why.
I printed the the currentUserID value and its nil.
How can I correct it so it runs my app well. I can share the rest of the code if needed.
import UIKit
import MapKit
import CoreLocation
import Firebase
//import FirebaseDatabase
enum AnnotationType {
case pickup
case destination
case driver
}
enum ButtonAction {
case requestRide
case getDirectionsToPassenger
case getDirectionToDestination
case startTrip
case endTrip
}
class HomeVC: UIViewController, Alertable {
#IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var centerMapBtn: UIButton!
#IBOutlet weak var destinationTextField: UITextField!
#IBOutlet weak var destinationCircle: UIView!
#IBOutlet weak var cancelBtn: UIButton!
#IBOutlet weak var actionBtn: UIButton!
var delegate: CenterVCDelegate?
var manager: CLLocationManager?
let currentUserId = Auth.auth().currentUser?.uid
// "IRD70YtgEyWRpaH8LkkjHZmjXPo1"
var ref: DatabaseReference!
var regionRadius: CLLocationDistance = 1000
var tableView = UITableView()
var matchingItems: [MKMapItem] = [MKMapItem]()
var route: MKRoute!
var selectedItemPlacemark: MKPlacemark? = nil
var actionForButton: ButtonAction = .requestRide
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
// print(ref.child("users").value(forKey: "users"))
self.ref.child("userse").childByAutoId().setValue("wise")
self.ref.child("users").child("sdsd").setValue("data")
// print(Auth.auth().currentUser?.uid)
// print(currentUserId)
manager = CLLocationManager()
manager?.delegate = self
manager?.desiredAccuracy = kCLLocationAccuracyBest
checkLocationAuthStatus()
mapView.delegate = self
destinationTextField.delegate = self
centerMapOnUserLocation()
DataService.instance.REF_DRIVERS.observe(.value, with: { (snapshot) in
self.loadDriverAnnotationFromFB()
DataService.instance.passengerIsOnTrip(passengerKey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
self.zoom(toFitAnnotationsFromMapView: self.mapView, forActiveTripWithDriver: true, withKey: driverKey)
}
})
})
cancelBtn.alpha = 0.0
UpdateService.instance.observeTrips { (tripDict) in
if let tripDict = tripDict {
let pickupCoordinateArray = tripDict["pickupCoordinate"] as! NSArray
let tripKey = tripDict["passengerKey"] as! String
let acceptanceStatus = tripDict["tripIsAccepted"] as! Bool
if acceptanceStatus == false {
//Broadcast to all driver
DataService.instance.driverIsAvailable(key: self.currentUserId!, handler: { (available) in
//check for errors
if let available = available {
if available == true {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let pickupVC = storyboard.instantiateViewController(withIdentifier: "PickupVC") as? PickupVC
pickupVC?.initData(coordinate: CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees), passengerKey: tripKey)
self.present(pickupVC!, animated: true, completion: nil)
}
}
})
}
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if self.currentUserId == nil {
print("login")
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (status) in
if status == true {
self.buttonsForDriver(areHidden: true)
}
})
DataService.instance.REF_TRIPS.observe(.childRemoved, with: { (removedTripSnapshot) in
let removedTripDict = removedTripSnapshot.value as? [String: AnyObject]
if removedTripDict?["driverKey"] != nil {
DataService.instance.REF_DRIVERS.child(removedTripDict?["driverKey"] as! String).updateChildValues(["driverIsOnTrip": false])
}
DataService.instance.userIsDriver(userKey: self.currentUserId!, handler: { (isDriver) in
if isDriver == true {
// Remove Overlays and annotation and hide request ride and cancel button
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
} else {
self.cancelBtn.fadeTo(alphaValue: 0.0, withDuration: 0.2)
self.destinationTextField.isUserInteractionEnabled = true
self.destinationTextField.text = ""
// Remove all map annotation and overlays
self.removeOverlaysAndAnnotations(forDrivers: false, forPassengers: true)
self.centerMapOnUserLocation()
}
})
})
DataService.instance.driverIsOnTrip(driverkey: self.currentUserId!, handler: { (isOnTrip, driverKey, tripKey) in
if isOnTrip == true {
DataService.instance.REF_TRIPS.observeSingleEvent(of: .value, with: { (tripSnapshot) in
if let tripSnapshot = tripSnapshot.children.allObjects as? [DataSnapshot] {
for trip in tripSnapshot {
if trip.childSnapshot(forPath: "driverKey").value as? String == self.currentUserId! {
let pickupCoordinateArray = trip.childSnapshot(forPath: "pickupCoordinate").value as! NSArray
let pickupCoordinate = CLLocationCoordinate2D(latitude: pickupCoordinateArray[0] as! CLLocationDegrees, longitude: pickupCoordinateArray[1] as! CLLocationDegrees)
let pickupPlacemark = MKPlacemark(coordinate: pickupCoordinate)
self.dropPinFor(placemark: pickupPlacemark)
self.searchMapKitForResultsWithPolyline(forOriginMapItem: nil, withDestinationMapItem: MKMapItem(placemark: pickupPlacemark))
self.setCustomRegion(forAnnotationType: .pickup, withCoordinate: pickupCoordinate)
self.actionForButton = .getDirectionsToPassenger
self.actionBtn.setTitle("GET DIRECTION", for: .normal)
// Fade in the action button
self.buttonsForDriver(areHidden: false)
}
}
}
})
There are a few issues that should be addressed and then a core issue.
The first thing is that Auth.auth().currentUser? is an optional - meaning that it could have a value or could be nil. Doing this..
let currentUserId = Auth.auth().currentUser?.uid
is telling your code that no matter if its got a value or nil, attempt to assign the .uid to currentUserId.
Protect your code by safely unwrapping options
guard let currentUser = Auth.auth().currentUser else {return}
//now you can access currentUser as it won't be nil
or
if let currentUser = Auth.auth().currentUser {
print("user is authenticated and currentUser is not nil")
} else {
print("not authenticated")
}
Now the core of the issue is, as far as I can see, you're not authenticating the user at all and that needs to happen prior to accessing Auth.auth().currentUser
Here's a complete authentication function with error handling
func authUser(user: String, pw: String) {
Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
if let x = error {
let err = x as NSError
switch err.code {
case AuthErrorCode.wrongPassword.rawValue:
print("wrong password")
case AuthErrorCode.invalidEmail.rawValue:
print("invalued email")
default:
print("unknown error")
}
} else {
if let user = auth?.user { //note; safely unwrap optional
print("uid: \(user.uid)")
}
}
})
}

How to create multiple MapKit annotations by using data from a server?

I have been having a real tough time making multiple annotations on a MapView. So far I have been able to create the relevant classes to download, parse and store the data into an array that can be used. Yet I am still struggling to use the said data and make the annotations required.
HomeModel Class - download and parse the required information from the server
import UIKit
import Foundation
protocol HomeModelProtocol: class {
func itemsDownloaded(items: NSArray)
}
class HomeModel: NSObject, URLSessionDataDelegate {
weak var delegate: HomeModelProtocol!
var data = Data()
let urlPath: String = "https://FAKEDATABASEURL.XYZ"
func downloadItems() {
let url: URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
let task = defaultSession.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Failed to download data")
} else {
print("Data downloaded")
self.parseJSON(data!)
}
}
task.resume()
}
func parseJSON(_ data:Data) {
var jsonResult = NSArray()
do {
jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray
} catch let error as NSError {
print(error)
}
var jsonElement = NSDictionary()
let locations = NSMutableArray()
for i in 0 ..< jsonResult.count {
jsonElement = jsonResult[i] as! NSDictionary
let location = LocationModel()
if let name = jsonElement["Name"] as? String,
let address = jsonElement["Address"] as? String,
let latitude = jsonElement["Latitude"] as? String,
let longitude = jsonElement["Longitude"] as? String {
location.name = name
location.address = address
location.latitude = latitude
location.longitude = longitude
}
locations.add(location)
}
DispatchQueue.main.async(execute: { ()-> Void in
self.delegate.itemsDownloaded(items: locations)
})
}
}
LocalModel Class - To store the data into an array to be used by the app
import UIKit
import Foundation
class LocationModel: NSObject {
// Properties
var name: String?
var address: String?
var latitude: String?
var longitude: String?
// Empty constructor
override init() { }
// Construct with #name, #address, #latitude and #longitude.
init(name: String, address: String, latitude: String, longitude: String) {
self.name = name
self.address = address
self.latitude = latitude
self.longitude = longitude
}
// Print the object's current state
override var description: String {
return "Name: \(String(describing: name)), Address:\(String(describing: address)), Latitude: \(String(describing: latitude)), Longitude: \(String(describing: longitude))"
}
}
Map View Controller - Controls the map for the application
import UIKit
import MapKit
import CoreLocation
class HotPlacesViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
var isFirstTime = true
var locationManager = CLLocationManager()
let newPin = MKPointAnnotation()
var selectedLocation:LocationModel?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Setup the location services delegate in this class.
locationManager.delegate = self
// This little method requests the users permission for location services whilst in this view controller.
if CLLocationManager.authorizationStatus() == .notDetermined {
self.locationManager.requestAlwaysAuthorization()
let alert = UIAlertController(title: "You can change this option in the Settings App", message: "So keep calm your selection is not permanent. 🙂",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
// Drops the pin on the users current location.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
mapView.removeAnnotation(newPin)
let location = locations.last! as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
if(self.isFirstTime) {
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
// Set the region on the map.
mapView.setRegion(region, animated: true)
self.isFirstTime = false
}
newPin.coordinate = location.coordinate
mapView.addAnnotation(newPin)
}
}
Make your let locations = NSMutableArray() array accessible by other classes so that you can use this array in your HotPlacesViewController class.
Then in your HotPlacesViewController class declare a property for holding locations data. And load the data inside viewDidLoad() like:
class HotPlacesViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
// declare a property which will hold the locations data
override func viewDidLoad() {
...
...
locations = // load your data here
}
}
Then, for multiple annotations follow this logic:
for location in locations {
let annotation = MKPointAnnotation()
annotation.title = location.name
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
mapView.addAnnotation(annotation)
}

Attempting to run a function when Google Maps Marker is tapped

I am attempting to run a function that will play a video when a marker on google maps is tapped however I cannot get that function to run although there are no errors. Perhaps there is an issue with my logic or control flow. Below is my view controller as well as the function.
#IBOutlet weak var viewMap: GMSMapView!
var placesClient: GMSPlacesClient?
var url : NSURL?
var videoData : NSData?
var doUpload : Bool?
var FriendsOrPublic : String?
var dataPath : String?
var gmsPlace : GMSPlace?
var gpsCoordinates : CLLocationCoordinate2D?
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
//placesClient = GMSPlacesClient()
var gmsPlace : GMSPlace?
let placesClient = GMSPlacesClient.sharedClient()
placesClient.currentPlaceWithCallback { (placeLikelihoods, error) -> Void in
guard error == nil else {
print("Current Place error: \(error!.localizedDescription)")
return
}
if let placeLikelihoods = placeLikelihoods {
for likelihood in placeLikelihoods.likelihoods {
gmsPlace = likelihood.place
//print("Current Place name \(gmsPlace.name) at likelihood \(likelihood.likelihood)")
//print("Current Place address \(gmsPlace.formattedAddress)")
//print("Current Place attributions \(gmsPlace.attributions)")
//print("Current PlaceID \(gmsPlace.placeID)")
self.gpsCoordinates = (gmsPlace!.coordinate)
}
//print(self.gpsCoordinates)
}
}
let testObject = PFObject(className: "TestObject")
testObject["foo"] = "bar"
testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
print("Object has been saved.")
}
print(url)
print(videoData)
print(doUpload)
print(FriendsOrPublic)
print(dataPath)
if doUpload == true {
Upload()
}
Download()
viewMap.delegate = self
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
//print("locations = \(locValue.latitude) \(locValue.longitude)")
gpsCoordinates = locValue
let camera = GMSCameraPosition.cameraWithTarget(gpsCoordinates!, zoom: 16.9)
//let camera = GMSCamera
print(camera)
viewMap.camera = camera
viewMap.myLocationEnabled = true
viewMap.settings.myLocationButton = false
let marker = GMSMarker()
marker.position = self.gpsCoordinates!
marker.title = "Newport Beach"
marker.snippet = "California"
marker.map = viewMap
locationManager.stopUpdatingLocation()
}
var marker : GMSMarker!
func Download() {
let query = PFQuery(className:"UploadedVideoCurrent")
// query.whereKey("GPS", equalTo: "ChIJTbSOh8Pf3IARt311y2Wqspc")
query.whereKey("typeofPost", equalTo: "Public")
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) coordinates.")
// Do something with the found objects
if let objects = objects {
for object in objects {
print(object.objectForKey("GPS"))
let postsLat = object.objectForKey("GPS")!.latitude as Double
let postsLong = object.objectForKey("GPS")!.longitude as Double
let position: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: postsLat, longitude:postsLong)
self.marker = GMSMarker(position: position)
self.marker.map = self.viewMap
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
}
func viewMap(viewMap: GMSMapView, didTapMarker marker: GMSMarker)
{
print("tapped")
}
You're calling the wrong delegate function. you should still call the function "mapView" because that's how the delegate function is defined, even though your map itself is declared as "viewMap"
func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {
print("tapped")
}

iOS Simulator and reverseGeocodeLocation

Is it normal for the iOS simulator to not work every so often with regards to grabbing the location? My project for this app keeps crashing 50% of the time whenever I run the simulator...but I can't seem to pinpoint the problem within the code itself. If the problem is indeed in the code itself, can anyone please help me find the problem? The error is "fatal error: unexpectedly found nil while unwrapping an Optional value" and it says it occurs in my refreshPost function on the line
eventsPostedQuery.whereKey("CityName", equalTo: self.usersLocation)
I am using Parse as part of this app. Also, I have a viewDidAppear to refresh the "Posts", is this a correct way to go about this? Greatly appreciated!
import UIKit
import Parse
class HomeTableViewController: UITableViewController, CLLocationManagerDelegate {
#IBOutlet weak var navigationBar: UINavigationItem!
#IBOutlet weak var menuButton: UIBarButtonItem!
#IBAction func cancelPost(segue: UIStoryboardSegue) {
}
var users = [String: String]()
var usernames = [String]()
var eventInfo = [String]()
var imageFiles = [PFFile]()
var usersLocation: String!
var locationManager: CLLocationManager!
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in
if error != nil {
print(error)
} else {
let p = placemarks?.first // ".first" returns the first element in the collection, or nil if its empty
// this code above will equal the first element in the placemarks array
let city = p?.locality != nil ? p?.locality : ""
let state = p?.administrativeArea != nil ? p?.administrativeArea : ""
self.navigationBar.title = ("\(city!), \(state!)")
self.usersLocation = ("\(city!), \(state!)")
self.locationManager.stopUpdatingLocation()
print(self.usersLocation)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
menuButton.target = self.revealViewController()
menuButton.action = Selector("revealToggle:")
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 250.0
}
override func viewDidAppear(animated: Bool) {
refreshPosts()
}
func refreshPosts() {
let query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let users = objects {
self.users.removeAll(keepCapacity: true)
self.usernames.removeAll(keepCapacity: true)
self.eventInfo.removeAll(keepCapacity: true)
self.imageFiles.removeAll(keepCapacity: true)
for object in users {
if let user = object as? PFUser {
self.users[user.objectId!] = user.username!
}
}
}
let eventsPostedQuery = PFQuery(className: "PostEvent")
eventsPostedQuery.whereKey("CityName", equalTo: self.usersLocation)
eventsPostedQuery.orderByDescending("createdAt")
eventsPostedQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let events = objects {
for event in events {
self.imageFiles.append(event["imageFile"] as! PFFile)
self.eventInfo.append(event["eventInfo"] as! String)
self.usernames.append(self.users[event["userId"] as! String]!)
self.tableView.reloadData()
}
}
})
})
}
You should call
refreshPosts()
from inside the else block of this completion block:
CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in
if error != nil {
print(error)
} else {
let p = placemarks?.first // ".first" returns the first element in the collection, or nil if its empty
// this code above will equal the first element in the placemarks array
let city = p?.locality != nil ? p?.locality : ""
let state = p?.administrativeArea != nil ? p?.administrativeArea : ""
self.navigationBar.title = ("\(city!), \(state!)")
self.usersLocation = ("\(city!), \(state!)")
self.locationManager.stopUpdatingLocation()
print(self.usersLocation)
}
}
As in only update the post if the reverse geocoder is finished and didn't return an error.

Resources