SWIFT3中的Google Maps API全屏

时间:2017-02-22 14:14:26

标签: ios xcode api google-maps swift3

这是当前的代码,在进行修改后,它不再在澳大利亚悉尼地图上显示标记

class LocateVC:UIViewController {

@IBOutlet weak var mapView: GMSMapView!

override func viewDidLoad() {
    super.viewDidLoad()
    //override func loadView() {
    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    let camera = GMSCameraPosition.camera(withLatitude: -33.86  , longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: self.mapView.bounds, camera: camera)
    self.mapView = mapView

    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView
  }

}

输出

这是加载的屏幕 This is the screen that loads

悉尼澳大利亚没有标记 And no marker on Sydney Australia

2 个答案:

答案 0 :(得分:0)

您正在将mapview设为view的主ViewController

所以改变这两行

let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView

。通过

let mapView = GMSMapView.map(withFrame: self.mapView.bounds, camera: camera)
self.mapView = mapView

答案 1 :(得分:0)

@Nirav的答案几乎就在那里

这里我做了什么:

import UIKit
import GoogleMaps

class AddressMapController: UIViewController {

    @IBOutlet weak var map123: GMSMapView! // this is linked to a UIView in storyboard

    var currentLocation: CLLocation! //this is passed from the main controller

    override func viewDidLoad() {
        super.viewDidLoad()

        let lat = currentLocation.coordinate.latitude
        let long = currentLocation.coordinate.longitude

        let camera = GMSCameraPosition.camera(withLatitude: lat  , longitude: long, zoom: 12)

        // Creates a marker in the center of the map.
        let currentLocation_Marker = GMSMarker()
        currentLocation_Marker.position = CLLocationCoordinate2D(latitude: lat, longitude: long)
        currentLocation_Marker.title = "My Current Location"
        currentLocation_Marker.snippet = "I am here now"
        currentLocation_Marker.map = map123

        self.map123.camera = camera
    }
}

希望它能在未来帮助某人