Am trying to implement to find nearby places of my current location using google maps. I have created a project in Google Developer Console and got 'ios APIkey' and 'Server APIkey'. I also enabled Google Places API and Google Maps SDK for iOS. Below is the code to find out near by places.
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, name : String){
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=\(apiServerKey)&location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true"
urlString += "&name=\(name)"
urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
println(urlString)
if placesTask.taskIdentifier > 0 && placesTask.state == .Running {
placesTask.cancel()
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in
println("inside.")
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
if let json = NSJSONSerialization.JSONObjectWithData(data, options:nil, error:nil) as? NSDictionary {
if let results = json["results"] as? NSArray {
for rawPlace:AnyObject in results {
println(rawPlace)
self.results.append(rawPlace as! String)
}
}
}
self.placesTask.resume()
}
}
The passed coordinate is current location's coordinate. If I execute a above code, nothing is happening but generated url is valid one. If i put that url in Google am getting correct results. But nothing displaying in my app. Please help me to resolve this. and please let me know where am wrong!!!
You add your places to the results array, but you have done anything to update your mapview, for example, adding markers to your mapview.
sample code add marker to mapview:
You can see this tutorial about how to get nearby places with Google Maps in Swift.
Just set your Latt, long and GoogleAPIKey.
Moreover, if you want to search place by text, Just make urlString as below.
In arrayPlaces you have nearby places.