Search code examples
google-mapsswiftios8google-places-apigoogle-maps-sdk-ios

Fetching nearby places using google maps


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


Solution

  • 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.