質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

2回答

1968閲覧

swift 現在位置の表示について

ives

総合スコア19

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

1クリップ

投稿2018/08/11 14:38

編集2018/08/15 14:30

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. } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2018/08/15 00:31

locationManager(_:didUpdateLocations:)が呼ばれないのでしょうか?位置情報は許可されてますか?
ives

2018/08/15 03:25

mac本体の位置情報オンです
fuzzball

2018/08/15 03:52

macじゃなくてiPhoneの方です。あと、最初の質問にも答えて下さい。
ives

2018/08/15 04:29

エラーはないので恐らく呼ばれてると思います。シミュレータで実装してるんですがもしかして実機でやって見ないと現在位置が表示されないんですか?
fuzzball

2018/08/15 05:35 編集

「恐らく」ではなくきちんと確認して下さい。(print文を入れるだけで確認できます)実機でもシミュレータでも位置取得は出来ます。許可については端末の「設定」の中で確認して下さい。
ives

2018/08/15 07:23

すみません。printに入れた"成功"がdebugに表示されませんでした。実機は使っていませんシミュレータだけでの実装ですが許可してあります
fuzzball

2018/08/16 00:07

追記したコード、インデントが潰れています。(見る人のことを考えて下さい)
guest

回答2

0

locationManager(_:didUpdateLocations:)

swift

1func locationManager(_ manager: CLLocationManager, didUpdatelocations locations: [CLLocation]) 2{ 3 () 4}

searchBarSearchButtonClicked(_:)の外に出して下さい。

L

didUpdatelocationsではなくdidUpdateLocationsです。

Info.plist

swift

1manager.requestWhenInUseAuthorization()

ならば、Info.plistに書くのはNSLocationWhenInUseUsageDescriptionです。

Privacy - Location When In Use Usage Description`

投稿2018/08/15 07:50

編集2018/08/16 00:42
fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ives

2018/08/15 14:29 編集

外に出しましたが表示されません。泣 念の為コードを書いておきます
fuzzball

2018/08/15 10:37

ここに書かずに質問内のコードを更新して下さい。(上のコメントのコードは削除して下さい)
fuzzball

2018/08/16 00:42

回答に2つ追記しました。
guest

0

シミュレータで位置設定はしていますか?

参考) https://qiita.com/mattak/items/420039c674fe4392a647

投稿2018/08/14 10:07

t_obara

総合スコア5488

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ives

2018/08/14 12:54

設定していなかったのでしてから立ち上げてみましたが変わらず表示されないままです
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問