Skip to content

Instantly share code, notes, and snippets.

@tkc
Created August 1, 2016 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkc/4e790314a7d9e248c83eefa29edf6e13 to your computer and use it in GitHub Desktop.
Save tkc/4e790314a7d9e248c83eefa29edf6e13 to your computer and use it in GitHub Desktop.
import XCPlayground
import MapKit
class ViewController: UIViewController {
private var lat:CLLocationDegrees = 0
private var long:CLLocationDegrees = 0
private var latitudeDelta:CLLocationDegrees = 0
private var longitudeDelta:CLLocationDegrees = 0
private var pins:[PinModel] = []
override func viewDidLoad() {
super.viewDidLoad()
let myMapView: MKMapView = MKMapView()
myMapView.frame = self.view.frame
myMapView.mapType=MKMapType.Standard
let myLatitude: CLLocationDegrees = lat
let myLongitude: CLLocationDegrees = long
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(myLatitude, myLongitude)
myMapView.setCenterCoordinate(center, animated: true)
let mySpan: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta)
let myRegion: MKCoordinateRegion = MKCoordinateRegionMake(center, mySpan)
myMapView.region = myRegion
self.view.addSubview(myMapView)
self.makePins();
self.makePinsView(myMapView);
}
func makePinsView(map:MKMapView) {
for pin in pins{
let data = self.getPin(pin.latPoint,long: pin.longPint,text: pin.timeText);
map.addAnnotation(data)
}
}
func makePins() {
let model1=PinModel();
model1.latPoint=35.681298
model1.longPint=139.76674;
model1.timeText="text1";
pins.append(model1);
let model2=PinModel();
model2.latPoint=35.681298
model2.longPint=139.76684;
model2.timeText="text2";
pins.append(model2);
let model3=PinModel();
model3.latPoint=35.681298
model3.longPint=139.76694;
model3.timeText="text3";
pins.append(model3);
}
func getPin(lat: CLLocationDegrees,long: CLLocationDegrees,text:String) -> MKPointAnnotation {
let center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(lat, long)
let myPin: MKPointAnnotation = MKPointAnnotation()
myPin.coordinate = center
myPin.title = text
return myPin
}
}
class PinModel {
internal var latPoint: CLLocationDegrees = 0
internal var longPint: CLLocationDegrees = 0
internal var timeText: String=""
}
let ctrl = ViewController()
ctrl.lat=35.681298;
ctrl.long=139.76624;
ctrl.latitudeDelta=0.005;
ctrl.longitudeDelta=0.005;
XCPlaygroundPage.currentPage.liveView = ctrl.view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment