当Google地图允许在主视图上显示时,它可以正常运行。
但是,当我使用新的Sub时,地图的位置是错误的,即使纬度和经度值发生变化,也不会改变。
代码段:
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
}
}
答案 0 :(得分:1)
将此行添加到您的代码中: -
mapSubView.camera = GMSCameraPosition(target: CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949), zoom: 15, bearing: 0, viewingAngle: 0)
GoogleMaps
相机只是定义了地图的方向以及诸如你想要多少缩放等属性,是什么缩放目标等等。有关此功能的更多细节以及参数代表CMD +的内容点击此功能转到其文档
修改强> 上面的代码添加了一个新的 GMSMapView ,使用 IBOutlet配置的现有 GMSMapView 上方的 View Controller 中的代码强>
更新代码:
@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 Maps iOS SDK文档:GMSCameraPosition Class Reference
Google Maps iOS SDK文档: GMSMarker Class Reference
答案 1 :(得分:0)
罪魁祸首是:
marker.map = mapView
将其替换为:
marker.map = self.mapSubView
此外,无需创建 mapView 。
这是完整的代码,这应该有效:
let camera = GMSCameraPosition.cameraWithLatitude(13.009047, longitude: 77.652949, zoom: 6)
self.mapSubView.camera = camera
self.mapSubView.myLocationEnabled = true
marker.position = CLLocationCoordinate2D(latitude: 13.009047, longitude: 77.652949)
marker.title = "Bangalore"
marker.snippet = "India"
marker.map = self.mapSubView
重要更改:
self.mapSubView.camera = camera
marker.map = self.mapSubView