Last active
December 11, 2015 12:48
-
-
Save mmmries/4602585 to your computer and use it in GitHub Desktop.
Whereami sample code for my blog post.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BNRMapPoint | |
attr_reader :coordinate, :title | |
def initialize(coordinate, title) | |
@coordinate, @title = coordinate, title | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BNRMapPoint | |
def initialize(coordinate, title) | |
@coordinate, @title = coordinate, title | |
end | |
def coordinate; @coordinate; end | |
def title; @title; end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def found_location(loc) | |
coord = loc.coordinate | |
point = BNRMapPoint.new(coord, locationTitleField.text) | |
worldView.addAnnotation(point) | |
# ... some cleanup code here | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WhereamiController < UIViewController | |
extend IB | |
attr_reader :location_manager | |
## ib outlets | |
outlet :worldView, MKMapView | |
outlet :activityIndicator, UIActivityIndicatorView | |
outlet :locationTitleField, UITextField | |
def viewDidLoad | |
@location_manager = CLLocationManager.alloc.init | |
location_manager.setDelegate(self) | |
location_manager.setDesiredAccuracy(KCLLocationAccuracyBest) | |
end | |
#MKMapViewDelegate | |
def mapView( map, didUpdateUserLocation: location) | |
@last_location = location | |
coord = location.coordinate | |
region = MKCoordinateRegionMakeWithDistance(coord, 250, 250) | |
worldView.setRegion(region, animated:true) | |
end | |
#UITextFieldDelegate | |
def textFieldShouldReturn(field) | |
field.resignFirstResponder | |
find_location | |
true | |
end | |
def locationManager(man, didUpdateToLocation:to, fromLocation: from) | |
found_location(to) | |
end | |
private | |
def find_location | |
location_manager.startUpdatingLocation | |
activityIndicator.startAnimating | |
locationTitleField.setHidden(true) | |
end | |
def found_location(loc) | |
coord = loc.coordinate | |
point = BNRMapPoint.new(coord, locationTitleField.text) | |
worldView.addAnnotation(point) | |
location_manager.stopUpdatingLocation | |
activityIndicator.stopAnimating | |
locationTitleField.setText("") | |
locationTitleField.setHidden(false) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment