#import <AMapSearchKit/AMapSearchKit.h>
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
自動(dòng)定位當(dāng)前城市的經(jīng)緯度
class FirstViewController: UIViewController ,CLLocationManagerDelegate {
// 聲明一個(gè)全局變量
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
// 設(shè)置定位的精確度
locationManager.desiredAccuracy = kCLLocationAccuracyBest
// 設(shè)置定位變化的最小距離 距離過(guò)濾器
locationManager.distanceFilter = 50
// 設(shè)置請(qǐng)求定位的狀態(tài)
locationManager.requestWhenInUseAuthorization()
// 設(shè)置代理為當(dāng)前對(duì)象
locationManager.delegate = self;
if CLLocationManager.locationServicesEnabled(){
// 開啟定位服務(wù)
locationManager.startUpdatingLocation()
}else{
print("沒有定位服務(wù)")
}
}
// 定位失敗調(diào)用的代理方法
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
}
// 定位更新地理信息調(diào)用的代理方法
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.count > 0
{
let locationInfo = locations.last!
let alert = UIAlertController(title: "獲取的地理坐標(biāo)", message: "經(jīng)度是:\(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)
}
}
}
長(zhǎng)按地圖獲取當(dāng)前位置的經(jīng)緯度并且逆地址編碼出城市及具體位置
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
}
// 長(zhǎng)按獲取地址
func mapView(mapView: MAMapView!, didLongPressedAtCoordinate coordinate: CLLocationCoordinate2D) {
//coordinate: 經(jīng)緯度
animated(coordinate) //添加大頭針
regeoCodeRequeset(coordinate)
}
//逆地理編碼請(qǐng)求
func regeoCodeRequeset(coor: CLLocationCoordinate2D){
let searchRequest = AMapReGeocodeSearchRequest()
//經(jīng)緯度格式為: 經(jīng)度攻礼,緯度
searchRequest.location = AMapGeoPoint.locationWithLatitude(CGFloat(coor.latitude), longitude: CGFloat(coor.longitude))
self.search.AMapReGoecodeSearch(searchRequest)
}
//地理編碼請(qǐng)求回調(diào)
func onReGeocodeSearchDone(request: AMapReGeocodeSearchRequest, response: AMapReGeocodeSearchResponse) {
print("request :\(request)")
print("response :\(response)")
if (response.regeocode != nil) {
//經(jīng)緯度
let coordinate = CLLocationCoordinate2DMake(Double(request.location.latitude), Double(request.location.longitude))
print(coordinate)
//城市
print(response.regeocode.addressComponent.city)
//具體位置
print(response.regeocode.formattedAddress)
}
}
在指定的經(jīng)緯度添加大頭針
//將大頭針放到地圖中去
func animated(coordinate: CLLocationCoordinate2D) {
//移除前面的大頭針
mapView.removeAnnotations(pointAnnotations)
//添加大頭針
let pointAnnotation = MAPointAnnotation()
//給大頭針添加坐標(biāo)
pointAnnotation.coordinate = coordinate
//將大頭針添加到地圖
mapView.addAnnotation(pointAnnotation)
pointAnnotations.append(pointAnnotation)
}
//添加大頭針函數(shù)
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")
}
//設(shè)置氣泡可以彈出, 默認(rèn)為NO
annotationView!.canShowCallout = true
//設(shè)置標(biāo)注動(dòng)畫顯示, 默認(rèn)為NO
annotationView!.animatesDrop = true
//設(shè)置標(biāo)柱可以拖動(dòng), 默認(rèn)為NO
annotationView?.draggable = true
//大頭針顏色
annotationView?.pinColor = MAPinAnnotationColor.Red
return annotationView
}
return nil
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者