【问题标题】:How to assign marker.title by coordinate (swift)?如何通过坐标(swift)分配marker.title?
【发布时间】:2016-08-22 14:47:55
【问题描述】:

我有longitudelatitude,因为我使用didLongPressAtCoordinate,现在我想将marker.title 分配给这个coordinate 的标题。

func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
    self.googleMapsView.clear()
    self.googleMapsView.myLocationEnabled = false
    marker.map = self.googleMapsView
    marker.title = --> //my problem


}

我找不到好的解决方案

【问题讨论】:

  • 嗨 scbas1000,欢迎来到 Stack Overflow。你的问题很模糊,回答的可能性很小。请访问how to ask 上的指南。
  • 感谢您的关注,我编辑它,现在我认为是正确的
  • @scbas1000 不,你的问题仍然很模糊,你还没有发布任何代码来显示你到目前为止所拥有的东西。发布您拥有的代码,清楚地讨论您目前正在做什么,您想要实现的目标(示例会有所帮助),然后列出您在到达那里时遇到的错误/问题
  • @scbas1000 //my problem 应该是什么意思?
  • 我找不到好的解决方案

标签: ios swift google-maps-sdk-ios


【解决方案1】:

这样获取您的地址:-

  func getAddressForLatLng(latitude: CLLocationDegrees, longitude: CLLocationDegrees, completionBlock : ((addrs : String)->Void)) {
    let url = NSURL(string: "\(baseUrl)latlng=\(latitude),\(longitude)&key=\(serverKey)")
    print(url)


    let data = NSData(contentsOfURL: url!)
    let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
    if let result = json["results"] as? NSArray {
        if let address = result[0]["address_components"] as? NSArray {
            let number = address[0]["short_name"] as! String
            let street = address[1]["short_name"] as! String
            let city = address[2]["short_name"] as! String
            let state = address[4]["short_name"] as! String
            let zip = address[6]["short_name"] as! String
            print("\(number) \(street), \(city), \(state) \(zip)")
            completionBlock(addrs: "\(number) \(street), \(city), \(state) \(zip)")

        }
    }
}

更新您的 didLongPressAtCoordinate 函数:-

func mapView(mapView: GMSMapView!, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) {

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
    self.googleMapsView.clear()
    self.googleMapsView.myLocationEnabled = false
    marker.map = self.googleMapsView
    getAddressForLatLng(coordinate.latitude, longitude: coordinate.longitude) { (addrs) in

        marker.title = addrs
        marker.snippet = "\(coordinate.latitude),\(coordinate.longitude)"

    }


}

请注意:- baseUrlserverKey 是您的 Google 控制台 baseUrl 和 serverKey

【讨论】:

  • 您确实意识到州和城市不会经常出现,而且您的代码经常出错。最重要的是,它被严格编写为适用于美国的大多数地区,并且不适用于美国以外的大多数地区。
【解决方案2】:

您需要对坐标进行地理编码。您每分钟可以执行此操作的次数限制为 50 次(由 google 设置),因此使用此操作时要高效。 Here is a tutorial on the process

一旦你有了地址,你就说

marker.title = "\(theAddressVariableName)\n\nLat: \(coordinate.latitude)\nLon: coordinate.longitude"

根据需要格式化它。另外,我不明白为什么您会在该函数中包含代码行self.googleMapsView.myLocationEnabled = false。它可能应该去别的地方,但我会允许对它的位置产生怀疑。

【讨论】:

    猜你喜欢
    • 2018-05-06
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 2022-12-19
    • 2021-11-21
    • 1970-01-01
    相关资源
    最近更新 更多