-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
51 lines (41 loc) · 1.55 KB
/
ViewController.swift
File metadata and controls
51 lines (41 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// ViewController.swift
// MapKitSwiftTutorial
//
// Created by 小峰央志 on 2014/06/25.
// Copyright (c) 2014年 hssh. All rights reserved.
//
import UIKit
import MapKit
class ViewController: UIViewController, UISearchBarDelegate, MKMapViewDelegate {
@IBOutlet var mapSearchBar: UISearchBar
@IBOutlet var mapView: MKMapView
override func viewDidLoad() {
super.viewDidLoad()
mapSearchBar.delegate = self
mapView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(
mapSearchBar.text,
completionHandler: {placemarks, error in
let placemark: CLPlacemark = placemarks[0] as CLPlacemark
let newLocation = placemark.location.coordinate
let annotation = MKPointAnnotation()
annotation.setCoordinate(newLocation)
self.mapView.addAnnotation(annotation)
var mr = self.mapView.visibleMapRect
let pt = MKMapPointForCoordinate(annotation.coordinate)
mr.origin.x = pt.x - mr.size.width * 0.5;
mr.origin.y = pt.y - mr.size.height * 0.25;
self.mapView.setVisibleMapRect(mr, animated: true)
}
)
}
}