Google Autocomplete API - format output result

后端 未结 2 1376
时光说笑
时光说笑 2021-01-20 04:22

I\'m trying to implement Google Places API, so here is my code:




    

        
2条回答
  •  隐瞒了意图╮
    2021-01-20 05:04

    I was able to solve my problem with the following code:

    $(document).ready(function() {
         var el = $('#street').attr("placeholder", "")
            , autocomplete = new google.maps.places.Autocomplete( ( el.get(0) ), { types: ['geocode'] } );
    
            el.bind("blur", function() {
              el.css("color", "#fff"); // this will prevent text flashing
            })
    
            google.maps.event.addListener(autocomplete, 'place_changed', function() {
               var place = autocomplete.getPlace();
    
                el.val(place.name);
    
                // this will override default Google suggestion
                setTimeout(function() {
                    el.val(place.name);
                    el.css("color", "");
                }, 0)
            });
    })
    

提交回复
热议问题