谷歌无地图地址自动完成Google Places AutoComplete example without using Maps

In this article I will explain with an example, how to implement the Google Places Autocomplete without using Google Maps.
 
In addition to this article will also explain how to use the Place changed event handler of the Google Autocomplete TextBox to get the selected place, its address and its location coordinates i.e. Latitude and Longitude.
 
Implementing Google Places Autocomplete without using Google Maps
The HTML Markup consists of an HTML TextBox using which the Google Places Autocomplete will be implemented.
The very first thing is to inherit the Google Maps API script along with the places library. Then inside the Google Maps window load event handler, the Google Places Autocomplete is applied to the TextBox and then the place_changed event handler is assigned to it.
 
The place_changed event handler is triggered when a place is selected in the Google Places Autocomplete TextBox. It first gets the selected place and then determines its address and its location coordinates i.e. Latitude and Longitude and then displays the details in JavaScript alert message box.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
    <script type="text/javascript">
        google.maps.event.addDomListener(window, 'load', function () {
            var places = new google.maps.places.Autocomplete(document.getElementById('txtPlaces'));
            google.maps.event.addListener(places, 'place_changed', function () {
                var place = places.getPlace();
                var address = place.formatted_address;
                var latitude = place.geometry.location.A;
                var longitude = place.geometry.location.F;
                var mesg = "Address: " + address;
                mesg += "nLatitude: " + latitude;
                mesg += "nLongitude: " + longitude;
                alert(mesg);
            });
        });
    </script>
    <span>Location:</span>
    <input type="text" id="txtPlaces" style="width: 250px" placeholder="Enter a location" />
</body>
</html>
 
Google Autocomplete TextBox displaying places
谷歌无地图地址自动完成Google Places AutoComplete example without using Maps
谷歌无地图地址自动完成Google Places AutoComplete example without using Maps
JavaScript alert displaying the details of the selected place
谷歌无地图地址自动完成Google Places AutoComplete example without using Maps
谷歌无地图地址自动完成Google Places AutoComplete example without using Maps

下载:Google_Places_Autocomplete

原文:http://www.aspsnippets.com/Articles/Google-Places-AutoComplete-example-without-using-Maps.aspx

转自:谷歌无地图地址自动完成Google Places AutoComplete example without using Maps

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值