iOS --- 地图框架MapKit的简单使用

iOS中可以简单地使用MapKit框架来进行地图的相关开发工作.

基本步骤:

  1. import MapKit
  2. ViewController 继承 MKMapViewDelegate 协议
  3. 添加一个MapKit View
  4. 准备一个相应的region信息, 即以哪为中心, 方圆多少范围
  5. 在mapView中设置该region即可
  6. 添加地理位置的标注annotation
  7. 地理位置标注添加到map中的相应操作.

ViewController

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        mapView.delegate = self
        // MKMapView中有一个delegate属性, ViewController继承MKMapViewDelegate协议, 就必须实现该协议中的必需的方法

        let location = CLLocationCoordinate2D(latitude: 22.284681, longitude: 114.158177)
        let span = MKCoordinateSpanMake(0.05, 0.05)
        // region可以视为以location为中心, 方圆多少范围
        let region = MKCoordinateRegion(center: location, span: span)
        // mapView会显示该region的map
        // mapView.mapType = MKMapType.Standard
        mapView.setRegion(region, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

ViewController如上所示, 只需准备好location, 然后只需mapView的setRegion操作即可呈现map.

地理位置标注annotation

在map中, 我们可以在指定的位置添加一个标注annotation.

        // 在地图上添加一个位置标注
        let annotation = MKPointAnnotation()
        annotation.coordinate = location
        annotation.title = "Hong Kong"
        annotation.subtitle = "Someplace"
        mapView.addAnnotation(annotation)

        // 在地图上添加另一个位置标注
        let location2 = CLLocationCoordinate2D(latitude: 22.294681, longitude: 114.170177)
        let annotation2: MKPointAnnotation = MKPointAnnotation()
        annotation2.coordinate = location2
        annotation2.title = "Hong Kong"
        annotation2.subtitle = "Someplace2"
        mapView.addAnnotation(annotation2)

效果如图所示:

annotation 1annotation 2
Hong Kong SomeplaceHong Kong Someplace2

annotation显示时的操作

有时候, 我们希望地图出现annotation时候执行一些操作, 如自动放到或缩小.

    // 呈现该annotation的时候, 调用该方法
    func mapView(mapView: MKMapView!, didAddAnnotationViews views: [AnyObject]!) {
        println("didAddAnnotationViews")
        let annotationView: MKAnnotationView = views[0] as! MKAnnotationView
        let annotation = annotationView.annotation
        let region: MKCoordinateRegion = MKCoordinateRegionMakeWithDistance(annotation.coordinate, 500, 500)
        self.mapView.centerCoordinate = region.center
        self.mapView.setRegion(region, animated: true)
        self.mapView.selectAnnotation(annotation, animated: true)
    }
}

如下图: 该annotation一旦在map中呈现, 则自动跳转到以该region的center为map中心, 指定范围的一片区域.
annotation呈现时的操作

除此之外, 下一篇准备学习高德地图的相关开发.

Demo

Demo地址: DemoLBS

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值