0

Below codes from Google only return a place if I pick one from the list like the one I attached.

My question: Is there any function available for me to store all the place's detail in a given coordinate? For example, if I have a coordinate of (51.5108396, -0.0922251), how can I get all the information of nearby places? I am not familiar with Json. Is there any example close to what I want? Thanks a lot.

This function placesClient.currentPlaceWithCallback is somehow close to what I want but it cannot use custom coordinate because it uses user's current coordinate.

//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

Pak Ho Cheung
  • 1,382
  • 6
  • 22
  • 52
  • It is not at all clear what you're asking. Google has a variety of APIs for getting info about locations. Some interact with the user, and there are also back-end APIs that let you make queries and get back results (in JSON, if memory serves). You'll need to define what you need to do and then find an API that will let you do that. – Duncan C Jul 30 '16 at 00:17
  • Just updated my question. Is there any example for Json? – Pak Ho Cheung Jul 30 '16 at 00:54
  • Probably, yes. However, I'm not going to do your work for you. I'd have to so search Google's documentation just like you will. – Duncan C Jul 30 '16 at 01:02
  • 1
    http://stackoverflow.com/questions/29636718/fetching-nearby-places-using-google-maps Is it a good example for learning JSON and approach the things i want? – Pak Ho Cheung Jul 30 '16 at 01:29

1 Answers1

0

Fetching nearby places using google maps Something is changed due to upgraded iOS version.

complete changed 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()
}
Community
  • 1
  • 1
Pak Ho Cheung
  • 1,382
  • 6
  • 22
  • 52