Moving Camera in Google Maps

妖精的绣舞 提交于 2019-12-12 02:44:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!