swift开发笔记28 使用系统自带地图

之前用的是百度地图,最后上架打包时提示百度的类库在swift中未完全编译,上架时必须取消自动bitcode功能。

于是打算用系统自带的地图取代原来的百度地图,其实也不需要该多少代码,百度地图的类名往往也有对应的系统地图类名,只是加了个“B”,例如:

多边形类:MKPolygon,在百度地图改成了:BMKPolygon。

首先去掉百度类库framework,加上系统地图类库MapKit.framework:


去掉:baidumap开头的这几个, 以及百度要用,但是我需要的QuartzCore.framework、OpenGLES.framework、SystemConfiguration.framework、CoreGraphics.framework、Security.framework等,


然后把项目目录下的百度文件删了。。。还有桥接文件里的几个头文件

然后定位跟踪和绘制矩形代码:

import UIKit
import MapKit
import CoreLocation
class BaiduMapViewController: UIViewController,MKMapViewDelegate {
    
    @IBOutlet weak var iosmap: MKMapView!
    var locationManager:CLLocationManager!
    var centerx:Double?
    var centery:Double?
    override func viewDidLoad() {
        super.viewDidLoad()
        iosmap.mapType = .Standard
        iosmap.delegate = self
        iosmap.showsBuildings = true
        if CLLocationManager.locationServicesEnabled(){
            iosmap.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
            self.locationManager=CLLocationManager()
            locationManager.requestWhenInUseAuthorization()
            locationManager.requestAlwaysAuthorization()
        }
        //上班地点区域
        addPloygon()
    }
    //跟踪用户位置 并调整地图范围
    func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
        if  let loc=userLocation.location {
            self.iosmap.centerCoordinate = loc.coordinate
            let viewregion=MKCoordinateRegionMakeWithDistance(loc.coordinate, 10000, 10000)
            self.iosmap.setRegion(viewregion, animated: true)
        }
    }
    //添加矩形区域
    func addPloygon (){
        //左下
        let x1 = 116.320313
        let y1 = 39.906578 
        //右上
        let x2 = 116.340723
        let y2 = 39.91869  
        // 添加多边形覆盖物
        var coords = [CLLocationCoordinate2D]()
        coords.append(CLLocationCoordinate2DMake(y1, x1))
        coords.append(CLLocationCoordinate2DMake(y!, x2))
        coords.append(CLLocationCoordinate2DMake(y2, x2))
        coords.append(CLLocationCoordinate2DMake(y2, x1))
        
        let  polygon = MKPolygon(coordinates: &coords, count:  Int(coords.count)) 
        self.iosmap.addOverlay(polygon) 
 
    }
//矩形的样式
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKPolygon {
        let polygonView = MKPolygonRenderer(overlay: overlay)
        polygonView.strokeColor = UIColor.redColor()
        
        return polygonView
    }
    return MKOverlayRenderer()
}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值