问题
I am trying to move the camera in Google Maps to specified location (lat & long).
Here's my code/function, this code is located in ViewController1 (which contains the view for mapView):
func select_structure(lat: String, lon: String){
let camera = GMSCameraPosition.cameraWithLatitude(Double(lat)!, longitude: Double(lon)!, zoom: 6.0)
mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
mapView.mapType = kGMSTypeSatellite
mapView.camera = camera
view = mapView
populate_structures2()
let newcamera = GMSCameraPosition.cameraWithLatitude(Double(lat)!, longitude: Double(lon)!, zoom: 17.0)
self.mapView.animateToCameraPosition(newcamera)
print(mapView)
}
When I call that code in ViewController2, when the app is run for the first time, it doesn't work. When I use the function nothing happens. But I know that it is being read. And when I run it for the second, third and so on, it works!
Just a quick note. ViewController2 is a sliding menu. And I am calling the function from ViewController1 thru this:
var ClassViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
ClassViewController.select_structure(structures_lat[indexPath.item], lon: structures_lon[indexPath.item])
I initialize the mapView in ViewController1's loadView:
override func loadView() {
let camera = GMSCameraPosition.cameraWithLatitude(35.98917593, longitude: -95.94740106, zoom: 6.0)
mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
mapView.myLocationEnabled = true
mapView.mapType = kGMSTypeSatellite
//mapView.delegate = self
mapView.settings.allowScrollGesturesDuringRotateOrZoom = false
view = mapView
load_lines()
load_structures()
}
Any idea?
来源:https://stackoverflow.com/questions/39450670/moving-camera-in-google-maps