2016-06-14 8 views
0

Swift에서 위치 이름 문자열로 변환하려는 Google지도의 마커의 위도와 경도 위치가 있습니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까? 마커의 위치 주소를 표시하려고하는데 어떻게해야할지 모르겠다.빠른 클릭으로 마커 주소 (거리 주소) 표시

func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) { 
     if counterMarker < 2 
     { 
      counterMarker += 1 
      let marker = GMSMarker(position: coordinate) 
      marker.appearAnimation = kGMSMarkerAnimationPop 

       marker.map = mapView 
      marker.position.latitude = coordinate.latitude 
      marker.position.longitude = coordinate.longitude 

      print(marker.position.latitude) 
      print(marker.position.longitude) 


      } 
    } 
+0

가능한 중복 : http://stackoverflow.com/questions/19025148/ios-7- 여기

내가 위도와 경도를 마커를 추가하고 가져 오는 데 사용 내 코드입니다 add-overlay-crash-app/http://stackoverflow.com/questions/6721124/app-crashing-when-i-add-overlay-to-mkmapview/http://stackoverflow.com/questions/31390630/exc- 불량 액세스 사용 중 gmaps-sdk-1-9-0-xcode-6-4-run-on-8-3-device/http://stackoverflow.com/questions/31264537/adding-google-maps- as-subview-crashes-ios-app-exc-bad-bad/http://stackoverflow.com/questions/24837295/how-to-solve-this-exc-bad-accesscode-exc-i38 6-gpflt-in-swift 프로그래밍 –

답변

2
func mapView(mapView: GMSMapView, didLongPressAtCoordinate coordinate: CLLocationCoordinate2D) { 
      if counterMarker < 2 
      { 
       counterMarker += 1 
       let marker = GMSMarker(position: coordinate) 
       marker.appearAnimation = kGMSMarkerAnimationPop 

        marker.map = mapView 
       marker.position.latitude = coordinate.latitude 
       marker.position.longitude = coordinate.longitude 

        self.getAddressForLatLng(String(format: "%@",marker.position.latitude), longitude:String(format: "%@",marker.position.longitude) 

       } 
     } 


    func getAddressForLatLng(latitude: String, longitude: String) { 
     let url = NSURL(string: "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(latitude),\(longitude)&key=YOUR-APIKEY") 
     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("\n\(number) \(street), \(city), \(state) \(zip) \(address)") 
      } 
     } 
    } 
+0

iOS 9.0에서 'sendSynchronousRequest (_ : returningResponse :)'가 사용되지 않습니다. [NSURLSession dataTaskWithRequest : completionHandler :]를 사용하십시오 (NSURLSession.h !! 참조). 그리고 내 애플 리케이션에 어떤 주소가 보이지 않는다. –

+0

이 코드를 사용하면 한 번 더 덧붙여 –

+0

이제 마커를 더 이상 만들 수 없다! 오류 : self.getAddressForLatLng (String (format : "% @", marker.position.latitude), longitude : String (형식 : "% @", marker.position.longitude)) –