Capture nearby places using Google maps

I am trying to implement to find nearby places of my current location using google maps. I created a project in the Google Developer Console and got the "ios APIkey" and "Server APIkey". I also included the Google Places API and the Google Maps SDK for iOS. Below is a code that can be found near 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 transmitted coordinate is the current coordinate of the location. If I execute the above code, nothing happens, but the generated URL is valid. If I put this URL on Google, I get the correct results. But nothing is displayed in my application. Please help me solve this problem. and please let me know where it is wrong!

+6
source share
2 answers

You add your places to the results array, but you did something to update your mapview, for example by adding markers to your mapview.

Example code Add marker in 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 on how to get nearby locations with Google Maps in Swift.

+9
source

Just install 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); } }]; 

Also, if you want to find a place in the text, just create a urlString as shown 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.

0
source

Source: https://habr.com/ru/post/985142/


All Articles