Giter Club home page Giter Club logo

spgoogleplacesautocomplete's Introduction

SPGooglePlacesAutocomplete

SPGooglePlacesAutocomplete is a simple objective-c wrapper around the Google Places Autocomplete API. The API can be used to provide autocomplete functionality for text-based geographic searches, by returning Places such as businesses, addresses, and points of interest as a user types. SPGooglePlacesAutocomplete also provides support for converting Place results into CLPlacemark objects for easy mapping with MKMapView.

Screenshots

How To Use It

Requirements

SPGooglePlacesAutocomplete requires a deployment target >= iOS 5.0.

Installation

  1. Link your project against the CoreLocation framework.
  2. Copy the following files to your project:
    • SPGooglePlacesAutocompleteUtilities.h/.m
    • SPGooglePlacesAutocompleteQuery.h/.m
    • SPGooglePlacesPlaceDetailQuery.h/.m
    • SPGooglePlacesAutocompletePlace.h/.m
  3. (Optional) If you would like to utilize the provided sample view controller for searching and mapping Places, link your project against the MapKit framework and copy the following files to your project:
    • SPGooglePlacesAutocompleteViewController.h/.m/.xib
    • locateButton(@2x).png
    • location(@2x).png
  4. Open SPGooglePlacesAutocompleteUtilities.h and replace kGoogleAPIKey with your Google API key. You can find your API key in the Google APIs Console.

Performing Queries

Instantiate a new SPGooglePlacesAutocompleteQuery and fill in the properties you'd like to specify.

#import "SPGooglePlacesAutocompleteQuery.h"

...

SPGooglePlacesAutocompleteQuery *query = [SPGooglePlacesAutocompleteQuery query];
query.input = @"185 berry str";
query.radius = 100.0;
query.language = @"en";
query.types = SPPlaceTypeGeocode; // Only return geocoding (address) results.
query.location = CLLocationCoordinate2DMake(37.76999, -122.44696);

Then, call -fetchPlaces to ping Google's API and fetch results. The resulting array will return objects of the SPGooglePlacesAutocompletePlace class.

[query fetchPlaces:^(NSArray *places, NSError *error) {
    NSLog(@"Places returned %@", places);
}];

If you need to update the query (for instance, as the user types), simply update the appropriate properties and call -fetchPlaces again. Any outstanding requests will automatically be cancelled and a new request with the updated properties will be issued.

Resolving Places to CLPlacemarks

The Google Places Autocomplete API will return the names of Places that match your query. It will not, however, return lat-long information about these results. SPGooglePlacesAutocomplete handles this by resolving Place results to placemarks. Simply call -resolveToPlacemark on a SPGooglePlacesAutocompletePlace:

[query fetchPlaces:^(NSArray *places, NSError *error) {
    SPGooglePlacesAutocompletePlace *place = [places firstObject];
    if (place) {
        [place resolveToPlacemark:^(CLPlacemark *placemark, NSString *addressString, NSError *error) {
            NSLog(@"Placemark: %@", placemark);
        }];
    }
}];

When searching for "geocode" (address) Places, the library utilizes CLGeocoder to geocode the address. When searching for "establishment" (business) Places, the library will automatically ping The Google Places API to fetch the details needed to geolocate the business.

Sample Code

For an example of how to use SPGooglePlacesAutocomplete, please see the included example project. Be sure to replace kGoogleAPIKey with your Google API key!

spgoogleplacesautocomplete's People

Contributors

spoletto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spgoogleplacesautocomplete's Issues

How to get dictionary address

Hi,
thank's for yours work.
I use it but and i've a question:
How to obtain the address dictionary and not the string in the tableview?

I want get results the "terms" in google API to autocomplete the address like

Address
City - Country.

THZ

https://developers.google.com/places/documentation/autocomplete?hl=it
-- SAMPLE JSON GOOGLE --
{
"status": "OK",
"predictions": [ {
"description": "Paris, France",
"id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
"reference": "CiQYAAAA0Q_JA...kT3ufVLDDvTQsOwZ_tc",
"terms": [ {
"value": "Paris",
"offset": 0
}, {
"value": "France",
"offset": 7
} ],
"types": [ "geocode" ],
"matched_substrings": [ {
"offset": 0,
"length": 5
} ]
}, {

Request Denied

I followed the instructions in the README and used the key for my app "Google Places API for iOS" as well as "Google Maps JavaScript API" in the kGoogleAPIKey define. However, in both cases, I get the error:
Could not fetch Places. REQUEST_DENIED

Browser API Key required even if mobile app implementation

Hi, great job with the wrapper. Thanks for creating it.

Implementing this on a mobile app requires the Google api key for browser apps, even if it is a mobile app implementation.

Not sure if this has been flagged up already but thought I'd post up about it in case anybody else gets stuck trying to implement this on mobile.

Getting Warning

I am getting the following warning, "No previous prototype for function SPEnsureGoogleAPIKey", into "SPGooglePlacesAutocompleteUtilities.m" class.

Can you please provide a way to remove this annoying warning. Thanks!

Removing NSURLConnection

As per new policies of Apple, we cannot use NSURLConnection in our code anymore. I am using "SPGooglePlacesAutocomplete" since 3 years in my Project. How do I update code to get rid of NSURLConnection from "SPGooglePlacesAutocompleteQuery.m" and "SPGooglePlacesPlaceDetailQuery.m" ?

Filter by country

Hi there,

Would be great to also add the "components" parameter to those Google Place API requests.
That would be useful to filter for example by country:

&components=country:fr

Cheers

How to get full street address?

Very often when I search for a business, it finds it along with the exact coordinates. However, the actual street address is missing. I was wondering if this was an option to configure ias one of searchQuery's properties but there are only two that I could find:

@property (nonatomic) SPGooglePlacesAutocompletePlaceType types;

typedef enum {
    SPPlaceTypeGeocode = 0,
    SPPlaceTypeEstablishment
} SPGooglePlacesAutocompletePlaceType;

Are there any other types that are supported or somehow to get it to return the street address?

Autosearch

I need to use google map?
Auto search?

Can't getting touch on uiview after click on uiteableview cell.

Hi,

I have used your controller but I am getting one issue in that.

When I search particular place using search bar list of all places are displaying on uitableview after selecting particular cell I am not able touch on my screen.

Do you have any idea.

Thanks
Keyur Bhalodiya

CocoaPods specification

Would it be possible to get this library turned into a CocoaPod for use with dependency management?

Thanks!

Using onlyObject with the results from geocodeAddressString is faulty

CLGeocoder geocodeAddressString: completionHandler: may return more than one placemark, which resolveEstablishmentPlaceToPlacemark and resolveGecodePlaceToPlacemark are not anticipating. They use onlyObject to get a placemark out of the results, but that will return nil if the placemarks array contains more than 1 item. The better approach is to always select the 1st placemark, regardless of how many are returned.

changes in clplacemark of resolveToPlacemark block

Due to some changes in the api, the place mark we get from
[place resolveToPlacemark:^(CLPlacemark *placemark, NSString *addressString, NSError *error)
block is different now, and there is lot of changes in placemark. so is there any other way to get the latitude and longitude from clplacemark,

i can get latitude and longitude but its too long way by dictionary to dictionary checking. my previous code which is working is following

        [place resolveToPlacemark:^(CLPlacemark *placemark, NSString *addressString, NSError *error) {
            if (error) 
              SPPresentAlertViewWithErrorAndTitle(error, @"Error while getting location");
            }
            else if (placemark)
            {
                id objtest = placemark;

                NSString *str = [NSString stringWithFormat:@"%@",objtest];
                NSArray *chunks = [str componentsSeparatedByString: @";"];

                if (chunks.count >1)
                {
                    NSString *lat = [chunks objectAtIndex:0];
                    NSString *lng = [chunks objectAtIndex:1];

                    selectedLatitude  = [[lat componentsSeparatedByString:@"\""] objectAtIndex:1];
                    selectedLongitude = [[lng componentsSeparatedByString:@"\""] objectAtIndex:1];
                }
                else{
                    selectedLatitude  = [NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];
                    selectedLongitude = [NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];
                }
                NSLog(@"selected latitude : %@",selectedLatitude);
                NSLog(@"selected longitude : %@",selectedLongitude);
            }
            else{
                NSLog(@"Anything other...");
            }
        }];
    }

NSDictionary with Addresss

Hey,

Thank you for your code.

How do I get back an NSDictionary of regions with objectForKey for:
locality
sublocality
postal_code
country
administrative_area1
administrative_area2

Your code works great; however, I need to display without the Country.

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.