Google 地图 Mapview 未以标记为中心
Google Maps Mapview not centering on marker
我在 ios 中使用 google 地图和 swift 4,我必须在屏幕上显示带有标记的地图视图,问题是每当我显示mapview 我得到了这个结果:
我想要得到的结果是:
这是我正在使用的代码:
func loadMap(location:CLLocation){
self.mapView.isMyLocationEnabled = true
self.mapView.isUserInteractionEnabled = true
self.view.layoutIfNeeded()
self.addLocationMarker()
}
func addLocationMarker(){
// Creates a marker in the center of the map.
let marker = GMSMarker()
print(self.startLocation)
marker.position = CLLocationCoordinate2D(latitude: self.startLocation.coordinate.latitude, longitude: self.startLocation.coordinate.longitude)
marker.map = self.mapView
let camera = GMSCameraPosition.camera(withLatitude: self.startLocation.coordinate.latitude, longitude: self.startLocation.coordinate.longitude, zoom: 12.0)
self.mapView.camera = camera
}
并且 mapview 是故事板中视图的出口
这段代码对我来说很好用。试试这个 (Swift 4)
func setupMapView() {
DispatchQueue.main.async {
let position = CLLocationCoordinate2D(latitude: 32.9483364, longitude: -96.8241543)
let marker = GMSMarker(position: position)
self.mapView.camera = GMSCameraPosition(target: position, zoom: 15, bearing: 0, viewingAngle: 0)
marker.title = "Enter marker title here"
marker.map = self.mapView
}
}
注意:我使用了示例经度和纬度。你应该插入你的。
我在 ios 中使用 google 地图和 swift 4,我必须在屏幕上显示带有标记的地图视图,问题是每当我显示mapview 我得到了这个结果:
我想要得到的结果是:
这是我正在使用的代码:
func loadMap(location:CLLocation){
self.mapView.isMyLocationEnabled = true
self.mapView.isUserInteractionEnabled = true
self.view.layoutIfNeeded()
self.addLocationMarker()
}
func addLocationMarker(){
// Creates a marker in the center of the map.
let marker = GMSMarker()
print(self.startLocation)
marker.position = CLLocationCoordinate2D(latitude: self.startLocation.coordinate.latitude, longitude: self.startLocation.coordinate.longitude)
marker.map = self.mapView
let camera = GMSCameraPosition.camera(withLatitude: self.startLocation.coordinate.latitude, longitude: self.startLocation.coordinate.longitude, zoom: 12.0)
self.mapView.camera = camera
}
并且 mapview 是故事板中视图的出口
这段代码对我来说很好用。试试这个 (Swift 4)
func setupMapView() {
DispatchQueue.main.async {
let position = CLLocationCoordinate2D(latitude: 32.9483364, longitude: -96.8241543)
let marker = GMSMarker(position: position)
self.mapView.camera = GMSCameraPosition(target: position, zoom: 15, bearing: 0, viewingAngle: 0)
marker.title = "Enter marker title here"
marker.map = self.mapView
}
}
注意:我使用了示例经度和纬度。你应该插入你的。