SWIFT3 中的 Google Maps API 全屏

问题描述 投票:0回答:2

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

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

ios xcode google-maps swift3
2个回答
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

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
    }
}

希望它能帮助将来的人

© www.soinside.com 2019 - 2024. All rights reserved.