swift 下の追記です
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { UIApplication.shared.beginIgnoringInteractionEvents() let activityIndicator = UIActivityIndicatorView() activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray activityIndicator.center = self.view.center activityIndicator.hidesWhenStopped = true activityIndicator.startAnimating() self.view.addSubview(activityIndicator) //Hide the search bar searchBar.resignFirstResponder() dismiss(animated: true, completion: nil) //Create the request let searchRequest = MKLocalSearchRequest() searchRequest.naturalLanguageQuery = searchBar.text let activeSearch = MKLocalSearch(request: searchRequest) activeSearch.start{ (response, error) in if response == nil { print("error") } else { //remove annotation let annotations = self.mapView.annotations self.mapView.removeAnnotation(annotations as! MKAnnotation) //getting date let latitude = response?.boundingRegion.center.latitude let longitude = response?.boundingRegion.center.longitude //create annotation let annotation = MKPointAnnotation() annotation.title = searchBar.text annotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!) self.mapView.addAnnotation(annotation) //zooming in on annotation let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude!, longitude!) let span = MKCoordinateSpanMake(0.1, 0.1) let region = MKCoordinateRegionMake(coordinate, span) self.mapView.setRegion(region, animated: true) } } } func locationManager(_ manager: CLLocationManager, didUpdatelocations locations: [CLLocation]) { print("成功") let location = locations[0] let span:MKCoordinateSpan = MKCoordinateSpanMake(location.coordinate.latitude, location.coordinate.longitude) let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span) mapView.setRegion(region, animated: true) self.mapView.showsUserLocation = true } override func viewDidLoad() { super.viewDidLoad() mapView=MKMapView() manager.delegate = self manager.desiredAccuracy = kCLLocationAccuracyBest manager.requestWhenInUseAuthorization() manager.startUpdatingHeading() } ```### 前提・実現したいこと ユーザーの現在位置をマップ上に表示したい ### 発生している問題・エラーメッセージ
swiftで現在位置を表示したいのですがいざ以下のコードでシミュレータで実装してみても表示されません。コード上にエラー等はないので間違いがわかりません。
ちなみにBuild phrasesにCoreLocation.framework追加済み
info.plistにPrivacy – Location Always and When In Use Usage Description
Privacy – Location Always Usage Description
Privacy - Location Usage Descriptionの三つも追加済みです
Mac本体の位置情報設定もオンにしてあります
どうかお助け下さい。よろしくお願いします。
### 該当のソースコード ```swift import UIKit import MapKit import CoreLocation class FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UISearchBarDelegate { let manager = CLLocationManager() @IBOutlet weak var mapView: MKMapView! @IBAction func searchBar(_ sender: Any) { let searchController = UISearchController(searchResultsController: nil) searchController.searchBar.delegate = self present(searchController, animated: true, completion: nil) } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { UIApplication.shared.beginIgnoringInteractionEvents() let activityIndicator = UIActivityIndicatorView() activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray activityIndicator.center = self.view.center activityIndicator.hidesWhenStopped = true activityIndicator.startAnimating() self.view.addSubview(activityIndicator) //Hide the search bar searchBar.resignFirstResponder() dismiss(animated: true, completion: nil) //Create the request let searchRequest = MKLocalSearchRequest() searchRequest.naturalLanguageQuery = searchBar.text let activeSearch = MKLocalSearch(request: searchRequest) activeSearch.start{ (response, error) in if response == nil { print("error") } else { //remove annotation let annotations = self.mapView.annotations self.mapView.removeAnnotation(annotations as! MKAnnotation) //getting date let latitude = response?.boundingRegion.center.latitude let longitude = response?.boundingRegion.center.longitude //create annotation let annotation = MKPointAnnotation() annotation.title = searchBar.text annotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!) self.mapView.addAnnotation(annotation) //zooming in on annotation let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude!, longitude!) let span = MKCoordinateSpanMake(0.1, 0.1) let region = MKCoordinateRegionMake(coordinate, span) self.mapView.setRegion(region, animated: true) } } func locationManager(_ manager: CLLocationManager, didUpdatelocations locations: [CLLocation]) { let location = locations[0] let span:MKCoordinateSpan = MKCoordinateSpanMake(location.coordinate.latitude, location.coordinate.longitude) let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude) let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span) mapView.setRegion(region, animated: true) self.mapView.showsUserLocation = true } } override func viewDidLoad() { super.viewDidLoad() mapView=MKMapView() manager.delegate = self manager.desiredAccuracy = kCLLocationAccuracyBest manager.requestWhenInUseAuthorization() manager.startUpdatingHeading() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
locationManager(_:didUpdateLocations:)が呼ばれないのでしょうか?位置情報は許可されてますか?
mac本体の位置情報オンです
macじゃなくてiPhoneの方です。あと、最初の質問にも答えて下さい。
エラーはないので恐らく呼ばれてると思います。シミュレータで実装してるんですがもしかして実機でやって見ないと現在位置が表示されないんですか?
「恐らく」ではなくきちんと確認して下さい。(print文を入れるだけで確認できます)実機でもシミュレータでも位置取得は出来ます。許可については端末の「設定」の中で確認して下さい。
すみません。printに入れた"成功"がdebugに表示されませんでした。実機は使っていませんシミュレータだけでの実装ですが許可してあります
追記したコード、インデントが潰れています。(見る人のことを考えて下さい)