2016-05-13 82 views
0

我試圖用CLRegion實現地理定位功能。但是當我運行這個應用程序MKCircle沒有出現,誰能告訴我哪個部分是錯的?在Swift中構建地理定位功能

func setupData(){ 
    if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self){ 

    let tripspotRegion = [ 
     tripSpot(title: "一中商圈", coordinate: CLLocationCoordinate2DMake(24.149062, 120.684891), regionRadius: 300.0, location: "臺中一中", type: "food"), 
     tripSpot(title: "逢甲夜市", coordinate: CLLocationCoordinate2DMake(24.180407, 120.645086), regionRadius: 300.0, location:"臺中逢甲", type: "food"), 
     tripSpot(title: "東海商圈", coordinate: CLLocationCoordinate2DMake(24.181143, 120.593158), regionRadius: 300.0, location: "東海商圈", type: "food")] 
    //set annotation 
    let coordinate = CLLocationCoordinate2D() 
    let regionRadius = 300.0 
    let title = "title" 
    let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude,longitude: coordinate.longitude), radius: regionRadius, identifier: title) 
    //set annotation 
    let tripSpotAnnotation = MKPointAnnotation() 

    tripSpotAnnotation.coordinate = coordinate 
    tripSpotAnnotation.title = "\(title)" 
    mapView.addAnnotations(tripspotRegion) 
    locationManager.startMonitoringForRegion(region) 
    // draw a circle 
     let circle = MKCircle(centerCoordinate: coordinate, radius: regionRadius) 

     mapView.addOverlay(circle) 
    } 
    // check if can monitor region 
     else{ 

      print("system can't track regions") 



      } 
} 
+0

你有沒有實現'MapView類(MapView類:的MKMapView,rendererForOverlay覆蓋:MKOverlay)'?此外,你還沒有提供'座標'的經度或緯度 – Paulw11

+0

是的我已經實現了mapView(mapView:MKMapView,rendererForOverlay覆蓋:MKOverlay),但註釋顯示當前所以我應該如何爲座標提供一個緯度和經度? –

+0

您已在'tripspotRegion'數組中定義了三個座標。也許你可以用其中一個作爲中心?否則你的地區將在0,0 – Paulw11

回答

1

你已經把三個座標放在一個數組中,但你沒有對它們做任何事情。您正在創建一個新的coordinate,但不會初始化它,因此您的區域和覆蓋圖將位於(0,0)處。我想你的意思是使用tripspotArray添加區/覆蓋:

func setupData(){ 
    if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self) { 

     let tripspotRegion = [ 
      tripSpot(title: "一中商圈", coordinate: CLLocationCoordinate2DMake(24.149062, 120.684891), regionRadius: 300.0, location: "臺中一中", type: "food"), 
      tripSpot(title: "逢甲夜市", coordinate: CLLocationCoordinate2DMake(24.180407, 120.645086), regionRadius: 300.0, location:"臺中逢甲", type: "food"), 
      tripSpot(title: "東海商圈", coordinate: CLLocationCoordinate2DMake(24.181143, 120.593158), regionRadius: 300.0, location: "東海商圈", type: "food")] 

     for aSpot in tripspotRegion { 
      //set annotation 
      let coordinate =aSpot.coordindate 
      let regionRadius = aSpot.regionRadius 
      let title = aSpot.title 
      let region = CLCircularRegion(center: coordinate, radius: regionRadius, identifier: title) 
//set annotation 
      let tripSpotAnnotation = MKPointAnnotation() 

      tripSpotAnnotation.coordinate = coordinate 
      tripSpotAnnotation.title = title 
      mapView.addAnnotations([tripSpotAnnotation]) 
      locationManager.startMonitoringForRegion(region) 
// draw a circle 
      let circle = MKCircle(centerCoordinate: coordinate, radius: regionRadius) 
      mapView.addOverlay(circle) 
     } 
    } else{ 
     print("system can't track regions") 
    } 
} 
+0

哇!非常感謝你!這正是我想要的!更重要的是,我現在認識到它的主要理想。感謝! –