可以使用原生地图,也可以使用高德地图或者其他,高德开发者网站会教你如何在各个平台使用高德地图。
原生地图
记得import MapKit
不要忘记设置info.plist
- ViewController
import UIKit
//地图
import MapKit
//位置
import CoreLocation
class ViewController: UIViewController {
//MARK: - 属性
//1.地图
var mapView = MKMapView()
//2.定位
//注意:CoreLocationManager的对象必须是类的属性或者全局变量,不能是函数中的局部变量。
let locationManager = YTLoactionManager()
//MAKR: - 方法
override func viewDidLoad() {
super.viewDidLoad()
//创建地图
self.creatMap()
//开始定位
// self.locationManager.startLoaction(self)
//添加长按手势
let longPress = UILongPressGestureRecognizer.init(target: self, action: "longPressAction:")
self.mapView.addGestureRecognizer(longPress)
}
}
//MARK: - 长按手势
extension ViewController{
func longPressAction(longPress:UILongPressGestureRecognizer) {
if longPress.state != .Began {
return
}
//1.拿到被长按的点的坐标
let point = longPress.locationInView(self.mapView)
//2.将坐标点转换成经纬度
let coord = self.mapView.convertPoint(point, toCoordinateFromView: self.mapView)
//3.拿到经纬度对应地址
//self.addAnnotation(coord, title: "", subtitle: "")
YTGeocoder.getAddress(coord) { (dict) in
let street = dict!["Street"] as! String
let name = dict!["Name"] as! String
self.addAnnotation(coord, title:street, subtitle: name)
}
}
}
//MARK: - 定位协议方法
extension ViewController:CLLocationManagerDelegate{
//位置改变的时候自动调用(地图跟随)
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
//获取到当前定位的位置信息
let coord = locations.first?.coordinate
//在地图上定位到当前位置
self.mapView.setRegion(MKCoordinateRegionMake(coord!, MKCoordinateSpanMake(0.01, 0.01)), animated: true)
//通过经纬度获取具体地址
YTGeocoder.getAddress(coord!) { (dict) in
print(dict)
//获取地址信息
let street = dict!["Street"] as! String
let name = dict!["Name"] as! String
//创建大头针
self.addAnnotation(coord!, title:street, subtitle: name)
}
}
}
//MARK: - 创建地图对象
extension ViewController{
func creatMap() {
//1.设置frame
self.mapView.frame = self.view.bounds
//2.添加到界面上
//注:地图上的"高德地图"标识的是,地图上的数据信息是高德的数据。
self.view.addSubview(self.mapView)
//3.设置地图类型
//Standard 标准地图
//Satellite 卫星地图
//Hybrid 卫星地图
self.mapView.mapType = .Standard
//4.将地图定位到指定的位置
//参数1:想要锁定的位置信息
//a.经纬度
//b.地图的放大系数(值越小放大倍数越大)
self.mapView.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(30.67, 104.06), MKCoordinateSpanMake(0.01, 0.01)), animated: true)
YTGeocoder.getAddress(CLLocationCoordinate2DMake(30.67, 104.06)) { (dict) in
// self.addAnnotation(CLLocationCoordinate2DMake(30.67, 104.06), title: dict!["Street"] as! String, subtitle: dict!["Name"] as! String)
}
//5.添加锚点(大头针)
// self.addAnnotation(CLLocationCoordinate2DMake(30.67, 104.06), title: "标题", subtitle: "副标题")
//6.设置代理
self.mapView.delegate = self
}
//添加大头针
func addAnnotation(coordinate:CLLocationCoordinate2D,title:String,subtitle:String){
//a.创建锚点对象
let annotattion = MKPointAnnotation.init()
//b.设置锚点对应的经纬度
annotattion.coordinate = coordinate
//c.设置title属性
annotattion.title = title
annotattion.subtitle = subtitle
//在地图上添加锚点
self.mapView.addAnnotation(annotattion)
}
}
//MARK: - 地图协议方法
extension ViewController:MKMapViewDelegate{
//1.定制大头针(和tableViewCell的定制几乎是一样)
//大头针是可以复用的
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?{
//a.去复用池中查看是否有可以复用的大头针
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("an")
//b.判断是否找到可以复用的大头针,如果没有找到就创建一个新的
if annotationView == nil {
annotationView = MKAnnotationView.init(annotation: annotation, reuseIdentifier: "an")
}
//c.刷新数据
//设置图片
annotationView?.image = UIImage.init(named: "luffy1")
//设置可以显示气泡
annotationView?.canShowCallout = true
//d.返回
return annotationView
}
}
- YTLoactionManager
import UIKit
import CoreLocation
class YTLoactionManager: CLLocationManager {
//开始定位
func startLoaction(delegate:CLLocationManagerDelegate?) {
//查看定位服务是否可用
if CLLocationManager.locationServicesEnabled(){
//1.请求定位服务
//a.设置info.plist
//b.请求服务
self.requestAlwaysAuthorization()
//2.设置定位精度
self.desiredAccuracy = kCLLocationAccuracyBest
//3.设置刷新距离
self.distanceFilter = 100
//4.设置代理
self.delegate = delegate
//5.开始定位
self.startUpdatingLocation()
}else{
print("定位服务不可用")
}
}
}
- YTGeocoder
import UIKit
import CoreLocation
class YTGeocoder: CLGeocoder {
static let defaultGecoder = YTGeocoder()
//1.将经纬度转换成地址
//注意:如果是类型方法,那么方法中的self指的是当前类。如果是变量型方法,那么方法中的self指的是当前对象
static func getAddress(coordinate:CLLocationCoordinate2D,finish:(NSDictionary?)->Void){
//将地址(经纬度)反编码成为真正的地址
//参数1:经纬度对应的地址
//参数2:反编码成功后会自动指定的闭包
self.defaultGecoder.reverseGeocodeLocation(CLLocation.init(latitude: coordinate.latitude, longitude: coordinate.longitude)) { (placeMarkArray, error) in
//拿到编码结果
let mark = placeMarkArray?.first
//返回结果
finish(mark?.addressDictionary)
}
}
//2.将地址转换成经纬度
static func getcoordinate(address:String,finish:([CLLocationCoordinate2D])->Void){
var retArray = [CLLocationCoordinate2D]()
self.defaultGecoder.geocodeAddressString(address) { (placeMarkArray, error) in
//1.遍历数组拿到每一个编码结果
for mark in placeMarkArray!{
//获取到经纬度对应的地址
let coord = mark.location?.coordinate
//将经纬度存到数组中
retArray.append(coord!)
}
//2.返回结果
finish(retArray)
}
}
}
高德地图
将资源拷贝到工程目录,然后工程配置
工程配置
一、3D地图
1.加入framework:
MAMapKit.framework
AMapFoundationKit
2.添加地图资源文件:AMap.bundle
3.添加系统静态依赖库
UIKit.framework
Foundation.framework
CoreGraphics.framework
QuartzCore.framework
OpenGLES.framework
CoreLocation.framework
CoreTelephony.framework
SystemConfiguration.framework
libz.tbd
libstdc++ 6.0.9.tbd
libc++.tbd
Security.framework
AdSupport.framework
4.设置other linker flags :-ObjC
-ObjC5.创建桥接文件,添加头文件:
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <MAMapKit/MAMapKit.h>
6.设置plist文件
6.1.配置网络
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
6.2.配置地图回调
<key>LSApplicationQueriesSchemes</key>
<array>
<string>iosamap</string>
</array>
7.配置key值
AMapServices.sharedServices().apiKey = "自己去高德开发者网站申请"
8.具体实现
二、定位
1.加入framework:
AMapFoundationKit.framework
AMapLocationKit.framework
2.添加系统静态依赖库
libz.tbd
libc++.tbd
libstdc++ 6.0.9.tbd
CoreTelephony.framework
SystemConfiguration.framework
3.创建桥接文件,添加头文件:
#import <AMapLocationKit/AMapLocationKit.h>
4.设置定位权限
在项目的 Info.plist 添加 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription 字段
三、POI搜索
1.加入framework:
AMapFoundationKit.framework
AMapSearchKit.framework
2.添加系统静态依赖库
同地图
3.创建桥接文件,添加头文件
#import <AMapSearchKit/AMapSearchKit.h>
例子代码
配置太复杂了,配置后的结果
桥接文件
#ifndef ZGJ_Briding_Header_h
#define ZGJ_Briding_Header_h
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#endif /* ZGJ_Briding_Header_h */
AppDelegate
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//配置地图Key值(要去高德开发者网站申请)
AMapServices.sharedServices().apiKey = "30d5585b86658411a80712e97427bdb2"
return true
}
}
ViewController
import UIKit
class ViewController: UIViewController {
//属性
//1.地图视图
let mapView = MAMapView()
//2.搜索对象
let search = AMapSearchAPI()
override func viewDidLoad() {
super.viewDidLoad()
self.creatMap()
self.addLine()
//设置搜索对象的代理(搜索结果是通过协议方法实现的)
self.search.delegate = self
//key值验证失败,搜索失败
self.startSearch()
}
}
//MARK: - 创建地图
extension ViewController{
func creatMap(){
//1.设置frame
self.mapView.frame = self.view.bounds
//2.添加到界面
self.view.addSubview(self.mapView)
//3.设置定位
self.mapView.setRegion(MACoordinateRegionMake(CLLocationCoordinate2DMake(30.6, 104.06), MACoordinateSpanMake(0.01, 0.01)), animated: true)
//4.添加锚点
let annotation = MAPointAnnotation()
//经纬度
annotation.coordinate = CLLocationCoordinate2DMake(30.6, 104.06)
annotation.title = "四川省成都市"
annotation.subtitle = "青羊区"
self.mapView.addAnnotation(annotation)
//5.设置代理
self.mapView.delegate = self
}
}
//MARK: - 协议
extension ViewController:MAMapViewDelegate{
//1.自定义大头针
func mapView(mapView: MAMapView!, viewForAnnotation annotation: MAAnnotation!) -> MAAnnotationView! {
//查看复用池中是否有可用的大头针
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier("an")
//如果没有就创建新的
if anView == nil{
anView = MAAnnotationView(annotation: annotation, reuseIdentifier: "an")
}
//是否可以弹出气泡
anView.canShowCallout = true
//是否支持拖动
anView.draggable = true
//设置图片(自定义的要设置图片才看得见锚点)
anView.image = UIImage(named: "locationImage.png")
return anView
}
//2.定制图层
func mapView(mapView: MAMapView!, rendererForOverlay overlay: MAOverlay!) -> MAOverlayRenderer! {
if overlay.isKindOfClass(MAPolyline.self){
let polyLineView = MAPolylineRenderer(overlay: overlay)
//设置线宽
polyLineView.lineWidth = 3
//设置填充颜色
polyLineView.strokeColor = UIColor.redColor()
//设置连接处的样式
polyLineView.lineJoinType = kMALineJoinRound
//设置线断点
polyLineView.lineCapType = kMALineCapArrow
return polyLineView
}
return nil
}
}
//MARK: - 画折线图
extension ViewController{
func addLine(){
//实质是在地图上添加图层(画线)
//创建多个经纬度
let coord1 = CLLocationCoordinate2DMake(30.6, 104.06)
let coord2 = CLLocationCoordinate2DMake(30.62, 104.064)
let coord3 = CLLocationCoordinate2DMake(30.64, 104.07)
let coord4 = CLLocationCoordinate2DMake(30.66, 104.083)
//必须是var
var coordArray = [coord1,coord2,coord3,coord4]
//根据多个经纬度确定一条折线
let line = MAPolyline(coordinates: &coordArray, count: 4)
self.mapView.addOverlay(line)
//想要显示折现要在协议里面实现方法
}
}
//MARK: - AMapSearch协议
extension ViewController:AMapSearchDelegate{
//1.搜索功能
func startSearch(){
//1.创建搜索对象
let request = AMapPOIKeywordsSearchRequest()
//2.设置搜索对象
//a.设置搜索关键字
request.keywords = "医院"
//b.设置城市
request.city = "成都"
//c.设置类型
request.types = "医院"
//d.是否扩展搜索
request.requireExtension = true
//e.是否限制城市
request.cityLimit = true
//不知道
request.requireSubPOIs = true
//3.开始搜索
self.search.AMapPOIKeywordsSearch(request)
}
//*******协议方法******
//请求错误
func AMapSearchRequest(request: AnyObject!, didFailWithError error: NSError!) {
print("搜索请求错误")
}
//所搜结果
func onPOISearchDone(request: AMapPOISearchBaseRequest!, response: AMapPOISearchResponse!) {
//判断搜索到的结果是否为空
if response.pois.count == 0{
return
}
//拿到结果数组
let retArray = response.pois
//展示结果
self.mapView.addAnnotations(retArray)
print(retArray) //测试
}
}
网友评论