【问题标题】:Put multiple markers in Google maps. Swift在谷歌地图中放置多个标记。迅速
【发布时间】:2018-03-13 14:32:32
【问题描述】:

我有数组中的纬度和经度数。我想在谷歌地图中显示多个标记。我对此有疑问。这是我的代码。

@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()
}

我无法使用此代码在地图视图中显示多个标记。地图中只显示一个与数组的最后一个索引相关的标记。请有人帮助我。谢谢

【问题讨论】:

    标签: swift google-maps marker


    【解决方案1】:

    您只需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()
    }
    

    【讨论】:

    • @Sid Mhatre 非常感谢
    【解决方案2】:

    这是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)
    
        }
    

    在循环后打印一个简单的标记以查看工作!

    但是你要实现 lat、long、title 并从一些数据/变量中截取。

    是的,我在寻找一种实现动态变量的方法时浪费了很多时间......

    var marker1 = 
    var marker2 = 
    var marker3 = 
    ....
    

    如果您现在如何使用 swift 实现,请实施该帖子。

    :-)

    【讨论】:

      【解决方案3】:

      在swift 5中,你可以使用

      for x in mylocationsArr {
          let marker = GMSMarker()
          marker.position = CLLocationCoordinate2DMake(x.lat, x.lon)
          marker.title = x.description
          marker.map = self.googleMap        
      }
      

      【讨论】:

        【解决方案4】:
        //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")
                
                
                
                
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-18
          • 2011-02-22
          • 1970-01-01
          相关资源
          最近更新 更多