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:
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
}
}
}
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(latitude, longitude)
marker.title = placeName
marker.map = self.mapView
}
}
}
You can see this tutorial about how to get nearby places with Google Maps in Swift.
Just set your Latt, long and GoogleAPIKey.
NSString *urlString= [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=%@,%@&radius=2000&key=%@",lati,longi,kGoogleAPIKey];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
if (connectionError)
{
NSLog(@"Error");
}
else
{
NSDictionary *dictJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *arrayPlaces = [[NSArray alloc] initWithArray:[dictJson objectForKey:@"results"]];
NSLog("Place %@",arrayPlaces);
}
}];
Moreover, if you want to search place by text, Just make urlString as below.
NSString *urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?sensor=true&key=%@&language=en&input=%@&query=123+main+street&radius=50000&location=0.0,0.0&rankby=distance",kGoogleAPIKey,searchText];
In arrayPlaces you have nearby places.