Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save blazovics/aaa41932bf3b4d8e9bbc to your computer and use it in GitHub Desktop.
Save blazovics/aaa41932bf3b4d8e9bbc to your computer and use it in GitHub Desktop.
Messenger
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let coordinate = view.annotation!.coordinate
let geocoder = CLGeocoder()
let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
geocoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
if (error != nil) {
print("Error occured \(error!.localizedDescription)")
return
}
if placemarks != nil && placemarks!.count > 0 {
let clPlacemark = placemarks!.first!
let placemark = MKPlacemark(placemark: clPlacemark)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = view.annotation!.title!
var mapItems = [MKMapItem]()
mapItems.append(mapItem)
mapItems.append(MKMapItem.mapItemForCurrentLocation())
let launchOptions: [String:AnyObject] = [MKLaunchOptionsMapTypeKey : MKMapType.Hybrid.rawValue, MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
MKMapItem.openMapsWithItems(mapItems, launchOptions: launchOptions)
}
})
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
let locationAge = -newLocation.timestamp.timeIntervalSinceNow
if locationAge > 5.0{
return
}
if newLocation.horizontalAccuracy < 0{
return
}
if lastLocation == nil || lastLocation!.horizontalAccuracy > newLocation.horizontalAccuracy {
lastLocation = newLocation
if newLocation.horizontalAccuracy <= manager.desiredAccuracy{
stopLocationManager()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
for message in messages{
let toUser = message["to_user"] as! String
let fromUser = message["from_user"] as! String
let title = "\(toUser) \(fromUser)"
let coordinate = CLLocationCoordinate2DMake(message["latitude"] as! Double, message["longitude"] as! Double)
let annotation = MessageAnnotation(coordinate: coordinate, title:title, subtitle:message["topic"] as! String)
self.mapView.addAnnotation(annotation)
}
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if let _ = annotation as? MessageAnnotation
{
let reusableId = "MessengerAnnotationID"
var pinAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reusableId) as? MKPinAnnotationView
if pinAnnotationView == nil{
pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reusableId)
pinAnnotationView!.pinColor = MKPinAnnotationColor.Green
pinAnnotationView!.canShowCallout = true
let callOutButton = UIButton(type:.DetailDisclosure)
pinAnnotationView!.rightCalloutAccessoryView = callOutButton
}
else{
pinAnnotationView!.annotation = annotation
}
return pinAnnotationView
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment