import UIKit import AVFoundation import CoreLocation import MapKit class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, CLLocationManagerDelegate { var imagePicker:UIImagePickerController! var myLocationManager:CLLocationManager! var myLatitudeLabel:UILabel! var myLongitudeLabel:UILabel! var currenttlabel:UILabel! var myMapView: MKMapView! var lat:Double = 0.0 var lng:Double = 0.0 var t:Double = 0.0 override func viewDidLoad() { super.viewDidLoad() // MapViewを生成 myMapView = MKMapView() myMapView.frame = CGRectMake(0, 250, 380, 420) let myLatitude: CLLocationDegrees = 37.331741 let myLongitude: CLLocationDegrees = -122.030333 let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(myLatitude, myLongitude) // myMapView.setCenterCoordinate(center, animated: true) let mySpan: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1) let myRegion: MKCoordinateRegion = MKCoordinateRegionMake(center, mySpan) myMapView.region = myRegion self.view.addSubview(myMapView) /* // pin test let latlng : [[Double]] = [ [ 35.0997456629156, 139.076407179366, 1411637575680 ], [ 35.0994438305824, 139.076087577398, 1411637575680 ], [ 35.099366214159, 139.076417656745, 1411637706752 ], [ 35.0993660291837, 139.076421125554, 1411637706752 ], [ 35.0995956687584, 139.076252868529, 1411637706752 ], [ 35.0996091542296, 139.076103408783, 1411637837824 ], [ 35.0996138574882, 139.076094450559, 1411637706752 ], [ 35.0998947888951, 139.075874729463, 1411637968896 ], [ 35.0989795569657, 139.075842742006, 1411638362112 ], [ 35.098147736895, 139.075409649069, 1411638493184 ], [ 35.0978490435962, 139.074287737581, 1411638624256 ], [ 35.0980158476486, 139.074019678067, 1411638624256 ], [ 35.0978487544089, 139.073832342531, 1411638624256 ], [ 35.0963003544846, 139.070642650128, 1411640328192 ], [ 35.0986367545496, 139.076270123367, 1411640983552 ], ] for (var i = 0; i < latlng.count; i++) { let ll: [Double] = latlng[i] let lat: CLLocationDegrees = ll[0] let lng: CLLocationDegrees = ll[1] var myPin: MKPointAnnotation = MKPointAnnotation() let pos: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, lng) myPin.coordinate = pos // myPin.title = ll[2] // myPin.subtitle = "サブタイトル" myMapView.addAnnotation(myPin) if (i == 0) { myMapView.setCenterCoordinate(pos, animated: false) } } */ // button let myButton: UIButton = UIButton() myButton.frame = CGRectMake(0, 0, 200, 40) myButton.backgroundColor = UIColor.redColor(); myButton.layer.masksToBounds = true myButton.setTitle("写真", forState: UIControlState.Normal) myButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) myButton.layer.cornerRadius = 5.0 myButton.layer.position = CGPoint(x: self.view.frame.width/2, y:50) myButton.tag = 1 myButton.addTarget(self, action: "onClickButton:", forControlEvents: .TouchUpInside) self.view.addSubview(myButton); let myButton2:UIButton = UIButton() myButton2.frame = CGRectMake(0, 0, 200, 40) myButton2.backgroundColor = UIColor.redColor(); myButton2.layer.masksToBounds = true myButton2.setTitle("GPS", forState: UIControlState.Normal) myButton2.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) myButton2.layer.cornerRadius = 5.0 myButton2.layer.position = CGPoint(x: self.view.frame.width/2, y:100) myButton2.tag = 1 myButton2.addTarget(self, action: "onClickButton2:", forControlEvents: .TouchUpInside) self.view.addSubview(myButton2); // lat lng myLatitudeLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 30)) myLatitudeLabel.layer.position = CGPoint(x: self.view.bounds.width/2, y:150) myLatitudeLabel.textAlignment = NSTextAlignment.Center self.view.addSubview(myLatitudeLabel) myLongitudeLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 30)) myLongitudeLabel.layer.position = CGPoint(x: self.view.bounds.width/2, y:180) myLongitudeLabel.textAlignment = NSTextAlignment.Center self.view.addSubview(myLongitudeLabel) currenttlabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 30)) currenttlabel.layer.position = CGPoint(x: self.view.bounds.width/2, y:210) currenttlabel.textAlignment = NSTextAlignment.Center self.view.addSubview(currenttlabel) //現在地の取得 myLocationManager = CLLocationManager() myLocationManager.delegate = self let status = CLLocationManager.authorizationStatus() if(status == CLAuthorizationStatus.NotDetermined) { println("didChangeAuthorizationStatus:\(status)"); self.myLocationManager.requestAlwaysAuthorization() } } func onClickButton2(sender: UIButton) { myLocationManager.desiredAccuracy = kCLLocationAccuracyBest myLocationManager.distanceFilter = 10 myLocationManager.startUpdatingLocation() } func onClickButton(sender: UIButton){ println("onClickMyButton:") println("sender.currentTitile: \(sender.currentTitle)") println("sender.currentTitile: \(sender.tag)") if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) { imagePicker = UIImagePickerController() imagePicker!.delegate = self imagePicker!.sourceType = UIImagePickerControllerSourceType.Camera imagePicker!.allowsEditing = false self.presentViewController(imagePicker, animated: true, completion: nil) } } func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) { println("didChangeAuthorizationStatus"); // 認証のステータスをログで表示 var statusStr = ""; switch (status) { case .NotDetermined: statusStr = "NotDetermined" case .Restricted: statusStr = "Restricted" case .Denied: statusStr = "Denied" case .Authorized: statusStr = "Authorized" case .AuthorizedWhenInUse: statusStr = "AuthorizedWhenInUse" } println(" CLAuthorizationStatus: \(statusStr)") } func locationManager(manager: CLLocationManager!,didUpdateLocations locations: [AnyObject]!){ lat = manager.location.coordinate.latitude lng = manager.location.coordinate.longitude t = manager.location.timestamp.timeIntervalSince1970 myLatitudeLabel.text = "緯度:\(manager.location.coordinate.latitude)" myLongitudeLabel.text = "経度:\(manager.location.coordinate.longitude)" currenttlabel.text = "時間:\(manager.location.timestamp)" // show pin var myPin: MKPointAnnotation = MKPointAnnotation() myPin.coordinate = manager.location.coordinate myMapView.addAnnotation(myPin) myMapView.setCenterCoordinate(myPin.coordinate, animated: true) // http var myUrl:NSURL = NSURL(string:"http://sabae.club/poslog/?lat=\(manager.location.coordinate.latitude)&lng=\(manager.location.coordinate.longitude)&t=\(manager.location.timestamp.timeIntervalSince1970)") var myRequest:NSURLRequest = NSURLRequest(URL: myUrl) NSURLConnection.sendAsynchronousRequest(myRequest, queue: NSOperationQueue.mainQueue(), completionHandler: self.getHttp) println("sent http://sabae.club/poslog/?lat=\(manager.location.coordinate.latitude)&lng=\(manager.location.coordinate.longitude)&t=\(manager.location.timestamp.timeIntervalSince1970)") } func locationManager(manager: CLLocationManager!,didFailWithError error: NSError!){ print("error") } func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){ self.dismissViewControllerAnimated(true, completion:nil) UIImageWriteToSavedPhotosAlbum(image, self, "imageDidFinishSavingWithErrorContextInfo:error:contextInfo:", nil) } func imagePickerControllerDidCancel(picker: UIImagePickerController!){ self.dismissViewControllerAnimated(true, completion:nil) } func imageDidFinishSavingWithErrorContextInfo(image: UIImage!, error: NSErrorPointer, contextInfo: UnsafeMutablePointer<()>){ if error != nil { println("error") return } var imgData:NSData = UIImageJPEGRepresentation(image, 0.8) // post image var myUrl:NSURL = NSURL(string:"http://sabae.club/poslog/?lat=\(lat)&lng=\(lng)&t=\(t)") var myRequest:NSMutableURLRequest = NSMutableURLRequest(URL: myUrl) myRequest.HTTPBody = imgData; myRequest.HTTPMethod = "POST" NSURLConnection.sendAsynchronousRequest(myRequest, queue: NSOperationQueue.mainQueue(), completionHandler: self.getHttp) println( "success" ) } func getHttp(res:NSURLResponse?,data:NSData?,error:NSError?){ var myData:NSString = NSString(data: data!, encoding: NSUTF8StringEncoding) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }