xsf0515 2015-05-05 00:41
浏览 1726

使用SWIFT开发地图应用 有个疑问求各位大神帮忙

    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按钮点击事件中调用会出错。
求解。。。。

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 易语言把MYSQL数据库中的数据添加至组合框
    • ¥20 求数据集和代码#有偿答复
    • ¥15 关于下拉菜单选项关联的问题
    • ¥20 java-OJ-健康体检
    • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
    • ¥15 使用phpstudy在云服务器上搭建个人网站
    • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
    • ¥15 vue3+express部署到nginx
    • ¥20 搭建pt1000三线制高精度测温电路
    • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况