Output town/city in input rather than full place using Places Autocomplete












1















Currently, I am using regions for my places autocomplete input. I previously used cities before regions as this outputted the town/city in the input.



But now I want to be able to input the postcode or town/city and still have the output as just the town/city.



Basically, I want to use regions but have the output of cities into the input element.



Examples below:



Input: London
Actual Output: London, UK (This is fine)
Desired Output: London, UK

Input: SW1A
Actual Output: London SW1A, UK (This is not fine)
Desired Output: London, UK

Input: NW3 7JR
Actual Output: Hampstead Lane, London NW3 7JR, UK (This is not fine)
Desired Output: London, UK


The Javascript currently using:



//Function for Places Autocomplete
function activatePlacesSearch(){
var input = document.getElementById('id_local_area');
var options = {
types: ['(regions)'],
componentRestrictions: {country: 'gb'}};
var autocomplete = new google.maps.places.Autocomplete(input, options);
}









share|improve this question



























    1















    Currently, I am using regions for my places autocomplete input. I previously used cities before regions as this outputted the town/city in the input.



    But now I want to be able to input the postcode or town/city and still have the output as just the town/city.



    Basically, I want to use regions but have the output of cities into the input element.



    Examples below:



    Input: London
    Actual Output: London, UK (This is fine)
    Desired Output: London, UK

    Input: SW1A
    Actual Output: London SW1A, UK (This is not fine)
    Desired Output: London, UK

    Input: NW3 7JR
    Actual Output: Hampstead Lane, London NW3 7JR, UK (This is not fine)
    Desired Output: London, UK


    The Javascript currently using:



    //Function for Places Autocomplete
    function activatePlacesSearch(){
    var input = document.getElementById('id_local_area');
    var options = {
    types: ['(regions)'],
    componentRestrictions: {country: 'gb'}};
    var autocomplete = new google.maps.places.Autocomplete(input, options);
    }









    share|improve this question

























      1












      1








      1








      Currently, I am using regions for my places autocomplete input. I previously used cities before regions as this outputted the town/city in the input.



      But now I want to be able to input the postcode or town/city and still have the output as just the town/city.



      Basically, I want to use regions but have the output of cities into the input element.



      Examples below:



      Input: London
      Actual Output: London, UK (This is fine)
      Desired Output: London, UK

      Input: SW1A
      Actual Output: London SW1A, UK (This is not fine)
      Desired Output: London, UK

      Input: NW3 7JR
      Actual Output: Hampstead Lane, London NW3 7JR, UK (This is not fine)
      Desired Output: London, UK


      The Javascript currently using:



      //Function for Places Autocomplete
      function activatePlacesSearch(){
      var input = document.getElementById('id_local_area');
      var options = {
      types: ['(regions)'],
      componentRestrictions: {country: 'gb'}};
      var autocomplete = new google.maps.places.Autocomplete(input, options);
      }









      share|improve this question














      Currently, I am using regions for my places autocomplete input. I previously used cities before regions as this outputted the town/city in the input.



      But now I want to be able to input the postcode or town/city and still have the output as just the town/city.



      Basically, I want to use regions but have the output of cities into the input element.



      Examples below:



      Input: London
      Actual Output: London, UK (This is fine)
      Desired Output: London, UK

      Input: SW1A
      Actual Output: London SW1A, UK (This is not fine)
      Desired Output: London, UK

      Input: NW3 7JR
      Actual Output: Hampstead Lane, London NW3 7JR, UK (This is not fine)
      Desired Output: London, UK


      The Javascript currently using:



      //Function for Places Autocomplete
      function activatePlacesSearch(){
      var input = document.getElementById('id_local_area');
      var options = {
      types: ['(regions)'],
      componentRestrictions: {country: 'gb'}};
      var autocomplete = new google.maps.places.Autocomplete(input, options);
      }






      javascript google-maps google-api google-places-api googleplacesautocomplete






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 15:10









      alextford11alextford11

      1049




      1049
























          1 Answer
          1






          active

          oldest

          votes


















          0














          In short, this will never happen.



          Place Autocomplete will always echo the input back to the user, so if part of a postal code is typed, at least that part of the postal code will be part of the prediction. With the types= parameter you can restrict predictions, but in the examples above that will only cause no predictions to be returned.



          There are a few things you can do:




          1. Take the structured_formatting.secondary_text which, while not always exactly the "London, UK" level you want, it is always available on the Place Autocomplete response, so no need to do even a Details request for the Place ID.

          2. Request details for this Place ID (from either Geocoding API or Place Details) and reverse geocode its location latlng in the Geocoding API. That will give you all political features that contain the latlng. Then you need to be careful about which one represents the "city" or whatever it is you want. See explanation about this in https://google-developers.appspot.com/maps/documentation/javascript/examples/places-autocomplete-addressform






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433818%2foutput-town-city-in-input-rather-than-full-place-using-places-autocomplete%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            In short, this will never happen.



            Place Autocomplete will always echo the input back to the user, so if part of a postal code is typed, at least that part of the postal code will be part of the prediction. With the types= parameter you can restrict predictions, but in the examples above that will only cause no predictions to be returned.



            There are a few things you can do:




            1. Take the structured_formatting.secondary_text which, while not always exactly the "London, UK" level you want, it is always available on the Place Autocomplete response, so no need to do even a Details request for the Place ID.

            2. Request details for this Place ID (from either Geocoding API or Place Details) and reverse geocode its location latlng in the Geocoding API. That will give you all political features that contain the latlng. Then you need to be careful about which one represents the "city" or whatever it is you want. See explanation about this in https://google-developers.appspot.com/maps/documentation/javascript/examples/places-autocomplete-addressform






            share|improve this answer




























              0














              In short, this will never happen.



              Place Autocomplete will always echo the input back to the user, so if part of a postal code is typed, at least that part of the postal code will be part of the prediction. With the types= parameter you can restrict predictions, but in the examples above that will only cause no predictions to be returned.



              There are a few things you can do:




              1. Take the structured_formatting.secondary_text which, while not always exactly the "London, UK" level you want, it is always available on the Place Autocomplete response, so no need to do even a Details request for the Place ID.

              2. Request details for this Place ID (from either Geocoding API or Place Details) and reverse geocode its location latlng in the Geocoding API. That will give you all political features that contain the latlng. Then you need to be careful about which one represents the "city" or whatever it is you want. See explanation about this in https://google-developers.appspot.com/maps/documentation/javascript/examples/places-autocomplete-addressform






              share|improve this answer


























                0












                0








                0







                In short, this will never happen.



                Place Autocomplete will always echo the input back to the user, so if part of a postal code is typed, at least that part of the postal code will be part of the prediction. With the types= parameter you can restrict predictions, but in the examples above that will only cause no predictions to be returned.



                There are a few things you can do:




                1. Take the structured_formatting.secondary_text which, while not always exactly the "London, UK" level you want, it is always available on the Place Autocomplete response, so no need to do even a Details request for the Place ID.

                2. Request details for this Place ID (from either Geocoding API or Place Details) and reverse geocode its location latlng in the Geocoding API. That will give you all political features that contain the latlng. Then you need to be careful about which one represents the "city" or whatever it is you want. See explanation about this in https://google-developers.appspot.com/maps/documentation/javascript/examples/places-autocomplete-addressform






                share|improve this answer













                In short, this will never happen.



                Place Autocomplete will always echo the input back to the user, so if part of a postal code is typed, at least that part of the postal code will be part of the prediction. With the types= parameter you can restrict predictions, but in the examples above that will only cause no predictions to be returned.



                There are a few things you can do:




                1. Take the structured_formatting.secondary_text which, while not always exactly the "London, UK" level you want, it is always available on the Place Autocomplete response, so no need to do even a Details request for the Place ID.

                2. Request details for this Place ID (from either Geocoding API or Place Details) and reverse geocode its location latlng in the Geocoding API. That will give you all political features that contain the latlng. Then you need to be careful about which one represents the "city" or whatever it is you want. See explanation about this in https://google-developers.appspot.com/maps/documentation/javascript/examples/places-autocomplete-addressform







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 29 '18 at 12:03









                miguevmiguev

                3,6211135




                3,6211135






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433818%2foutput-town-city-in-input-rather-than-full-place-using-places-autocomplete%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen