import UIKit
let APIKey = "456502f40fcce736c5a32fbb397ff19c"
class MapViewController: UIViewController,UITableViewDelegate,AMapSearchDelegate,MAMapViewDelegate,UIGestureRecognizerDelegate{
var mapView: MAMapView?
var isRecording: Bool = false
var locationButton: UIButton?
var searchButton: UIButton?
var imageShare: UIImage?
var currentRoute: Route?
var tipView: TipView?
var statusView: StatusView?
var search: AMapSearchAPI?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.edgesForExtendedLayout = UIRectEdge.None
initToolBar()
initMapView()
// initTipView()
}
override func viewDidAppear(animated: Bool) {
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
}
//MARK:- Initialization
func initMapView() {
MAMapServices.sharedServices().apiKey=APIKey
mapView = MAMapView(frame: self.view.bounds)
mapView!.delegate = self
self.view.addSubview(mapView!)
self.view.sendSubviewToBack(mapView!)
mapView!.showsUserLocation = true
mapView!.userTrackingMode = MAUserTrackingMode.Follow
mapView!.distanceFilter = 10.0
mapView!.desiredAccuracy = kCLLocationAccuracyBestForNavigation
mapView!.setZoomLevel(15.1, animated: true)
}
func initToolBar() {
let rightButtonItem: UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_list.png"), style: UIBarButtonItemStyle.Bordered, target: self, action: "actionHistory")
navigationItem.rightBarButtonItem = rightButtonItem
let leftButtonItem: UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_play.png"), style: UIBarButtonItemStyle.Bordered, target: self, action: "actionRecordAndStop")
navigationItem.leftBarButtonItem = leftButtonItem
imageShare = UIImage(named: "location_share@2x.png")
locationButton = UIButton(frame: CGRectMake(CGRectGetWidth(view.bounds) - 80, CGRectGetHeight(view.bounds) - 120, 60, 60))
locationButton!.autoresizingMask = UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleTopMargin
locationButton!.backgroundColor = UIColor.whiteColor()
locationButton!.layer.cornerRadius = 5
locationButton!.layer.shadowColor = UIColor.blackColor().CGColor
locationButton!.layer.shadowOffset = CGSizeMake(5, 5)
locationButton!.layer.shadowRadius = 5
//locationButton!.tag=mapView!.userLocation.coordinate
locationButton!.addTarget(self, action: "addPoint:", forControlEvents: UIControlEvents.TouchUpInside)
locationButton!.setImage(imageShare, forState: UIControlState.Normal)
view.addSubview(locationButton!)
}
func initTipView() {
tipView = TipView(frame: CGRectMake(0, 0, CGRectGetWidth(view.bounds), 30))
view.addSubview(tipView!)
statusView = StatusView(frame: CGRectMake(5, 35, 150, 150))
statusView!.showStatusInfo(nil)
view.addSubview(statusView!)
}
//MARK:- Actions
func stopLocationIfNeeded() {
if !isRecording {
println("stop location")
mapView!.setUserTrackingMode(MAUserTrackingMode.None, animated: false)
mapView!.showsUserLocation = false
}
}
func actionHistory() {
println("actionHistory")
let historyController = RecordViewController(nibName: nil, bundle: nil)
historyController.title = "Records"
navigationController!.pushViewController(historyController, animated: true)
}
func actionRecordAndStop() {
println("actionRecord")
isRecording = !isRecording
if isRecording {
showTip("Start recording...")
navigationItem.leftBarButtonItem!.image = UIImage(named: "icon_stop.png")
if currentRoute == nil {
currentRoute = Route()
}
addLocation(mapView!.userLocation.location)
}
else {
navigationItem.leftBarButtonItem!.image = UIImage(named: "icon_play.png")
addLocation(mapView!.userLocation.location)
hideTip()
saveRoute()
}
}
func addPoint(sender: UIButton) {
searchReGeocodeWithCoordinate( mapView!.userLocation.coordinate)
}
func actionSearch(sender: UIButton) {
let searchDemoController = SearchViewController(nibName: nil, bundle: nil)
navigationController!.pushViewController(searchDemoController, animated: true)
}
//MARK:- Helpers
func addLocation(location: CLLocation?) {
let success = currentRoute!.addLocation(location)
if success {
showTip("locations: \(currentRoute!.locations.count)")
}
}
func saveRoute() {
if currentRoute == nil {
return
}
let name = currentRoute!.title()
let path = FileHelper.recordPathWithName(name)
NSKeyedArchiver.archiveRootObject(currentRoute!, toFile: path!)
currentRoute = nil
}
func showTip(tip: String?) {
tipView!.showTip(tip)
}
func hideTip() {
tipView!.hidden = true
}
//MARK:- MAMapViewDelegate
func mapView(mapView: MAMapView , didUpdateUserLocation userLocation: MAUserLocation ) {
}
/**
- (void)mapView:(MAMapView *)mapView didChangeUserTrackingMode:(MAUserTrackingMode)mode animated:(BOOL)animated;
*/
func mapView(mapView: MAMapView, didChangeUserTrackingMode mode: MAUserTrackingMode, animated: Bool) {
locationButton?.setImage(imageShare, forState: UIControlState.Normal)
}
func searchReGeocodeWithCoordinate(coordinate: CLLocationCoordinate2D!) {
let regeo: AMapReGeocodeSearchRequest = AMapReGeocodeSearchRequest()
regeo.location = AMapGeoPoint.locationWithLatitude(CGFloat(coordinate.latitude), longitude: CGFloat(coordinate.longitude))
//println("regeo :\(regeo)")
self.search!.AMapReGoecodeSearch(regeo)
}
//MARK:- AMapSearchDelegate
func searchRequest(request: AnyObject!, didFailWithError error: NSError!) {
println("request :\(request), error: \(error)")
}
// - (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
func onReGeocodeSearchDone(request: AMapReGeocodeSearchRequest, response: AMapReGeocodeSearchResponse) {
println("request :\(request)")
println("response :\(response)")
}
}
不是已经定义好了mapView么,而且下面也初始化了。为什么在我addPoint按钮点击事件中调用会出错。
求解。。。。