在 Google 地图中放置多个标记。 Swift
Put multiple markers in Google maps. Swift
我在数组中有纬度和经度的数量。我想在 google 地图中显示多个标记。我对此有疑问。这是我的代码。
@IBOutlet weak var viewMap: UIView!
let locationManager = CLLocationManager()
var mapView = GMSMapView()
var cameraPosition = GMSCameraPosition()
fileprivate func loadData() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
showCurrentLocationOnMap()
}
func showCurrentLocationOnMap(){
mapView.settings.myLocationButton = true
mapView.isMyLocationEnabled = true
for data in nearByPlacesArray!{
let camera = GMSCameraPosition.camera(withLatitude: (data.latitude)!, longitude: (data.longitude)!, zoom: 14.0)
mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: self.viewMap.frame.size.width, height: self.viewMap.frame.size.height), camera: camera)
let location = CLLocationCoordinate2D(latitude: data.latitude!, longitude: data.longitude!)
print("location: \(location)")
let marker = GMSMarker()
marker.position = location
marker.snippet = data.name!
marker.map = mapView
}
self.viewMap.addSubview(mapView)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locationManager.stopUpdatingLocation()
}
我无法使用此代码在地图视图中显示多个标记。与数组的最后一个索引相关的地图中只显示一个标记。请有人帮助我。谢谢
您只需addSubview(mapView)
查看即可创建标记。
var mapView: GMSMapView!
func showCurrentLocationOnMap(){
mapView.settings.myLocationButton = true
mapView.isMyLocationEnabled = true
let camera = GMSCameraPosition.camera(withLatitude: 11.11, longitude: 12.12, zoom: 14.0) //Set default lat and long
mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: self.viewMap.frame.size.width, height: self.viewMap.frame.size.height), camera: camera)
self.viewMap.addSubview(mapView)
for data in nearByPlacesArray!{
let location = CLLocationCoordinate2D(latitude: data.latitude!, longitude: data.longitude!)
print("location: \(location)")
let marker = GMSMarker()
marker.position = location
marker.snippet = data.name!
marker.map = mapView
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locationManager.stopUpdatingLocation()
}
这是 1 个标记的代码,对吧?:
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
上面的代码显示了多个标记:
for marker in 1...5 {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.96, longitude: 151.20)
marker.title = "Jow Loove"
marker.snippet = "California"
marker.map = mapView
print ("Your GMSMarker")
print (marker)
}
在循环后打印一个简单的标记以查看效果!
但是您需要实施纬度、经度、标题并从某些数据/变量中截取。
是的,我花了很多时间寻找一种实现动态变量的方法...
var marker1 =
var marker2 =
var marker3 =
....
如果您现在如何使用 swift,请实施 post。
:-)
在swift 5、可以使用
for x in mylocationsArr {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(x.lat, x.lon)
marker.title = x.description
marker.map = self.googleMap
}
//you can fill nearByPlacesArray
nearByPlacesArray = ["lat:":"23.898988",Long:"72.898988"]
// you can add multiple lat and long in nearByPlacesArray this array.
for data in nearByPlacesArray{
let lat = Double(data.Latitude)
let long = Double(data.Longitude)
let location = CLLocationCoordinate2D(latitude:lat!, longitude:long!)
print("location1: \(location)")
let marker = GMSMarker()
marker.position = location
// marker.snippet = data.name
marker.map = mapView
marker.icon = GMSMarker.markerImage(with:.black)
marker.icon = UIImage(named: "Location_Icon")
}
我在数组中有纬度和经度的数量。我想在 google 地图中显示多个标记。我对此有疑问。这是我的代码。
@IBOutlet weak var viewMap: UIView!
let locationManager = CLLocationManager()
var mapView = GMSMapView()
var cameraPosition = GMSCameraPosition()
fileprivate func loadData() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
showCurrentLocationOnMap()
}
func showCurrentLocationOnMap(){
mapView.settings.myLocationButton = true
mapView.isMyLocationEnabled = true
for data in nearByPlacesArray!{
let camera = GMSCameraPosition.camera(withLatitude: (data.latitude)!, longitude: (data.longitude)!, zoom: 14.0)
mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: self.viewMap.frame.size.width, height: self.viewMap.frame.size.height), camera: camera)
let location = CLLocationCoordinate2D(latitude: data.latitude!, longitude: data.longitude!)
print("location: \(location)")
let marker = GMSMarker()
marker.position = location
marker.snippet = data.name!
marker.map = mapView
}
self.viewMap.addSubview(mapView)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locationManager.stopUpdatingLocation()
}
我无法使用此代码在地图视图中显示多个标记。与数组的最后一个索引相关的地图中只显示一个标记。请有人帮助我。谢谢
您只需addSubview(mapView)
查看即可创建标记。
var mapView: GMSMapView!
func showCurrentLocationOnMap(){
mapView.settings.myLocationButton = true
mapView.isMyLocationEnabled = true
let camera = GMSCameraPosition.camera(withLatitude: 11.11, longitude: 12.12, zoom: 14.0) //Set default lat and long
mapView = GMSMapView.map(withFrame: CGRect(x: 0, y: 0, width: self.viewMap.frame.size.width, height: self.viewMap.frame.size.height), camera: camera)
self.viewMap.addSubview(mapView)
for data in nearByPlacesArray!{
let location = CLLocationCoordinate2D(latitude: data.latitude!, longitude: data.longitude!)
print("location: \(location)")
let marker = GMSMarker()
marker.position = location
marker.snippet = data.name!
marker.map = mapView
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locationManager.stopUpdatingLocation()
}
这是 1 个标记的代码,对吧?:
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
上面的代码显示了多个标记:
for marker in 1...5 {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.96, longitude: 151.20)
marker.title = "Jow Loove"
marker.snippet = "California"
marker.map = mapView
print ("Your GMSMarker")
print (marker)
}
在循环后打印一个简单的标记以查看效果!
但是您需要实施纬度、经度、标题并从某些数据/变量中截取。
是的,我花了很多时间寻找一种实现动态变量的方法...
var marker1 =
var marker2 =
var marker3 =
....
如果您现在如何使用 swift,请实施 post。
:-)
在swift 5、可以使用
for x in mylocationsArr {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(x.lat, x.lon)
marker.title = x.description
marker.map = self.googleMap
}
//you can fill nearByPlacesArray
nearByPlacesArray = ["lat:":"23.898988",Long:"72.898988"]
// you can add multiple lat and long in nearByPlacesArray this array.
for data in nearByPlacesArray{
let lat = Double(data.Latitude)
let long = Double(data.Longitude)
let location = CLLocationCoordinate2D(latitude:lat!, longitude:long!)
print("location1: \(location)")
let marker = GMSMarker()
marker.position = location
// marker.snippet = data.name
marker.map = mapView
marker.icon = GMSMarker.markerImage(with:.black)
marker.icon = UIImage(named: "Location_Icon")
}