集成時(shí)我們首先要申請(qǐng)本應(yīng)用對(duì)應(yīng)的key這個(gè)在官方文檔里面介紹的很詳細(xì)
在使用高德地圖前奈搜,我們首先要在code中注冊(cè)key
AMapServices.shared().apiKey = "a75721f344a52d***************";
初始化
//地圖初始化
let mapView = MAMapView(frame: UIScreen.main.bounds);
//搜索初始化
let search = AMapSearchAPI()
相關(guān)配置
view.addSubview(mapView)
//顯示地圖范圍(比例)
mapView.zoomLevel = 14;
//配置(可選)
let config = MAUserLocationRepresentation();
config.showsHeadingIndicator = true;
config.locationDotBgColor = UIColor.green;
config.image = UIImage(named: "test");
mapView.update(config);
// mapView.logoCenter = CGPoint(x: view.bounds.width + 155, y: 450);
//指南針及其位置
mapView.showsCompass = true;
mapView.compassOrigin = CGPoint(x: view.bounds.width - 45, y: 64);
//旋轉(zhuǎn)手勢(shì)關(guān)閉
mapView.isRotateEnabled = false;
//相關(guān)代理
mapView.delegate = self;
//設(shè)置定位精度
mapView.desiredAccuracy = kCLLocationAccuracyBest;
//設(shè)置定位距離
mapView.distanceFilter = 1.0;
//普通樣式
mapView.mapType = .standard;
//防止系統(tǒng)自動(dòng)殺掉定位 -- 后臺(tái)定位
mapView.pausesLocationUpdatesAutomatically = false;
//需要設(shè)置plist文件 Required background modes : App registers for location updates
mapView.allowsBackgroundLocationUpdates = true;
//確保定位和跟隨設(shè)置不被覆蓋
mapView.showsUserLocation = true;
mapView.userTrackingMode = .follow
截屏
//截屏
let image = mapView.takeSnapshot(in: self.view.bounds);
地址正向解析和逆地址編碼(具體結(jié)果需要代理實(shí)現(xiàn))
let userLocation = mapView.userLocation.coordinate;
NSLog("first latitude : %f , longitude : %f",userLocation.latitude,userLocation.longitude);
let regeo = AMapReGeocodeSearchRequest();
regeo.location = AMapGeoPoint.location(withLatitude: CGFloat(userLocation.latitude), longitude: CGFloat(userLocation.longitude));
regeo.requireExtension = true;
//逆地址編碼
search?.delegate = self
search?.aMapReGoecodeSearch(regeo);
//正向地址解析
let request = AMapGeocodeSearchRequest();
request.address = "成都市天府廣場(chǎng)";
search?.aMapGeocodeSearch(request);
地址正向解析和逆地址編碼代理實(shí)現(xiàn)(正向地址解析完成時(shí)杭跪,在解析地址處添加大頭針)
//MAKE: 逆地址編碼
extension MapViewController: AMapSearchDelegate{
func onReGeocodeSearchDone(_ request: AMapReGeocodeSearchRequest!, response: AMapReGeocodeSearchResponse!) {
guard response.regeocode != nil else {
return
}
let returnValue = response.regeocode.addressComponent.building;
let city: String = response.regeocode.addressComponent.city;
NSLog("%@",city );
print("\(String(describing: returnValue))")
}
//MAKE: 地址正向解析
func onGeocodeSearchDone(_ request: AMapGeocodeSearchRequest!, response: AMapGeocodeSearchResponse!) {
guard response.geocodes.count != 0 else {
return;
}
for geocode in response.geocodes {
print("latitude: \(geocode.location.latitude) ,longitude: \(geocode.location.longitude)");
//大頭針妥衣。正向地址解析完成時(shí)哑诊,在解析地址處添加大頭針
//需要實(shí)現(xiàn)代理
let pointAnnotation = MAPointAnnotation();
pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(geocode.location.latitude), longitude: CLLocationDegrees(geocode.location.longitude))
pointAnnotation.title = geocode.building
pointAnnotation.subtitle = geocode.formattedAddress
mapView.addAnnotation(pointAnnotation);
}
}
}
添加大頭針需要實(shí)現(xiàn)的代理
//大頭針代理
func mapView(_ mapView: MAMapView!, viewFor annotation: MAAnnotation!) -> MAAnnotationView! {
if annotation.isKind(of: MAPointAnnotation.self) {
let pointReuseIndetifier = "pointReuseIndetifier";
var annottationView: MAPinAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier) as! MAPinAnnotationView?
if annottationView == nil {
annottationView = MAPinAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier);
}
annottationView?.canShowCallout = true;
annottationView?.animatesDrop = true;
annottationView?.isDraggable = true;
annottationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
return annottationView
}
return nil
}
結(jié)果:
自定義大頭針視圖
class CustomCalloutView: UIView {
public var portraitView: UIImageView!
public var subtitleLabel: UILabel!
public var titlelabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame);
self.backgroundColor = UIColor.clear;
initSubViews();
}
func initSubViews() {
self.portraitView = UIImageView(frame: CGRect(x: 5, y: 5, width: 70, height: 50))
portraitView.backgroundColor = UIColor.black;
self.addSubview(portraitView);
titlelabel = UILabel(frame: CGRect(x: 10 + 70, y: 5, width: 120, height: 20));
titlelabel.font = UIFont.boldSystemFont(ofSize: 14);
titlelabel.textColor = UIColor.white;
titlelabel.text = "something say title";
self.addSubview(titlelabel)
subtitleLabel = UILabel(frame: CGRect(x: 10 + 70, y: 30, width: 120, height: 20));
subtitleLabel.font = UIFont.systemFont(ofSize: 12);
subtitleLabel.textColor = UIColor.lightGray;
subtitleLabel.text = "afhkshfksahflaskfsdnlkasjflaks rubish";
self.addSubview(subtitleLabel);
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
self.drawInContext(context: UIGraphicsGetCurrentContext()!);
self.layer.shadowColor = UIColor.black.cgColor;
self.layer.shadowOpacity = 1.0;
self.layer.shadowOffset = CGSize(width: 0, height: 0)
}
func drawInContext(context: CGContext) {
context.setLineWidth(2.0);
context.setFillColor(UIColor.coloreWithRGB(red: 77, green: 77, blue: 77, alpha: 0.3).cgColor);
self.getDrawPath(context: context);
context.fillPath();
}
func getDrawPath(context: CGContext){
let rrect = self.bounds;
let radius:CGFloat = 6.0;
let minx:CGFloat = rrect.minX
let midx:CGFloat = rrect.midX;
let maxx:CGFloat = rrect.maxX;
let miny:CGFloat = rrect.minY;
let maxy:CGFloat = rrect.maxY - 10;
context.move(to: CGPoint(x: midx + 10, y: maxy));
context.addLine(to: CGPoint(x: midx + 10, y: maxy + 10))
context.addLine(to: CGPoint(x: midx - 10, y: maxy));
context.addArc(tangent1End: CGPoint(x: minx, y: maxy), tangent2End: CGPoint(x: midx, y: miny), radius: radius);
context.addArc(tangent1End: CGPoint(x: minx, y:minx), tangent2End: CGPoint(x: maxx, y: miny), radius: radius);
context.addArc(tangent1End: CGPoint(x: maxx, y: miny), tangent2End: CGPoint(x: maxx , y: maxx), radius: radius);
context.addArc(tangent1End: CGPoint(x: maxx, y: maxy), tangent2End: CGPoint(x: midx, y: maxy), radius: radius);
context.closePath();
}
}
class CustomAnnotationView: MAAnnotationView {
public var calloutView: CustomCalloutView?
override func setSelected(_ selected: Bool, animated: Bool) {
if selected {
if calloutView == nil {
calloutView = CustomCalloutView(frame: CGRect(x: 0, y: 0, width: 200, height: 70));
calloutView!.center = CGPoint(x: self.bounds.width / 2 + self.calloutOffset.x, y: -self.bounds.height / 2 + self.calloutOffset.y);
calloutView!.portraitView.image = UIImage(named: "test");
calloutView!.titlelabel.text = self.annotation.title;
calloutView!.subtitleLabel.text = self.annotation.subtitle;
self.addSubview(calloutView!)
}
}else {
calloutView?.removeFromSuperview();
}
super.setSelected(selected, animated: animated);
}
}
let pointReuseIndetifier = "pointReuseIndetifier";
//自定義
var annotationView: CustomAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: pointReuseIndetifier) as! CustomAnnotationView?
if annotationView == nil {
annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: pointReuseIndetifier);
}
annotationView?.image = UIImage(named: "test");
// 設(shè)置為NO吠冤,用以調(diào)用自定義的calloutView
annotationView?.canShowCallout = false
let button = UIButton(type: .detailDisclosure)
annotationView?.rightCalloutAccessoryView = button;
//選中當(dāng)前自定義大頭針
annotationView?.setSelected(true, animated: true)
// 設(shè)置中心點(diǎn)偏移雕沉,使得標(biāo)注底部中間點(diǎn)成為經(jīng)緯度對(duì)應(yīng)點(diǎn)
annotationView!.centerOffset = CGPoint(x: -10, y: 0)