自动填充地址 + Google Maps API [英] Autofill Address + Google Maps API

查看:49
本文介绍了自动填充地址 + Google Maps API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的用户展示一个文本框,他们可以在其中输入地址.当他们输入地址时,我想为用户提供他们尝试输入的地址的建议/预测.我还担心此地址的相关性(例如,该地址不是世界另一端的地址).

I want to present my users with a text box wherein they can type in their address. As they type their address, I want to provide the users with suggestions/predictions of what address they are trying to type. I'm also concerned about the relevance of this address (e.g. that the address is not an address halfway across the world).

这可能吗?我正在使用 jQuery.

Is this possible? I'm using jQuery.

推荐答案

您可以使用 Google Places API 来自动完成地址.选择地址时,address_components 字段包含结构化地址数据(例如街道、城市、国家/地区),并且可以轻松转换为单独的字段.

You can use Google Places API to have an autocomplete for address. When address is selected, address_components field contains structured address data (e.g. street, city, country) and could be easily converted into separate fields.

这是一个简短的工作演示:

Here is a short working demo:

<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    </head>
    <body>
        <input type="text" id="address" style="width: 500px;"></input>

        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en-AU"></script>
        <script>
            var autocomplete = new google.maps.places.Autocomplete($("#address")[0], {});

            google.maps.event.addListener(autocomplete, 'place_changed', function() {
                var place = autocomplete.getPlace();
                console.log(place.address_components);
            });
        </script>
    </body>
</html>

这篇关于自动填充地址 + Google Maps API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆