2016-12-13 1 views
0

運転中のように、ユーザーが移動しているときにGoogleマップをどのように調整しますか?私が持っているコードはユーザーを中心にしておらず、ユーザーは単に境界とディサッパーに向かっています。どのようにしてユーザーを常に中央に置いていますか?ユーザーが場所を変更したときに地図の中心をどのように調整するのですか

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

    if let location = manager.location { 

     if (firstLoad) { 
      firstLoad = false 
      recentCoordinate = location.coordinate 
      mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: Constants.MapView.Zoom, bearing: 0, viewingAngle: Constants.MapView.ViewingAngle) 
     } 
     else { 
      // This line of code suppose to center the user as user moves along while keeping the zoom and angle state user has chosen 
      mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: mapView.camera.zoom, bearing: 0, viewingAngle: mapView.camera.viewingAngle) 


     } 

     // Filter out noise. Currently not working 
     let age = 0 - location.timestamp.timeIntervalSinceNow 
     if (age > 120) { 
      return; // ignore old (cached) updates 
     } 
     if (location.horizontalAccuracy < 0) { 
      return; // ignore invalid updates 
     } 

     if (location.horizontalAccuracy <= 10){ 
      // this is a valid update 
      // Draw route as user moves along 
      path.addCoordinate(CLLocationCoordinate2D(latitude: location.coordinate.latitude, 
       longitude: location.coordinate.longitude)) 
      let polyline = GMSPolyline(path: path) 
      polyline.strokeColor = UIColor.redColor() 
      polyline.strokeWidth = 3 
      polyline.map = mapView 

      // Save the coordinates to array 
      coordinateArray.append([location.coordinate.latitude, location.coordinate.longitude]) 



     } 
    } 
+1

あなたは '他によろしいです'文がトリガされますか?それは動作するはずです。 – Ryan

+0

私はかなりelse文がトリガーされていると確信しています。 – user30646

答えて

1

他にもいくつかの方法があります。

let update = GMSCameraUpdate.setTarget(location.coordinate) 
mapView.moveCamera(update) 

または

mapView.camera = GMSCameraPosition.camera(target: location.coordinate, zoom: 15.0) 

または

let update = GMSCameraUpdate.setTarget(location.coordinate) 
     mapView.animate(with: update) 
+0

GMSCameraPositionにカメラのメンバーがありません。私はそれが問題であれば迅速な2.3を使用しています。 – user30646

+0

setメソッドをメインの非同期スレッドに入れるとどうなりますか? – Ryan

+0

それはうまくいくかどうか分かりません。しかし、私は "mapView.animateToLocation(CLLocationCoordinate2D(latitude:location.coordinate.latitude、longitude:location.coordinate.longitude))"という作品を見つけました。 – user30646

0

誰もが私のようなSWIFT 2.3解決策を探している場合、ここでの解決策は次のとおりです。

mapView.animateToLocation(CLLocationCoordinate2D(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude)) 
関連する問題