无法在GSMapView xCode Swift 3中添加多个标记

时间:2017-07-07 13:34:04

标签: swift xcode google-maps google-maps-markers gmsmapview

我正在尝试在我的应用中在Google地图上添加多个标记。

在viewDidLoad下的viewcontrolller中,我可以加载地图和单个标记。

override func viewDidLoad() {
    super.viewDidLoad()
    title = NSLocalizedString("section_map", comment: "test")
    let camera = GMSCameraPosition.camera(withLatitude: 48.7784, longitude:9.18121, zoom: 12)
    let mapView = GMSMapView.map(withFrame: .zero, camera: camera)


    view = mapView
    mapView.settings.myLocationButton = true

    mapView.delegate = self

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: 48.7784,longitude: 9.18121)
    marker.title = "title"
    marker.snippet = "snipple"
    marker.icon = UIImage(named:"pin_you")
    marker.map = mapView

    mapData()
}

它调用mapData()并从中生成json文件 解析后调用setPin来设置标记

  func setPin(){


    DispatchQueue.main.async {

       for item in self.mapItems {
           print (" \(item.name)  \(item.marker) \(item.latitude) \(item.longitude)")

           let marker = GMSMarker()
           marker.position = CLLocationCoordinate2D(latitude: item.latitude,longitude: item.longitude)
           marker.title = item.name
           marker.snippet = item.fulladdress

           var iconImage: String
           switch (item.marker){
           case 1:
              iconImage = "pin_silver"
           case 2:
              iconImage = "pin_blue"
           case 3:
              iconImage = "pin_gold"
           case 6:
              iconImage = "pin_you"
           default:
              iconImage = "pin_silver"
           }
           marker.icon = UIImage(named:iconImage)
           marker.map = self.mapView


       }

    }
}

未显示引脚。

self.mapItems项目中的打印显示

Position number A  1 48.76947562 9.15440351
Position number B  1 48.75716485 9.17081058
Position number C  1 48.81191625 9.22752149
Position number D  2 48.81192516 9.22766708

这意味着所有适当的数据都可用。 但是地图是在viewDidLoad中制作的一个引脚 函数setPin中的标记未显示或未设置。

有没有人有想法?

2 个答案:

答案 0 :(得分:0)

我已经解决了它已经为setPin改变了mapData(mapView:GMSMapView!)(mapView:GMSMapView!)

  func setpin(mapView: GMSMapView!){


    DispatchQueue.main.async {

       for item in self.mapItems {
           print (" \(item.name)  \(item.marker) \(item.latitude) \(item.longitude)")

           let marker = GMSMarker()
           marker.position = CLLocationCoordinate2D(latitude: item.latitude,longitude: item.longitude)
           marker.title = item.name
           marker.snippet = item.fulladdress

           var iconImage: String
           switch (item.marker){
           case 1:
              iconImage = "pin_silver"
           case 2:
              iconImage = "pin_blue"
           case 3:
              iconImage = "pin_gold"
           case 6:
              iconImage = "pin_you"
           default:
              iconImage = "pin_silver"
           }
           marker.icon = UIImage(named:iconImage)
           marker.map = mapView


       }

    }
}

答案 1 :(得分:0)

聚类标记,您可以在地图上放置大量标记,而不会使地图难以阅读。标记聚类实用程序可帮助您管理不同缩放级别的多个标记。

有关完整的代码示例,请参阅GitHub上的ObjCDemoApp和SwiftDemoApp。

添加简单标记聚类器。

/// Point of Interest Item which implements the GMUClusterItem protocol.
class POIItem: NSObject, GMUClusterItem {
  var position: CLLocationCoordinate2D
  var name: String!

  init(position: CLLocationCoordinate2D, name: String) {
    self.position = position
    self.name = name
  }
}

以下代码使用实用程序库中包含的GMUNonHierarchicalDistanceBasedAlgorithm和MUDefaultClusterRenderer创建集群管理器:

class ViewController: UIViewController, GMUClusterManagerDelegate,
GMSMapViewDelegate {

   private var mapView: GMSMapView!
   private var clusterManager: GMUClusterManager!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Set up the cluster manager with the supplied icon generator and
    // renderer.
    let iconGenerator = GMUDefaultClusterIconGenerator()
    let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
    let renderer = GMUDefaultClusterRenderer(mapView: mapView,
                            clusterIconGenerator: iconGenerator)
    clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm,
                                                  renderer: renderer)

    // Generate and add random items to the cluster manager.
    generateClusterItems()

    // Call cluster() after items have been added to perform the clustering
    // and rendering on map.
     clusterManager.cluster()
  }
}

通过调用clusterManager将标记作为GMUClusterItem对象输入集群:addItem:。以下代码在地图相机的范围内随机生成群集项(POI),然后将它们提供给群集管理器:

/// Randomly generates cluster items within some extent of the camera and
/// adds them to the cluster manager.
private func generateClusterItems() {
  let extent = 0.2
   for index in 1...kClusterItemCount {
    let lat = kCameraLatitude + extent * randomScale()
    let lng = kCameraLongitude + extent * randomScale()
    let name = "Item \(index)"
    let item =
    POIItem(position: CLLocationCoordinate2DMake(lat, lng), name: name)
    clusterManager.addItem(item)
  }
}

 /// Returns a random value between -1.0 and 1.0.
 private func randomScale() -> Double {
   return Double(arc4random()) / Double(UINT32_MAX) * 2.0 - 1.0
}

处理标记和群集上的事件

class ViewController: UIViewController, GMUClusterManagerDelegate, GMSMapViewDelegate {

  private var mapView: GMSMapView!
  private var clusterManager: GMUClusterManager!

  override func viewDidLoad() {
     super.viewDidLoad()

    // ... Rest of code omitted for easy reading.

    // Register self to listen to both GMUClusterManagerDelegate and
    // GMSMapViewDelegate events.
    clusterManager.setDelegate(self, mapDelegate: self)
  }

  // MARK: - GMUClusterManagerDelegate

  func clusterManager(clusterManager: GMUClusterManager, didTapCluster cluster: GMUCluster) {
    let newCamera = GMSCameraPosition.cameraWithTarget(cluster.position,
  zoom: mapView.camera.zoom + 1)
    let update = GMSCameraUpdate.setCamera(newCamera)
    mapView.moveCamera(update)
  }

   // MARK: - GMUMapViewDelegate

  func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
    if let poiItem = marker.userData as? POIItem {
       NSLog("Did tap marker for cluster item \(poiItem.name)")
    } else {
       NSLog("Did tap a normal marker")
    }
    return false
  }
}

有关详细信息,请按照Here

进行操作