利用swift和百度地图写一个定位返回位置的工具

importUIKit

classlocationTool:NSObject,BMKGeoCodeSearchDelegate,BMKLocationServiceDelegate{

classfuncsharelocationInstance() ->locationTool{

struct Singleton {

staticvarpredicate:dispatch_once_t=0

staticvarinstance:locationTool? =nil

}

dispatch_once(&Singleton.predicate, {

Singleton.instance =locationTool()

})

returnSingleton.instance!

}

varlocationService =BMKLocationService().then{

$0.allowsBackgroundLocationUpdates=true

//设定定位精度

$0.desiredAccuracy=kCLLocationAccuracyBest

}

vargeocodeSearch =BMKGeoCodeSearch()

funcsetUpDelegate() {

locationService.delegate=self

geocodeSearch.delegate=self

locationService.startUserLocationService()

}

deinit{

locationService.delegate=nil

geocodeSearch.delegate=nil

}

}

extensionlocationTool{

funcreverseGeoSearch(lat:CLLocationDegrees, lon:CLLocationDegrees){

letreverseGeocodeSearchOption =BMKReverseGeoCodeOption()

reverseGeocodeSearchOption.reverseGeoPoint=CLLocationCoordinate2DMake(lat, lon)

NSLog("%f,%f", lat,lon)

letflag =geocodeSearch.reverseGeoCode(reverseGeocodeSearchOption)

ifflag {

debugPrint("反geo检索发送成功")

}else{

debugPrint("反geo检索发送失败")

locationService.startUserLocationService()

}

}

/**

*返回反地理编码搜索结果

*@param searcher搜索对象

*@param result搜索结果

*@param error错误号,@see BMKSearchErrorCode

*/

funconGetReverseGeoCodeResult(searcher:BMKGeoCodeSearch!, result:BMKReverseGeoCodeResult!, errorCode error:BMKSearchErrorCode) {

debugPrint("onGetReverseGeoCodeResult error:\(error)")

iferror ==BMK_SEARCH_NO_ERROR{

letitem =BMKPointAnnotation()

item.coordinate= result.location

item.title= result.address

//            postLocationGetCode(result.address)

cityAddress= result.address

letstring ="&province=\(result.addressDetail!.province)"+"&city=\(result.addressDetail!.city)"+"&area=\(result.addressDetail!.district)"

debugPrint(string)

HLNoteCenter.postNotificationName(HLNotificationAddressnotification, object: string)

locationService.stopUserLocationService()

}

}

funcwillStartLocatingUser() {

//        print("willStartLocatingUser")

debugPrint("willStartLocatingUser")

}

/**

*用户方向更新后,会调用此函数

*@param userLocation新的用户位置

*/

funcdidUpdateUserHeading(userLocation:BMKUserLocation!) {

debugPrint("heading is\(userLocation.heading)")

//        mapView!.updateLocationData(userLocation)

}

/**

*用户位置更新后,会调用此函数

*@param userLocation新的用户位置

*/

funcdidUpdateBMKUserLocation(userLocation:BMKUserLocation!) {

reverseGeoSearch(userLocation.location.coordinate.latitude, lon: userLocation.location.coordinate.longitude)

}

/**

*在地图View停止定位后,会调用此函数

*@param mapView地图View

*/

funcdidStopLocatingUser() {

debugPrint("didStopLocatingUser")

}

/**

*在地图View停止失败后,会调用此函数

*@param mapView地图View

*/

funcdidFailToLocateUserWithError(error:NSError!) {

locationService.startUserLocationService()

}

}

你可能感兴趣的:(利用swift和百度地图写一个定位返回位置的工具)