1

Google Maps APIを使用するiPhoneアプリで作業しています。ユーザーの現在地を取得する際に問題が発生しています。ロケーション。CLLocationManager(Google Maps API)からユーザーの場所を取得する際の問題

私は数日を費やしてコンパイラエラーを取り除いていますが、それでもまだうまくいきません。マップは表示されますが、最初の座標でのみlongおよびlat変数が提供されます。私はそれがCLLoationManager()と関係があると思います。

シミュレータ上の位置を更新すると結果が得られません。新人ミスをしているような気がします。何もわからないだけです。そして提案?

import UIKit 
    import CoreLocation 
    import GoogleMaps 


class ViewController: UIViewController, CLLocationManagerDelegate { 

var long:Double = -0.13 
var lat:Double = 51.0 

let locationManager = CLLocationManager() 

override func loadView() { 
    // Create a GMSCameraPosition that tells the map to display the 

    //user location stuff 
    self.locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.requestAlwaysAuthorization() 
    locationManager.startUpdatingLocation() 


    let camera = GMSCameraPosition.camera(withLatitude: CLLocationDegrees(lat), longitude: CLLocationDegrees(long), zoom: 5.0) 
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) 

    view = mapView 
    mapView.showUserLocation = true 
    // Creates a marker in the center of the map. 
    let marker = GMSMarker() 
    marker.position = CLLocationCoordinate2D(latitude: 51.51 , longitude: -0.13) 
    marker.title = "Test" 
    marker.snippet = "This is a test" 
    marker.map = mapView 
} 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { //i think this is the problem area 
    let userLocation = locations.last! 
    long = userLocation.coordinate.longitude 
    lat = userLocation.coordinate.latitude 

    self.locationManager.stopUpdatingLocation() 

} 
} 

答えて

0

ユーザーの所在地を取得すると、地図の現在の場所は更新されません。あなたは次のようなことをする必要があります:

// property to store map view instead of just a local variable in loadView() 
var mapView: GMSMapView? 

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { //i think this is the problem area 
    let userLocation = locations.last! 
    long = userLocation.coordinate.longitude 
    lat = userLocation.coordinate.latitude 

    let newCamera = GMSCameraPosition.camera(withLatitude: lat, longitude: long) 
    mapView.camera = newCamera 
    self.locationManager.stopUpdatingLocation() 

} 
関連する問題