2016-06-14 3 views
0

Ich habe einen Breiten- und Längengrad von Marker in Google Maps, die ich in den Standortnamen String in Swift konvertieren möchte. Was ist der beste Weg, dies zu tun? Ich möchte die Standortadresse der Marker anzeigen und weiß nicht, wie es geht.Markieradresse (Adresse) mit einem Klick auf swift anzeigen

hier ist mein Code, der i-Marker und Breiten- und Längen hinzufügen verwendet erhalten:

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

Mögliche Duplikat: 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- bad-access-using-gmaps-sdk-1-9-0-xcode-6-4-runing-auf-8-3-device/http://stackoverflow.com/questions/31264537/adding-google-maps- as-subview-crashes-ios-app-mit-exc-bad/http://stackoverflow.com/questions/24837295/how-to-solve-this-exc-bad-accesscode-exc-i38 6-gpflt-in-swift-Programmierung –

Antwort

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

Ich habe diesen Fehler: 'sendSynchronousRequest (_: returningResponse :)' war in iOS 9.0 veraltet: Benutze [NSURLSession dataTaskWithRequest: completionHandler:] (siehe NSURLSession.h !! und ich sehe keine adresse in meiner app –

+0

benutze diesen code ich hinzugefügt einmal –

+0

jetzt kann es keinen marker mehr machen! und der Fehler ist für diese Zeile: self.getAddressForLatLng (String (Format: "% @", marker.position.latitude), Länge: String (Format: "% @", marker.position.longitude)) –

Verwandte Themen