2016-12-13 57 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

我很確定其他語句被觸發。 – 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)) 
相關問題