[Xcode 实际操作]四、常用控件-(18)MKMapView地图,将地理坐标转换为实际地名

目录:[Swift]Xcode实际操作

本文将演示将地理坐标转换为实际地名。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

 1 import UIKit
 2 //首先往视图控制器类文件中,引入地图框架
 3 import MapKit
 4 
 5 //添加地理地图代理协议MKMapViewDelegate
 6 class ViewController: UIViewController {
 7 
 8     override func viewDidLoad() {
 9         super.viewDidLoad()
10         // Do any additional setup after loading the view, typically from a nib.
11         //初始化一个地理位置解析类,使用该类进行地理坐标的反向解析
12         let geocoder = CLGeocoder()
13         
14         //通过设定经纬度,来创建一个地理位置
15         let location = CLLocation(latitude: 39.9, longitude: 116.3)
16         //使用地理位置解析对象,解析地理坐标,
17         geocoder.reverseGeocodeLocation(location){ (placeMarks:[CLPlacemark]?, error:Error?)
18             ->Void in
19             //在闭包语句里,处理解析后的结果。
20             //当结果中的位置标记数组,长度大于0,执行接下来的操作
21             if (placeMarks?.count)! > 0
22             {
23                 //获得位置标记数组中的第一个元素
24                 let placeMark = placeMarks?.first
25                 //在控制台打印输出解析后的结果
26                 print(placeMark?.name ?? "")
27             }
28         }
29     }
30 
31     override func didReceiveMemoryWarning() {
32         super.didReceiveMemoryWarning()
33         // Dispose of any resources that can be recreated.
34     }
35 }

 

转载于:https://www.cnblogs.com/strengthen/p/10017349.html

要在MKMapView地图上显示提示框,可以使用MKAnnotationView来实现。以下是在Swift中的示例代码: 首先,需要定义一个实现了MKAnnotation协议的类来表示地图上的标注点,例如: ``` import MapKit class MyAnnotation: NSObject, MKAnnotation { var coordinate: CLLocationCoordinate2D var title: String? var subtitle: String? init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?) { self.coordinate = coordinate self.title = title self.subtitle = subtitle } } ``` 然后,在MKMapViewDelegate的方法中创建MKAnnotationView,并设置其属性,例如: ``` func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { guard let annotation = annotation as? MyAnnotation else { return nil } let identifier = "MyAnnotationView" var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView if annotationView == nil { annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) annotationView?.canShowCallout = true annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) } else { annotationView?.annotation = annotation } return annotationView } ``` 在这个方法中,我们创建了一个MKPinAnnotationView,并设置了它的canShowCallout属性为true,表示可以显示提示框。同时,我们还设置了它的rightCalloutAccessoryView属性为一个UIButton,表示在提示框的右侧添加一个详情按钮。 最后,我们需要在ViewController中创建MyAnnotation对象,并将它添加到MKMapView中,例如: ``` let annotation = MyAnnotation(coordinate: CLLocationCoordinate2D(latitude: 37.33182, longitude: -122.03118), title: "Apple Inc.", subtitle: "Cupertino") mapView.addAnnotation(annotation) ``` 这样,当我们运行应用程序时,在地图上就会显示一个名为"Apple Inc."的标注点,并且当我们点击这个标注点时,就会显示一个提示框,并且在提示框的右侧会有一个详情按钮。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值