swift地图定位(十六)poi及其他

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
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【课程特点】1、231节大容量课程:包含了SwiftUI的大部分知识点,详细讲解SwiftUI的方方面面;2、15个超级精彩的实例:包含美食、理财、健身、教育、电子商务等各行业的App实例;3、创新的教学模式:手把手教您SwiftUI用户界面开发技术,一看就懂,一学就会;4、贴心的操作提示:让您的眼睛始终处于操作的焦点位置,不用再满屏找光标;5、语言简洁精练:瞄准问题的核心所在,减少对思维的干扰,并节省您宝贵的时间;6、视频短小精悍:即方便于您的学习和记忆,也方便日后对功能的检索;7、齐全的学习资料:提供所有课程的源码,在Xcode 11 + iOS 13环境下测试通过; 更好的应用,更少的代码!SwiftUI是苹果主推的下一代用户界面搭建技术,具有声明式语法、实时生成界面预览等特性,可以为苹果手机、苹果平板、苹果电脑、苹果电视、苹果手表五个平台搭建统一的用户界面。SwiftUI是一种创新、简单的iOS开发中的界面布局方案,可以通过Swift语言的强大功能,在所有的Apple平台上快速构建用户界面。 仅使用一组工具和API为任何Apple设备构建用户界面。SwiftUI具有易于阅读和自然编写的声明式Swift语法,可与新的Xcode设计工具无缝协作,使您的代码和设计**同步。自动支持动态类型、暗黑模式、本地化和可访问性,意味着您的**行SwiftUI代码已经是您编写过的非常强大的UI代码了。 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值