2017-12-11 12 views
0

に完全に示されていない私は、GMSMapViewを持っており、GMSMapView上の現在位置を表示したいです。私はのInfo.plistソースコードに怒鳴るの行を追加した -現在の場所が迅速3

<key>NSLocationAlwaysUsageDescription</key> 
<string>Result</string> 
<key>NSLocationWhenInUseUsageDescription</key> 
<string>Result</string> 

私のシミュレートされた場所は、CA、Xcodeのから米国サンフランシスコです。 Simulated location

私はGMSMapViewをロードすると、位置アラートを取得します。 Location Alert

しかし、現在の場所は、常に今自分の携帯電話の場所はダッカ、バングラデシュを示すべきであるにもかかわらず、常にサンフランシスコで私のiPhoneの現在位置の変化にサンフランシスコとすべてのマップ関連のアプリを示します。 解決策は何ですか?
私は自分の携帯電話のGPSから私の現在の位置を知りたいです。あなたのviewDidLoadで

+0

これをチェックしてくださいhttps://stackoverflow.com/質問/ 25449469/swift-show-current-location-and-update-mkmapview – iOS

+0

あなたはシミュレータまたは実際のデバイスでテストしていますか?もしあなたがサンフランシスコのように位置を追加したように見える場合、場所はサンフランシスコを指しています。実際のデバイスでそれをテストしてください。 –

+1

gpxファイルを追加してみてください@Ahnaf –

答えて

0

()場所・マネージャーの構成については、以下を追加します。

override func viewDidLoad() { 
    super.viewDidLoad() 

    if (CLLocationManager.locationServicesEnabled()) 
    { 
     locationManager = CLLocationManager() 
     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.requestAlwaysAuthorization() 
     locationManager.startUpdatingLocation() 
    } 
} 

さらにGMSMapViewのためのデリゲートを実装する:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
    let location = locations.last as CLLocation 

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)   
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) 

    self.map.setRegion(region, animated: true) 
} 
+0

いつもサンフランシスコの緯度と経度を仲間にしました –

+0

シミュレータのために?私はapkを使用してアプリケーションをインストールする場合、それは完璧なのだろうか? –

2
Step 1: Import the this into your controller class 

    import CoreLocation 

Step 2: Add this delegate to your class file 

    class Your_controller: CLLocationManagerDelegate 

Step 3: Declare this above for viewed load 

    var locationManager = CLLocationManager() 

Step 4: Add this code to your `viewdidload` method 

    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.requestAlwaysAuthorization() 
    locationManager.startUpdatingLocation() 


    if CLLocationManager.locationServicesEnabled() { 
     switch (CLLocationManager.authorizationStatus()) { 
     case .notDetermined, .restricted, .denied: 
      print("No access") 
     case .authorizedAlways, .authorizedWhenInUse: 
      print("Access") 
     } 
    } else { 
     print("Location services are not enabled") 
    } 

Step 5: Add this code below viewdidload method 

    func locationManager(_ manager: CLLocationManager, 
     didUpdateLocations locations: [CLLocation]) { 

     let locationArray = locations as NSArray 
     let locationObj = locationArray.lastObject as !CLLocation 
     let coord = locationObj.coordinate 

     lattitude = coord.latitude 
     longitude = coord.longitude 
     print(lattitude) 
     print(longitude) 
    } 

Step 6: add to permission your plist file 

Key - "Privacy - Location When In Use Usage Description" 

Value - "This app needs access to your location" 

Step 7: run your Application on Hardware Device 
+0

それは本当に仕事です.... –

+0

ありがとう.......... –