Swift3.0之点击地图控件MKMapView的某点获取该点的经纬度

Swift3.0之点击地图控件MKMapView的某点获取该点的经纬度_第1张图片
效果图
iOS开发中常常会用到自带的地图控件MKMapView,可以用如下方法在点击MKMapView中某点的时候获取改点的经纬度:

假如声明了一个MKMapView为mainMapView

1、在地图中添加一个手势,这个手势是一个点击动作,点击时会调用tapPress这个方法:

 let mTap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapPress(_:)))
        
        
 mainMapView.addGestureRecognizer(mTap)


2、接下来就可以在tapPress这个方法中进行转换了

func tapPress(_ gestureRecognizer:UITapGestureRecognizer){
        
  let touchPoint:CGPoint = gestureRecognizer.location(in: self.mainMapView)
        
        
  let touchMapCoordinate:CLLocationCoordinate2D = self.mainMapView.convert(touchPoint, toCoordinateFrom: self.mainMapView)
        
  let msg = String.init(format: "coordinate =  {%f, %f}", touchMapCoordinate.latitude, touchMapCoordinate.longitude)
        
  let alert = UIAlertView.init(title: "", message: msg, delegate: nil, cancelButtonTitle: "Ok", otherButtonTitles:"Cancel")
        alert.show()
        
    }

你可能感兴趣的:(Swift3.0之点击地图控件MKMapView的某点获取该点的经纬度)