2016-08-22 36 views
-1

我有longitudelatitude,因为我使用didLongPressAtCoordinate,现在我想将marker.title分配给这个coordinate的标题。如何通过坐标(swift)分配marker.title?

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 


} 

我无法找到该

+4

嘿scbas1000,欢迎来到Stack Overflow。你的问题非常模糊,几乎没有机会被回答。请访问[如何问]的指南(http://stackoverflow.com/help/how-to-ask)。 – Alexander

+0

感谢您的关注,我编辑它,现在是对的我认为 – scbas1000

+1

@ scbas1000不,你的问题仍然很模糊,你还没有发布任何代码,显示你到目前为止。发布你的代码,明确地讨论你在一分钟内的工作,你试图达到的目标(例子会有所帮助),然后列出你在面对的错误/问题 –

回答

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是你的谷歌控制台的baseUrl和serverKey

+0

你应该认识到,州和城市不会经常出现,而且你的代码经常出错。最重要的是,它的写作严格要与美国的大多数人一起工作,并且不会在大多数美国人以外工作。 – Sethmr

+0

感谢您的回复#Davavidian – scbas1000

0

您需要进行地理编码的坐标很好的解决方案。你有50次的限制,你可以每分钟做一次(由谷歌设置),所以要高效使用这样做。 Here is a tutorial on the process

一旦你的地址,你刚才说

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

格式你想要的东西。此外,我不明白为什么你会在该函数内部有一行代码self.googleMapsView.myLocationEnabled = false。它可能应该去其他地方,但我会允许对其位置的怀疑的好处。