Search code examples
iosobjective-cmkmapviewgoogle-places-apimkannotation

How to use Google Place API for mapview seaching Place


I want to use Google Place API to find near by place using searchbar.

I am performing a demo in which I want that if I write any place name in search bar it will suggest list of near by places and I can select one of them from suggestion list. When I select the place my custom pin goes to selected place and I get attitude and longitude of that place.

Now my question is how to make search bar give suggestion of nearby place ? How can I move my pin to selected place and also gel lat long of that place ?

Any demo or suggestion are welcome. Please help me if you can.


Solution

  • I am using this (find below) api to search get places suggestion from google.Just Create a api key from google console and enable google places api.I think It is not free but 1000 calls per day is free.

    Autocomplete API:-

    https://maps.googleapis.com/maps/api/place/autocomplete/json?key=api_key&input=text_to_search

    NearBy API:-

    https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=YOUR_API_KEY

    This autocomplete place api of google will provide you suggestions on the basis of text you will provide to it.

    """"""""""""""""""""

    To show search result on the basis of given json by api, I am using tableview when data comes I reload the table.

    For searching I am using textfield not search bar.By the help of delegate method of textfield I achieve text change in textfield and call to api for result as text is updates in textfield.

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
     {
        NSString *stringWithformat = [textField.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
        [self CallToPlaceAPI:stringWithformat];
        return YES;
     }
    

    Use table just below the textfield to show result of autocomplete suggestions by given API result.

    I am using google map integration in ios app.When you will find a location with the help of search use this code to move map to that location.

    Trick:- add a static pin in middle of view in which your google map is added.Google map will move to location which find by search.Google map will automatically show the search location in middle of map.So it will be under your static pin.Use this code of google map to move map.

     -(void)moveMapToLatLong 
     {
            cameraPosition = [GMSCameraPosition cameraWithLatitude:latitude longitude:longitude zoom:12];
    
            mapViewmain.camera = cameraPosition;
      }
    

    By this way I am running good. Thanks