2016-07-29 3 views
0

Die folgenden Codes von Google geben nur einen Platz zurück, wenn ich einen aus der Liste wie den, den ich angehängt habe, auswähle.Ist es möglich, den Bildschirm nicht zu übernehmen? Google Places IOS swift

Meine Frage: Gibt es eine Funktion für mich, um alle Details des Orts in einer gegebenen Koordinate zu speichern? Zum Beispiel, wenn ich eine Koordinate von (51.5108396, -0.0922251) habe, wie kann ich alle Informationen von Orten in der Nähe bekommen? Ich kenne Json nicht. Gibt es ein nahes Beispiel, was ich will? Danke vielmals.

Diese Funktion placesClient.currentPlaceWithCallback ist irgendwie nah an dem, was ich will, aber es kann keine benutzerdefinierten Koordinaten verwenden, da es die aktuelle Koordinate des Benutzers verwendet.

//https://developers.google.com/places/ios-api/placepicker 
let center = CLLocationCoordinate2DMake(51.5108396, -0.0922251) 
let northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001) 
let southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001) 
let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest) 
let config = GMSPlacePickerConfig(viewport: viewport) 
let placePicker = GMSPlacePicker(config: config) 
placePicker?.pickPlaceWithCallback({ (place: GMSPlace?, error: NSError?) -> Void in 
    if let error = error { 
     print("Pick Place error: \(error.localizedDescription)") 
     return 
    } 
    if let place = place { 
     print("Place name \(place.name)") 
     print("Place address \(place.formattedAddress)") 
     print("Place attributions \(place.attributions)") 
    } else { 
     print("No place selected") 
    } 
}) 

enter image description here

+0

Es ist nicht klar, was Sie fragen. Google verfügt über eine Vielzahl von APIs, um Informationen zu Standorten zu erhalten. Einige interagieren mit dem Benutzer, und es gibt auch Back-End-APIs, mit denen Sie Abfragen ausführen und Ergebnisse abrufen können (in JSON, wenn der Speicher bereitgestellt wird). Sie müssen definieren, was Sie tun müssen, und dann eine API finden, mit der Sie das tun können. –

+0

Nur meine Frage aktualisiert. Gibt es ein Beispiel für Json? –

+0

Wahrscheinlich, ja. Aber ich werde deine Arbeit nicht für dich tun. Ich müsste die Google-Dokumentation so durchsuchen, wie Sie es möchten. –

Antwort

0

Fetching nearby places using google maps Etwas aufgrund Upgrade iOS-Version geändert.

komplett geändert Code

func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, types:[String]) { 
    var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=\("your api key")&location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true" 
    let typesString = types.count > 0 ? types.joinWithSeparator("|") : "food" 
    urlString += "&types=\(typesString)" 
    urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())! 
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true 
    let session = NSURLSession.sharedSession() 
    let placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in 
     UIApplication.sharedApplication().networkActivityIndicatorVisible = false 
     if let jsonResult = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as? NSDictionary { 
      let returnedPlaces: NSArray? = jsonResult["results"] as? NSArray 
      if returnedPlaces != nil { 
       for index in 0..<returnedPlaces!.count { 
        if let returnedPlace = returnedPlaces?[index] as? NSDictionary { 
         var placeName = "" 
         var latitude = 0.0 
         var longitude = 0.0 
         if let name = returnedPlace["name"] as? NSString { 
          placeName = name as String 
         } 
         if let geometry = returnedPlace["geometry"] as? NSDictionary { 
          if let location = geometry["location"] as? NSDictionary { 
           if let lat = location["lat"] as? Double { 
            latitude = lat 
           } 
           if let lng = location["lng"] as? Double { 
            longitude = lng 
           } 
          } 
         } 
         print("index", index, placeName, latitude, longitude) 
        } 
       } 
      } 
     } 
    } 
    placesTask.resume() 
} 
Verwandte Themen