import UIKit
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
lazy var locationM: CLLocationManager = {
let locationM = CLLocationManager()
if #available(iOS 8.0, *) {
locationM.requestAlwaysAuthorization()
}
return locationM
}()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.userTrackingMode = MKUserTrackingMode.followWithHeading
_ = locationM
}
func addAnnotation(_ coordinate: CLLocationCoordinate2D, title: String, subTitle: String) -> TGAnnotation {
let annotation: TGAnnotation = TGAnnotation()
annotation.coordinate = coordinate
annotation.title = title
annotation.subtitle = subTitle
mapView.addAnnotation(annotation)
return annotation
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// camera()
// snap()
poi()
}
func poi(){
let request: MKLocalSearchRequest = MKLocalSearchRequest()
request.naturalLanguageQuery = "小吃"
request.region = mapView.region
let search: MKLocalSearch = MKLocalSearch(request: request)
search.start { (response: MKLocalSearchResponse?, error: Error?) in
if error == nil {
let items = response!.mapItems
for item in items {
self.addAnnotation(item.placemark.coordinate, title: item.name ?? "", subTitle:item.phoneNumber ?? "")
print(item.name ?? "")
}
}
}
}
func snap() {
let option: MKMapSnapshotOptions = MKMapSnapshotOptions()
option.region = mapView.region
option.mapType = .satellite
option.size = CGSize(width: 1000, height: 1000)
let snapShoter = MKMapSnapshotter(options: option)
snapShoter.start { (shot: MKMapSnapshot?, error: Error?) in
if error == nil {
let image = shot?.image
let data = UIImagePNGRepresentation(image!)
try? data?.write(to: URL(fileURLWithPath: "/Users/targetcloud/Desktop/snap.png"), options: [.atomic])
}
}
}
func camera() {//3D视图
let center = mapView.centerCoordinate
// 参数: 需要看的位置、 从哪个地方看、 站多高看
let camera: MKMapCamera = MKMapCamera(lookingAtCenter: center, fromEyeCoordinate: CLLocationCoordinate2DMake(center.latitude, center.longitude + 0.1), eyeAltitude: 500)
mapView.setCamera(camera, animated: true)
}
}
class TGAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0)
var title: String?
var subtitle: String?
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let annotation = view.annotation
if #available(iOS 9.0, *) {
(view.detailCalloutAccessoryView as! UILabel).text = (annotation?.subtitle)!
}
print("选中了\(annotation?.subtitle)")
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
let annotation = view.annotation
print("取消选中了\(annotation?.subtitle)")
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let iden = "item"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: iden)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: iden)
}
annotationView?.annotation = annotation
annotationView?.image = UIImage(named: "category_1")
annotationView?.centerOffset = CGPoint(x: 0, y: 0)
annotationView?.canShowCallout = true
annotationView?.calloutOffset = CGPoint(x: -10, y: 10)
if #available(iOS 9.0, *) {
let detailLabel : UILabel = UILabel()
detailLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
detailLabel.numberOfLines = 0
annotationView?.detailCalloutAccessoryView = detailLabel
}
annotationView?.isDraggable = true
return annotationView
}
}
swift地图定位(十六)poi及其他
最新推荐文章于 2017-04-12 17:37:42 发布