Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 823
Pruthvi Hariharan Avatar asked Apr 14 '15 20:04

Pruthvi Hariharan


People also ask

How do I get a list of places on Google Maps API?

Go to the Google Cloud Console. Click the Select a project button, then select the same project you set up for the Maps JavaScript API and click Open. From the list of APIs on the Dashboard, look for Places API. If you see the Places API in the list, it's already enabled.

Is Google place API free?

Places API is not free, however, once you set up your billing account, you will be entitled for a one time $300 free credit(usable for Google Cloud Platform products) and a monthly recurring $200 free credit(exclusive for Google Maps Platform products), after consuming the credits, you will receive an OVER_QUERY_LIMIT ...


1 Answers

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.

like image 60
ztan Avatar answered Sep 30 '22 14:09

ztan