集成时我们首先要申请本应用对应的key这个在官方文档里面介绍的很详细
在使用高德地图前,我们首先要在code中注册key
AMapServices.shared().apiKey = "a75721f344a52d***************";
初始化
//地图初始化
let mapView = MAMapView(frame: UIScreen.main.bounds);
//搜索初始化
let search = AMapSearchAPI()
相关配置
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);
//旋转手势关闭
mapView.isRotateEnabled = false;
//相关代理
mapView.delegate = self;
//设置定位精度
mapView.desiredAccuracy = kCLLocationAccuracyBest;
//设置定位距离
mapView.distanceFilter = 1.0;
//普通样式
mapView.mapType = .standard;
//防止系统自动杀掉定位 -- 后台定位
mapView.pausesLocationUpdatesAutomatically = false;
//需要设置plist文件 Required background modes : App registers for location updates
mapView.allowsBackgroundLocationUpdates = true;
//确保定位和跟随设置不被覆盖
mapView.showsUserLocation = true;
mapView.userTrackingMode = .follow
截屏
//截屏
let image = mapView.takeSnapshot(in: self.view.bounds);
地址正向解析和逆地址编码(具体结果需要代理实现)
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 = "成都市天府广场";
search?.aMapGeocodeSearch(request);
地址正向解析和逆地址编码代理实现(正向地址解析完成时,在解析地址处添加大头针)
//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)");
//大头针。正向地址解析完成时,在解析地址处添加大头针
//需要实现代理
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);
}
}
}
添加大头针需要实现的代理
//大头针代理
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
}
结果:
9C813AC3-9E16-4283-9EF1-85F3A5A4BEEE.png
自定义大头针视图
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");
// 设置为NO,用以调用自定义的calloutView
annotationView?.canShowCallout = false
let button = UIButton(type: .detailDisclosure)
annotationView?.rightCalloutAccessoryView = button;
//选中当前自定义大头针
annotationView?.setSelected(true, animated: true)
// 设置中心点偏移,使得标注底部中间点成为经纬度对应点
annotationView!.centerOffset = CGPoint(x: -10, y: 0)
tmp24f32b21.png
网友评论