我有一个国家/地区列表(latitude-longitude),并在我的地图中为列表中的每个国家/地区设置一个标记。我使用Google Maps SDK。
当我仅在当前位置尝试此操作时,将显示标记。在列表中的其他元素中不会出现标记。
发布于 2018-12-27 10:08:55
每个GMSMarker都有一个地图属性,即需要在其中显示的地图。
下面是一个有用的类:
class MyCustomMap: GMSMapView {
var markers: [Marker] = [] // array of custom marker to be shown in the map
var defaultZoom: Float = 10 //default zoom level of the map
override func awakeFromNib() {
super.awakeFromNib()
settings.rotateGestures = true
isMyLocationEnabled = true
settings.setAllGesturesEnabled(true)
}
func removeAllMarkers() {
for eachMarker in markers {
eachMarker.map = nil
}
}
/// Sets the zoom level of the map to show all the marker at once with in the screen
func showAllMarkers(for markers: [Marker]) {
var bounds = GMSCoordinateBounds()
for marker in markers {
bounds = bounds.includingCoordinate(marker.position)
}
let update = GMSCameraUpdate.fit(bounds, withPadding: 100.0)
animate(with: update)
}
// MARK: - Private
func createMarker(with marker: Marker) {
markers.append(marker)
marker.map = self
}
func createMarkers(with markers: [Marker]) {
for eachMarker in markers {
createMarker(with: eachMarker)
}
}
}这里的标记是我的自定义标记,它继承了GMSMarker
class Marker : GMSMarker {
}此外,如果您试图在获取标记后在当前位置显示标记,请使用CLLocationCoordinate2D经度和经度制作新标记,并将其添加到地图中。
marker.map = your_mapviewhttps://stackoverflow.com/questions/53938948
复制相似问题