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")
}
}
你有沒有實現'MapView類(MapView類:的MKMapView,rendererForOverlay覆蓋:MKOverlay)'?此外,你還沒有提供'座標'的經度或緯度 – Paulw11
是的我已經實現了mapView(mapView:MKMapView,rendererForOverlay覆蓋:MKOverlay),但註釋顯示當前所以我應該如何爲座標提供一個緯度和經度? –
您已在'tripspotRegion'數組中定義了三個座標。也許你可以用其中一個作爲中心?否則你的地區將在0,0 – Paulw11