1.在Pods中導(dǎo)入AMap2DMap,AMapFoundation,AMaplocation,AMapSearch,PKHUD工具
2.創(chuàng)建一個(gè)OC橋接文件,導(dǎo)入:
import <AMapSearchKit/AMapSearchKit.h>
import <MAMapKit/MAMapKit.h>
import <AMapFoundationKit/AMapFoundationKit.h>
import <MAMapKit/MAMapView.h>
import <AMapLocationKit/AMapLocationKit.h>
import <Foundation/Foundation.h>
import <AMapSearchKit/AMapSearchAPI.h>
3.在OneMapViewController界面中繼承
MAMapViewDelegate,AMapSearchDelegate,AMapLocationManagerDelegate,UIGestureRecognizerDelegate并關(guān)聯(lián)
定義全局變量:
var mapView:MAMapView?
var search:AMapSearchAPI!
var searchButton:UIButton!
//兩個(gè)位置,一個(gè)固定,一個(gè)動(dòng),計(jì)算距離
var coordinatePoint:MAMapPoint! //坐標(biāo)(隨便自己點(diǎn)的位置)
var locationPoint:MAMapPoint! //位置(固定位置)
5.添加一系列函數(shù):
1)初始化地圖:
func initMapView(){
let compassX = mapView?.compassOrigin.x
let scaleX = mapView?.scaleOrigin.x
//設(shè)置指南針和比例尺的位置
mapView?.compassOrigin = CGPointMake(compassX!,21)
mapView?.scaleOrigin = CGPointMake(scaleX!,21)
mapView = MAMapView(frame:self.view.bounds)
mapView?.delegate = self
// self.view.addSubview(mapView!)
// self.view.sendSubviewToBack(mapView!)
// mapView!.mapType = .Satellite
mapView!.showsUserLocation = true//開(kāi)啟定位
self.view.addSubview(mapView!)
self.view.sendSubviewToBack(mapView!)
//設(shè)置用戶(hù)是否自定義范圍circle
// mapView?.customizeUserLocationAccuracyCircleRepresentation = true
// mapView!.userTrackingMode = MAUserTrackingModeFollow
// mapView?.allowsBackgroundLocationUpdates = true
mapView?.userTrackingMode = .Follow
mapView!.distanceFilter = 500.0
mapView!.desiredAccuracy=kCLLocationAccuracyBestForNavigation
}
2).
添加大頭針:
func mapView(mapView: MAMapView, viewForAnnotation annotation: MAAnnotation) -> MAAnnotationView? {
if annotation.isKindOfClass(MAPointAnnotation) {
let annotationIdentifier = "invertGeoIdentifier"
var poiAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) as? MAPinAnnotationView
if poiAnnotationView == nil {
poiAnnotationView = MAPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
}
//設(shè)置動(dòng)畫(huà)顯示绽乔,默認(rèn)為NO
poiAnnotationView!.animatesDrop = true
//設(shè)置氣泡可以彈出饮怯,默認(rèn)為NO
poiAnnotationView!.canShowCallout = true
//設(shè)置大頭針顏色
poiAnnotationView?.pinColor = MAPinAnnotationColor.Purple
poiAnnotationView?.image = UIImage(named: "sport-car.png")
// poiAnnotationView!.canShowCallout = true //設(shè)置氣泡可以彈出轻纪,默認(rèn)為NO
// poiAnnotationView!.animatesDrop = true //設(shè)置標(biāo)注動(dòng)畫(huà)顯示壶辜,默認(rèn)為NO
// poiAnnotationView!.draggable = true //設(shè)置標(biāo)注可以拖動(dòng)逞泄,默認(rèn)為NO
// poiAnnotationView!.pinColor = MAPinAnnotationColor.Green
return poiAnnotationView
}
return nil
}
3).
//逆地理編碼回調(diào)
func onReGeocodeSearchDone(request: AMapReGeocodeSearchRequest, response: AMapReGeocodeSearchResponse) {
if (response.regeocode != nil) {
let coordinate = CLLocationCoordinate2DMake(Double(request.location.latitude), Double(request.location.longitude))
let annotation = MAPointAnnotation()
annotation.coordinate = coordinate
annotation.title = response.regeocode.formattedAddress
annotation.subtitle = response.regeocode.addressComponent.province
print(annotation.title) //獲取位置具體信息
mapView!.addAnnotation(annotation)
let overlay = MACircle(centerCoordinate: coordinate, radius: 40.0) //添加大頭針的半徑
mapView!.addOverlay(overlay)
}
}
//逆地理編碼請(qǐng)求
func oppositeGeoCodeRequeset(coor: CLLocationCoordinate2D){
let searchRequest = AMapReGeocodeSearchRequest()
searchRequest.location = AMapGeoPoint.locationWithLatitude(CGFloat(coor.latitude), longitude: CGFloat(coor.longitude))
self.search.AMapReGoecodeSearch(searchRequest)
}
4.其余添加
reloadMap,initSearch,initGestureRecognizer,searchReGeocodeWithCoordinate, gestureRecognizer,handleLongPress,mapView(mapView: MAMapView , didUpdateUserLocation userLocation: MAUserLocation ) ,twoPointDistance,dangerousWarn,
mapView(mapView: MAMapView, rendererForOverlay overlay: MAOverlay) -> MAOverlayRenderer?函數(shù),intAddWarn