1.在Pods中导入AMap2DMap,AMapFoundation,AMaplocation,AMapSearch,PKHUD工具
2.创建一个OC桥接文件,导入:
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并关联
定义全局变量:
var mapView:MAMapView?
var search:AMapSearchAPI!
var searchButton:UIButton!
//两个位置,一个固定,一个动,计算距离
var coordinatePoint:MAMapPoint! //坐标(随便自己点的位置)
var locationPoint:MAMapPoint! //位置(固定位置)
5.添加一系列函数:
1)初始化地图:
func initMapView(){
let compassX = mapView?.compassOrigin.x
let scaleX = mapView?.scaleOrigin.x
//设置指南针和比例尺的位置
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//开启定位
self.view.addSubview(mapView!)
self.view.sendSubviewToBack(mapView!)
//设置用户是否自定义范围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)
}
//设置动画显示,默认为NO
poiAnnotationView!.animatesDrop = true
//设置气泡可以弹出,默认为NO
poiAnnotationView!.canShowCallout = true
//设置大头针颜色
poiAnnotationView?.pinColor = MAPinAnnotationColor.Purple
poiAnnotationView?.image = UIImage(named: "sport-car.png")
// poiAnnotationView!.canShowCallout = true //设置气泡可以弹出,默认为NO
// poiAnnotationView!.animatesDrop = true //设置标注动画显示,默认为NO
// poiAnnotationView!.draggable = true //设置标注可以拖动,默认为NO
// poiAnnotationView!.pinColor = MAPinAnnotationColor.Green
return poiAnnotationView
}
return nil
}
3).
//逆地理编码回调
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)
}
}
//逆地理编码请求
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?函数,intAddWarn
网友评论