你如何以用户更改位置为中心的地图
问题描述:
当用户移动时,如何驾驭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])
}
}
答
还有其他一些方法可以尝试。
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)
答
如果有人找像我这样的SWIFT 2.3解决方案,这里是解决方案:
mapView.animateToLocation(CLLocationCoordinate2D(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude))
你确定了'其他'语句被触发?它应该工作。 – Ryan
我很确定其他语句被触发。 – user30646