当允许在主视图上显示时,Google map 可以正常工作。
但是,当我使用新的 Sub 时, map 的位置是错误的,即使纬度和经度值发生变化, map 的位置似乎也没有改变。
代码片段:
import UIKit
import GoogleMaps
class MainVC: UIViewController {
@IBOutlet weak var mapSubView: GMSMapView!
override func viewDidLoad() {
let camera = GMSCameraPosition.cameraWithLatitude(13.009047, longitude: 77.652949, zoom: 6)
let mapView = GMSMapView.mapWithFrame(self.mapSubView.bounds, camera: camera)
mapView.myLocationEnabled = true
self.mapSubView = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949)
marker.title = "Bangalore"
marker.snippet = "India"
marker.map = mapView
}
}
最佳答案
将此行添加到您的代码中:-
mapSubView.camera = GMSCameraPosition(target: CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949), zoom: 15, bearing: 0, viewingAngle: 0)
GoogleMaps
camera 只是定义 map 的方向以及属性,例如你想要多少缩放,缩放的目标是什么等等。有关此功能的更多详细信息以及参数是什么在这个函数上代表 CMD+CLICK 去它的文档
编辑: 上面的代码添加了一个新的 GMSMapView,使用 View Controller 中的代码在已经存在的 GMSMapView 之上,该 GMSMapView 使用 IBOutlet 配置
更新代码:
@IBOutlet weak var mapSubView: GMSMapView!
override func viewDidLoad() {
mapSubView.myLocationEnabled = true
mapSubView.camera = GMSCameraPosition(target: CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949), zoom: 15, bearing: 0, viewingAngle: 0)
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949)
marker.title = "Bangalore"
marker.snippet = "India"
marker.map = mapSubView
}
Google map iOS SDK 文档:GMSCameraPosition Class Reference
Google map iOS SDK 文档: GMSMarker Class Reference
关于ios - 与输入相比,谷歌地图显示不同的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39270736/