高德定位和大头针的添加

2016-09-04  本文已影响720人  Dove_Q
#import <AMapSearchKit/AMapSearchKit.h>
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>

自动定位当前城市的经纬度

class FirstViewController: UIViewController ,CLLocationManagerDelegate {
    
    // 声明一个全局变量
    var locationManager:CLLocationManager!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager = CLLocationManager()
        
        // 设置定位的精确度
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        
        // 设置定位变化的最小距离 距离过滤器
        locationManager.distanceFilter = 50
        
        // 设置请求定位的状态
        locationManager.requestWhenInUseAuthorization()
        
        
        // 设置代理为当前对象
        locationManager.delegate = self;
        
        if CLLocationManager.locationServicesEnabled(){
            // 开启定位服务
            locationManager.startUpdatingLocation()
        }else{
            print("没有定位服务")
        }
        
    }
    // 定位失败调用的代理方法
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print(error)
    }
    // 定位更新地理信息调用的代理方法
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if locations.count > 0
        {
            let locationInfo = locations.last!
            let alert = UIAlertController(title: "获取的地理坐标", message: "经度是:\(locationInfo.coordinate.longitude),维度是:\(locationInfo.coordinate.latitude)", preferredStyle: .Alert)
            let action = UIAlertAction(title: "✅", style: .Default, handler: nil)
            alert.addAction(action)
            self.presentViewController(alert, animated: true, completion: nil)
        }
    }
    
}

长按地图获取当前位置的经纬度并且逆地址编码出城市及具体位置

class ViewController: UIViewController, MAMapViewDelegate, AMapLocationManagerDelegate, AMapSearchDelegate, UIGestureRecognizerDelegate{
    
    var pointAnnotations = Array<MAPointAnnotation>()
    var search: AMapSearchAPI!
    var mapView: MAMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        mapView = MAMapView(frame: CGRect(x: 0, y: 20, width: self.view.bounds.size.width, height: self.view.bounds.height / 2))
        mapView.showsUserLocation = true
        mapView.userTrackingMode = .Follow
        //MAMapViewDelegate
        mapView.delegate = self
        self.view.addSubview(mapView)
   
        search = AMapSearchAPI()
        search.delegate = self
       
    }
    
//    长按获取地址
    func mapView(mapView: MAMapView!, didLongPressedAtCoordinate coordinate: CLLocationCoordinate2D) {
        //coordinate: 经纬度
        animated(coordinate) //添加大头针
        regeoCodeRequeset(coordinate)
    }
    //逆地理编码请求
    func regeoCodeRequeset(coor: CLLocationCoordinate2D){
        let searchRequest = AMapReGeocodeSearchRequest()
        //经纬度格式为: 经度,纬度
        searchRequest.location = AMapGeoPoint.locationWithLatitude(CGFloat(coor.latitude), longitude: CGFloat(coor.longitude))
        self.search.AMapReGoecodeSearch(searchRequest)
        
    }
    //地理编码请求回调
    func onReGeocodeSearchDone(request: AMapReGeocodeSearchRequest, response: AMapReGeocodeSearchResponse) {
        
        print("request :\(request)")
        print("response :\(response)")
        
        if (response.regeocode != nil) {
            //经纬度
            let coordinate = CLLocationCoordinate2DMake(Double(request.location.latitude), Double(request.location.longitude))
            print(coordinate)
            //城市
            print(response.regeocode.addressComponent.city)
            //具体位置
            print(response.regeocode.formattedAddress)

        }
    }

在指定的经纬度添加大头针

//将大头针放到地图中去
    func animated(coordinate: CLLocationCoordinate2D) {
        //移除前面的大头针
        mapView.removeAnnotations(pointAnnotations)
        //添加大头针
        let pointAnnotation = MAPointAnnotation()
        //给大头针添加坐标
        pointAnnotation.coordinate = coordinate
        //将大头针添加到地图
        mapView.addAnnotation(pointAnnotation)
        
        pointAnnotations.append(pointAnnotation)
    }
    //添加大头针函数
    func mapView(mapView: MAMapView!, viewForAnnotation annotation: MAAnnotation!) -> MAAnnotationView! {
        if annotation.isKindOfClass(MAPointAnnotation) {

            var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier("pointReuseIndentifier") as? MAPinAnnotationView
            if annotationView == nil {
                annotationView = MAPinAnnotationView.init(annotation: annotation, reuseIdentifier: "pointReuseIndentifier")
            }
            //设置气泡可以弹出, 默认为NO
            annotationView!.canShowCallout = true
            //设置标注动画显示, 默认为NO
            annotationView!.animatesDrop = true
            //设置标柱可以拖动, 默认为NO
            annotationView?.draggable = true
            //大头针颜色
            annotationView?.pinColor = MAPinAnnotationColor.Red
            return annotationView
        }
        return nil
    }
}
上一篇下一篇

猜你喜欢

热点阅读