在mapView中显示多个标记

时间:2018-01-18 10:33:51

标签: ios swift google-maps swift4

我正在使用谷歌地图,我创建了这个功能

  func addMarker(place:EClass) {

            guard let coordinates = place.location  else {
                return
            }

            self.destination = coordinates

            marker = GMSMarker()
            marker?.position = coordinates
            marker?.title = place.name
            marker?.map = mapView
            mapView.selectedMarker = marker

        }

在我的mapView中在EClass类型的对象的位置添加一个标记;但在我的情况下,我需要在var places: [EClass] = []的每个元素的位置上的mapView上显示不同的标记。要做到这一点,我试图在我的viewDidLoad

中添加这样的东西
let srt = places

   srt.forEach { addMarker(place: $0) }

但它不起作用,我该怎么办?

这是我的班级EClass

import UIKit
import CoreLocation


private let geometryKey = "geometry"
private let locationKey = "location"
private let latitudeKey = "lat"
private let longitudeKey = "lng"
private let nameKey = "name"
private let vicinityKey = "vicinity"



class EClass: NSObject  {


    var location: CLLocationCoordinate2D?
    var name: String?
    var vicinity: String?
    var placeId: String



    init(placeInfo:[String: Any]) {

        placeId = placeInfo["place_id"] as! String


        // coordinates
        if let g = placeInfo[geometryKey] as? [String:Any] {
            if let l = g[locationKey] as? [String:Double] {
                if let lat = l[latitudeKey], let lng = l[longitudeKey] {
                    location = CLLocationCoordinate2D.init(latitude: lat, longitude: lng)
                }
            }
        }

        // name
        name = placeInfo[nameKey] as? String


        }

// UPDATE

static func getNearbyPlaces(by category:String, coordinates:CLLocationCoordinate2D, radius:Int, token: String?, completion: @escaping (QNearbyPlacesResponse?, Error?) -> Void) {

    var params : [String : Any]

    if let t = token {
        params = [
            "key" : AppDelegate.googlePlacesAPIKey,
            "pagetoken" : t,
        ]
    } else {
        params = [
            "key" : AppDelegate.googlePlacesAPIKey,      
            "radius" : radius,
            "location" : "\(coordinates.latitude),\(coordinates.longitude)",
            "type" : category.lowercased()
        ]
    }

    Alamofire.request(searchApiHost, parameters: params, encoding: URLEncoding(destination: .queryString)).responseJSON { response in

        if let error = response.error {
            completion(nil, error)
        }
        if let response = QNearbyPlacesResponse(dic : response.result.value as? [String : Any]) {
            completion(response, nil)
        }
        else {
            completion(nil, QNearbyPlacesResponseError.noParsingDone)
        }
    }
}

0 个答案:

没有答案