2016-12-13 2 views
0

Wie zentrieren Sie die Google-Karte, wenn sich der Benutzer bewegt, wie beim Fahren? Der Code, den ich habe, zentriert den Benutzer nicht, und der Benutzer geht einfach in Richtung Grenze und Disappear. Wie halten Sie den Benutzer ständig in der Mitte?Wie zentrieren Sie die Karte, wenn der Benutzer den Standort ändert

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

Sind Sie sicher, dass die 'sonst 'Anweisung wird ausgelöst? Es sollte funktionieren. – Ryan

+0

Ich bin mir ziemlich sicher, sonst wird Anweisung ausgelöst. – user30646

Antwort

1

Es gibt einige andere Möglichkeiten, die Sie versuchen können.

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

oder

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

oder

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

In GMSCameraPosition ist kein Kameramember vorhanden. Ich benutze swift 2.3, wenn das wichtig ist. – user30646

+0

Was passiert, wenn Sie die set-Methode im asynchronen Hauptthread platzieren? – Ryan

+0

Ich bin mir nicht sicher, ob das funktionieren wird. Aber ich habe eine Lösung gefunden, die funktioniert "mapView.animateToLocation (CLLocationCoordinate2D (latitude: location.coordinate.latitude, longitude: location.coordinate.longitude))" Danke für Ihre Hilfe. – user30646

0

Wenn jemand für eine rasche 2,3 Lösung wie mich suchen, hier ist die Lösung:

mapView.animateToLocation(CLLocationCoordinate2D(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude)) 
Verwandte Themen