2014-11-11 11 views
0

사용자의 위치를 ​​추적하고지도에서 추적 할 수있는 앱을 작성 중입니다. 사용자가 낳는 경로를 추적하기 시작하는 단추 하나, 사용자가 눕힌 경로를 추적하는 것을 중지하는 위치, 마지막으로 추적 된 선의지도를 지우는 단추 등 세 가지 단추가 있습니다. 취소 버튼을 설정하는 데 문제가 있습니다. 누군가가 명확한 경로 버튼을 위해 @IBAction을 설정하도록 도와 줄 수 있습니까?Xcode 스위프트 MKPolyline 제거

import UIKit 
import CoreLocation 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 

    @IBOutlet weak var theMap: MKMapView! 
    @IBOutlet weak var startStopTracking: UISegmentedControl! 
    @IBOutlet weak var clearPath: UIButton! 
    @IBOutlet weak var label: UILabel! 

    var manager:CLLocationManager! 
    var myLocations: [CLLocation] = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //Setup our Location Manager 
     manager = CLLocationManager() 
     manager.delegate = self 
     manager.desiredAccuracy = kCLLocationAccuracyBest 
     manager.requestAlwaysAuthorization() 
     //manager.startUpdatingLocation() 

     //Setup our Map View 
     theMap.delegate = self 
     theMap.mapType = MKMapType.Hybrid 
     theMap.showsUserLocation = true 

     //Setup startStopTracking button? 

     startStopTracking.selectedSegmentIndex = -1 
    } 

    @IBAction func indexChanged(sender: UISegmentedControl) { 
      switch startStopTracking.selectedSegmentIndex 
      { 
      case 0: 
       manager.startUpdatingLocation() 
      case 1: 
       manager.stopUpdatingLocation() 
      default: 
       break; 
      } 
    } 

    @IBAction func clearPath(sender: UIButton) { 
     //label.text = String(myLocations.count) 
     //stopTracking() 
     //theMap.removeOverlay(overlay: MKOverlay) -> Void 
    } 

    func stopTracking() { 
     manager.stopUpdatingLocation() 
     myLocations = [] 
    } 

    func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) { 
     //theLabel.text = "\(locations[0])" 
     myLocations.append(locations[0] as CLLocation) 

     let spanX = 0.007 
     let spanY = 0.007 
     var newRegion = MKCoordinateRegion(center: theMap.userLocation.coordinate, span: MKCoordinateSpanMake(spanX, spanY)) 
     theMap.setRegion(newRegion, animated: true) 

     if (myLocations.count > 1){ 
      var sourceIndex = myLocations.count - 1 
      var destinationIndex = myLocations.count - 2 

      let c1 = myLocations[sourceIndex].coordinate 
      let c2 = myLocations[destinationIndex].coordinate 
      var a = [c1, c2] 
      var polyline = MKPolyline(coordinates: &a, count: a.count) 
      theMap.addOverlay(polyline) 
     } 
    } 


    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { 

     if overlay is MKPolyline { 
      var polylineRenderer = MKPolylineRenderer(overlay: overlay) 
      polylineRenderer.strokeColor = UIColor.blueColor() 
      polylineRenderer.lineWidth = 6 
      return polylineRenderer 
     } 
     return nil 
    } 
} 
+0

; 뭐가 문제 야? – matt

답변

1

당신이 당신의 클리어 패스 방법에에 액세스 할 수 있도록 당신은 당신의 polyline 클래스 속성을해야 :

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 
    ... 
    var polyline: MKPolyline = MKPolyline() 


    @IBAction func clearPath(sender: UIButton) { 
     //label.text = String(myLocations.count) 
     //stopTracking() 
     theMap.removeOverlay(polyline) 
    } 

} 
당신은 removeOverlay``와 바른 길에 있었다
+0

반드시 그렇지는 않습니다. 지도보기의 '오버레이'를 통해 찾을 수 있습니다. 아마도 이것이 유일한 오버레이 일 수 있습니다.이 경우 오버레이는 찾기가 쉽습니다. – matt

+0

@matt : 맞습니다. 더 간단하고 명확한 솔루션입니다. – zisoft

+0

위 코드의 솔루션과 다른 점은 무엇입니까? @matt – Chris

관련 문제