Skip to content

Instantly share code, notes, and snippets.

@loplopLover
Created June 8, 2015 09:20
Show Gist options
  • Save loplopLover/c56d541db2b86e6d9774 to your computer and use it in GitHub Desktop.
Save loplopLover/c56d541db2b86e6d9774 to your computer and use it in GitHub Desktop.
//
// HowToGoViewController.swift
// Test
//
// Created by Haydar Karkin on 5.06.2015.
// Copyright (c) 2015 Haydar Karkin. All rights reserved.
//
import UIKit
import MapKit
class HowToGoViewController: UIViewController, CLLocationManagerDelegate, UITextFieldDelegate ,VJAutocompleteDataSource, VJAutocompleteDelegate {
weak var delegate: LeftMenuProtocol?
var locationManager: CLLocationManager!
@IBOutlet weak var backButton: UIButton!
@IBOutlet weak var mapKit: MKMapView!
@IBOutlet weak var fromTextField: UITextField!
@IBOutlet weak var toTextField: UITextField!
@IBOutlet weak var goMapButton: UIButton!
// -------------------------------------------------------------------------------
// MARK: - Private properties
// -------------------------------------------------------------------------------
var sourceDataArray = [AnyObject]();
var fromAutocomplete: VJAutocomplete!;
var toAutocomplete: VJAutocomplete!;
override func viewDidLoad() {
super.viewDidLoad()
fromTextField.delegate=self
toTextField.delegate=self
fromTextField.tag = 0
toTextField.tag = 1
self.mapKit.mapType = MKMapType.Standard
self.mapKit.showsUserLocation = true
self.mapKit.removeAnnotations(self.mapKit.annotations)
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
let location = self.locationManager.location
/*
var latitude: Double = location.coordinate.latitude as Double
var longitude: Double = location.coordinate.longitude as Double
println("current latitude :: \(latitude)")
println("current longitude :: \(longitude)")
*/
//autocomple
sourceDataArray = Country.listOfCountries();
// Initialize it with initWithTextField is recomended
fromAutocomplete = VJAutocomplete(textField: fromTextField);
toAutocomplete = VJAutocomplete(textField: toTextField);
setupAutocomplete();
//fromTextField.becomeFirstResponder();
//toTextField.becomeFirstResponder();
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as! CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapKit.setRegion(region, animated: true)
// Add an annotation on Map View
var point: MKPointAnnotation! = MKPointAnnotation()
point.coordinate = location.coordinate
point.title = "Current Location"
point.subtitle = "sub title"
self.mapKit.addAnnotation(point)
//stop updating location to save battery life
locationManager.stopUpdatingLocation()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.setNavigationBarItem()
}
@IBAction func didTouchToMain(sender: UIButton) {
delegate?.changeViewController(LeftMenu.Main)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func setCell(cell: UITableViewCell, withItem item: AnyObject) -> UITableViewCell {
cell.textLabel!.text = item as! NSString as String;
cell.textLabel!.font = UIFont.systemFontOfSize(15.0);
return cell;
}
func getItemsArrayWithSubstring(substring: String) -> [AnyObject] {
let beginsWithPredicate = NSPredicate(format: "SELF beginswith[c] %@", substring);
var searchedCountriesArray = (sourceDataArray as NSArray).filteredArrayUsingPredicate(beginsWithPredicate);
return searchedCountriesArray;
}
// -------------------------------------------------------------------------------
// MARK - VJAutocomplete delegate
// -------------------------------------------------------------------------------
func autocompleteWasSelectedRow(rowIndex: Int) {
fromTextField.resignFirstResponder();
toTextField.resignFirstResponder();
}
// -------------------------------------------------------------------------------
// MARK - UITextField delegate
// -------------------------------------------------------------------------------
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
if textField.tag == 0{
fromAutocomplete.shouldChangeCharacters(InRange: range, replacementString: string);
}else{
toAutocomplete.shouldChangeCharacters(InRange: range, replacementString: string);
}
return true;
}
func textFieldDidEndEditing(textField: UITextField) {
if textField.tag == 0{
fromAutocomplete.hideAutocomplete();
}else{
toAutocomplete.hideAutocomplete();
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder();
if textField.tag == 0{
fromAutocomplete.hideAutocomplete();
}else{
toAutocomplete.hideAutocomplete();
}
return true;
}
func textFieldShouldClear(textField: UITextField) -> Bool {
if textField.tag == 0{
fromAutocomplete.hideAutocomplete();
}else{
toAutocomplete.hideAutocomplete();
}
return true;
}
func textFieldDidBeginEditing(textField: UITextField) {
if textField.tag == 0{
fromTextField.becomeFirstResponder();
}else{
toTextField.becomeFirstResponder();
}
}
// -------------------------------------------------------------------------------
// MARK - Private methods
// -------------------------------------------------------------------------------
private func setupAutocomplete() {
// Set the data source as self
fromAutocomplete.autocompleteDataSource = self;
toAutocomplete.autocompleteDataSource = self;
// Set the delegate as self
fromAutocomplete.autocompleteDelegate = self;
toAutocomplete.autocompleteDelegate = self;
// Set minimum count of characters to show autocomplete
fromAutocomplete.minCountOfCharsToShow = 1;
toAutocomplete.minCountOfCharsToShow = 1;
// Set maximum of visible rows
fromAutocomplete.maxVisibleRowsCount = 5;
toAutocomplete.minCountOfCharsToShow = 5;
// Set cell height
fromAutocomplete.cellHeight = 25;
toAutocomplete.cellHeight = 25;
// Set border
fromAutocomplete.setBorder(1.5, cornerRadius: 8.0, color: UIColor.groupTableViewBackgroundColor());
toAutocomplete.setBorder(1.5, cornerRadius: 8.0, color: UIColor.groupTableViewBackgroundColor());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment