2016-08-22 5 views
-1

私はdidLongPressAtCoordinateを使用しているのでlongitudelatitudeを持っていますが、今はcoordinateのタイトルにmarker.titleを割り当てたいと思います。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

Hey scbas1000、スタックオーバーフローへようこそ。あなたの質問は非常に曖昧で、答えが出る可能性はほとんどありません。 [質問する](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)" 

    } 


} 

マインド: - baseUrlにそしてサーバキーをGoogleコンソールbaseUrlにあり、 serverKey

+0

州と都市は一定の場所に行かず、コードが間違っていることに気付きます。それの上にそれは厳密に米国の過半数と働くために書かれ、それの外の多数を働かせない。 – Sethmr

+0

ご回答ありがとうございます#Davidian – scbas1000

0

ための良い解決策を見つけることができません。あなたは1分あたり(Googleによって設定される)これを行うことができる回数は50回に制限されていますので、効率的に使用してください。あなたはアドレスを持っていたらHere is a tutorial on the process

は、あなただけの

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

フォーマットそれはしかし、あなたがしたいと言います。また、私はなぜコードの行をその機能の中にself.googleMapsView.myLocationEnabled = false持っているのか分かりません。それはおそらくどこか別の場所に行くはずですが、私はその配置に疑念の恩恵を与えるでしょう。

関連する問題