【问题标题】:Tap GMSPolygon Swift点击 GMSPolygon Swift
【发布时间】:2017-06-01 18:46:29
【问题描述】:

我的地图中有一些 GMSPolygon:

 let españa = GMSMutablePath()
    españa.add(CLLocationCoordinate2D(latitude: 42.11, longitude: -9.37))
    españa.add(CLLocationCoordinate2D(latitude: 43.94, longitude: -9.55))
    españa.add(CLLocationCoordinate2D(latitude: 43.60, longitude: -1.89))
    españa.add(CLLocationCoordinate2D(latitude: 42.02, longitude: 3.72))
    españa.add(CLLocationCoordinate2D(latitude: 36.16, longitude: -2.65))
    españa.add(CLLocationCoordinate2D(latitude: 37.10, longitude: -7.28))
    españa.add(CLLocationCoordinate2D(latitude: 42.08, longitude: -6.61))

    let polygonEspaña = GMSPolygon(path: españa)
    polygonEspaña.fillColor = UIColor(red : 0, green: 0, blue: 0, alpha: 0.05);
    polygonEspaña.strokeColor = .black
    polygonEspaña.strokeWidth = 2
    polygonEspaña.map = myMapView
    polygonEspaña.isTappable = true

我想点击它们来做一些功能。

我实现了这个方法:

import UIKit
import GoogleMaps



class ViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
}

当我点击它们时,什么也没有发生。

// ###### Click Country


private func myMapView(_ myMapView: GMSMapView, didTap overlay: GMSOverlay) {
    print("User Tapped Layer: \(overlay)")
}

我认为我的GMSMapViewDelegate 有问题,但我无法解决,我该怎么办?

编辑:我也有这个。

// ##### MAPA


    let mapCenter = GMSCameraPosition.camera(withLatitude: 48.23,
                                             longitude: 16.35,
                                             zoom: 3)

    myMapView.camera = mapCenter
    myMapView.setMinZoom(1, maxZoom: 8)

    myMapView.delegate = self


    // #### Estilo de mapa


    do {
        if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
            myMapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
        } else {
            NSLog("Unable to finde style.json")
        }
    } catch {
        NSLog("One or more of the map styles failed to load. \(error)")
    }

谢谢

【问题讨论】:

  • 你有这条线吗? : myMapView.delegate = self
  • 是的,我有。还有一个 MapStyle 工作正常。谢谢
  • 我的回答有帮助吗?

标签: ios swift google-maps tap


【解决方案1】:

首先,感谢您使用我对this问题的回答:)

为什么要创建委托方法private?我认为这可能是它没有被调用的原因。删除它。

【讨论】:

  • 抱歉,我不知道私有方法。怎么能去掉呢?谢谢!
  • 只需使用退格键从函数本身中删除单词private。所以你的函数应该是func myMapView(_ myMapView: GMSMapView, didTap overlay: GMSOverlay) 而不是private func myMapView(_ myMapView: GMSMapView, didTap overlay: GMSOverlay)
  • 我做到了,但仍然没有工作。我也有位置功能,但不起作用。 // ###### Click Country func myMapView(_ myMapView: GMSMapView, didTap overlay: GMSOverlay) { print("User Tapped Layer: (overlay)") } // #### 在控制台中查找位置。 func location (_ myMapView: GMSMapView, didTapAt 坐标: CLLocationCoordinate2D) { print("你在 (coordinate.latitude), (coordinate.longitude)") }
  • 我不确定赋值运算符是如何工作的,但您可以尝试将polygonEspaña.isTappable = true 移动到polygonEspaña.map = myMapView 上方
  • 仍然没有工作。我这样做了,什么也没发生。谢谢@Emil David
【解决方案2】:

终于成功了。

我的幻灯片菜单有问题:

RevealViewController

我不知道为什么使用 RevealViewController,函数来自:

GSMapViewDelegate 

没用

现在,当我点击我的多边形时,我会在控制台中收到一条文本。

但我有关于我的多边形的完整信息,我需要它的名称。

例如,如果我在西班牙点击,我希望收到“您在西班牙被窃听”。

谢谢

【讨论】:

    猜你喜欢
    • 2016-08-23
    • 2018-01-09
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2018-01-26
    • 1970-01-01
    相关资源
    最近更新 更多