标签:
Standard :普通地图
Satellite : 卫星云图
Hybrid : 普通 + 卫星云图
SatelliteFlyover ios9.0 3D立体
HybridFlyover ios9.0 3D立体混合
mapView.mapType = .HybridFlyover
mapView.scrollEnabled = false //滚动
mapView.rotateEnabled = false //旋转
mapView.zoomEnabled = false //缩放 缩放和滚动默认是有的
mapView.pitchEnabled = false //3D
if #available(iOS 9.0, *) {
mapView.showsCompass = true //显示指南针
mapView.showsScale = true //显示比例尺
mapView.showsTraffic = true //显示交通
}
mapView.showsBuildings = true //显示建筑物
mapView.showsPointsOfInterest = true //显示兴趣点
mapView.showsUserLocation = true //显示用户
//1. 显示用户的位置后,地图并不跟着用户的位置的改变而改变视角
mapView.showsUserLocation = true //显示用户
//2. 显示用户的位置之后,地图会跟随着用户的位置,一直让用户的位置显示在地图的中心,当时不灵活
mapView.userTrackingMode = .FollowWithHeading
//懒加载
private lazy var location : CLLocationManager = {
let location = CLLocationManager()
if #available(iOS 8.0, *) {
location.requestAlwaysAuthorization()
}
return location
}()
override func viewDidLoad() {
super.viewDidLoad()
mapView.showsBuildings = true
if #available(iOS 9.0, *) {
mapView.mapType = .HybridFlyover
}
mapView.mapType = .Standard
mapView.showsUserLocation = true
_ = location
mapView.userTrackingMode = .FollowWithHeading
}
extension ViewController : MKMapViewDelegate {
//当地图获取到用户的位置的时候回调用该方法
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
//MKUserLocation 大头数据模型 只要遵守了MKAnnotation就是大头针数据模型
//获取用户的大头针数据模型
userLocation.title = "肖锋"
userLocation.subtitle = "你好吗????"
//获取用户当前的中心位置
let center = userLocation.location?.coordinate
mapView.setCenterCoordinate(center!, animated: true)
//改变显示区域
let span = MKCoordinateSpanMake(0.162493481087147, 0.10857004327103)
let region = MKCoordinateRegionMake(center!, span)
mapView.setRegion(region, animated: true)
}
//当区域改变的时候会调用该方法
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
print(mapView.region.span)
}
}
import MapKit
class XFJAnnotation: NSObject , MKAnnotation {
//位置坐标
var coordinate : CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0)
//标题和子标题
var title : String?
var subtitle : String?
}
//拿到地图
@IBOutlet weak var mapView: MKMapView!
//点击屏幕获取
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//获取点
let point = touches.first?.locationInView(mapView)
//将试图上的点转化为经度和纬度
let coodinate = mapView.convertPoint(point!, toCoordinateFromView: mapView)
//添加大头针
let annotation = addAnntation(coodinate, title: "肖锋", subtitle: "嘿嘿!!!")
//反地理编码获取详细信息
let location = CLLocation(latitude: coodinate.latitude, longitude: coodinate.longitude)
geoc.reverseGeocodeLocation(location) { (clplc : [CLPlacemark]?, error : NSError?) -> Void in
//检查内容是否为空
guard let clplc = clplc else {return}
guard let clplcs = clplc.first else {return}
//设置大头针的标题和子标题
annotation.title = clplcs.locality
annotation.subtitle = clplcs.name
}
}
//移动屏幕移除所有的大头针
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
//取出所有的大头针
let annotation = mapView.annotations
//移除有所的大头针
mapView.removeAnnotations(annotation)
}
///MARK : - 创建大头针
extension ViewController {
private func addAnntation(coordinate : CLLocationCoordinate2D, title : String?, subtitle : String?) ->XFJAnnotation
{
//创建大头针
let annotation = XFJAnnotation()
//设置标题
annotation.title = title
annotation.subtitle = subtitle
//设置大头针的位置
annotation.coordinate = coordinate
//添加大头针
mapView.addAnnotation(annotation)
//返回大头针
return annotation
}
}
//添加大头针
/*
当创建一个大头针数据模型添加到地图上之后,会执行对应的代理方法,在代理方法中查找对应的大头针视图
*/
mapView.addAnnotation(annotation)
extension ViewController : MKMapViewDelegate {
//当添加一个大头针数据模型,会调用该方法,寻找对应的大头针视图并返回,如果此处返回的是nil,代表使用系统默认的大头针
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
//创建标识
let pinID = "pinID"
//从缓存池中通过标识取出大头针视图
var pinAnntationView = mapView.dequeueReusableAnnotationViewWithIdentifier(pinID) as? MKPinAnnotationView
//判断大头针是否为空
if pinAnntationView == nil {
pinAnntationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: pinID)
}
//设置数据模型(必须设置的)
pinAnntationView?.annotation = annotation
//运行弹框
pinAnntationView?.canShowCallout = true
//设置大头针的颜色
pinAnntationView?.pinTintColor = UIColor.redColor()
//设置大头针的下落动画
pinAnntationView?.animatesDrop = true
//拖动大头针顶视图
pinAnntationView?.draggable = true
//返回大头针视图
return pinAnntationView
}
//当拖动大头针视图的时候会调用
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
print(newState.rawValue,oldState.rawValue)
}
//当选中大头针的时候会调用
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
print("选中了\(view.annotation!.title)")
}
//当反选中大头针的时候回调用
func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {
print("反选中了\(view.annotation?.title)")
}
//大头针的左边视图
let image = UIImage(named: "Snip20160508_1")
let imageView = UIImageView(frame: CGRectMake(0, 0, 50, 50))
imageView.image = image
pinAnntationView?.leftCalloutAccessoryView = imageView
//大头针的右边视图
let image1 = UIImage(named: "Snip20160508_2")
let imageView1 = UIImageView(frame: CGRectMake(0, 0, 60, 60))
imageView1.image = image1
pinAnntationView?.rightCalloutAccessoryView = imageView1
override func viewDidLoad() {
super.viewDidLoad()
let userTrackingItem = MKUserTrackingBarButtonItem(mapView: mapView)
navigationItem.rightBarButtonItem = userTrackingItem
_ = location
}
标签:
原文地址:http://blog.csdn.net/xf931456371/article/details/51348814