<input class="flex-item" id="autocomplete" placeholder="address, zip or city" onFocus="geolocate()" type="text"></input>

var autocomplete;

function geolocate() {
if(!autocomplete){
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),{ types: ['geocode'] });
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = new google.maps.LatLng(
position.coords.latitude, position.coords.longitude);
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}

<body onload="initialize()">
<form class="form-address" id="address">

<!-- Search Field -->
<div class="flex-container c-1column" id="locationField">
<label>Search for your address</label>
<input class="flex-item" id="autocomplete" placeholder="address, zip or city" onFocus="geolocate()" type="text"></input>
</div>

<!-- Street Address -->
<div class="flex-container">
<label>Street address</label>
<div class="flex-item c-2column"><input id="streetNo" ></input></div>
<div class="flex-item c-2column"><input id="route" ></input></div>
</div>

<!-- City -->
<div class="flex-container">
<label>City</label>
<input class="flex-item c-1column" id="city" ></input>
</div>

<!-- State & Zip -->
<div class="flex-container">
<div class="flex-item c-2column">
<label>State</label>
<input id="state" ></input>
</div>
<div class="flex-item c-2column">
<label>Zip code</label>
<input id="zipcode" ></input>
</div>
</div>

<!-- Country -->
<div class="flex-container">
<label>Country</label>
<input class="flex-item c-1column" id="country" ></input>
</div>

</form>
</body>

var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
var myForm = {
street_number: 'streetNo',
route: 'route',
locality: 'city',
administrative_area_level_1: 'state',
country: 'country',
postal_code: 'zipcode'
};

function initialize() {
autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),{ types: ['geocode'] });
google.maps.event.addListener(autocomplete, 'place_changed', function() {
fillInAddress();
});
}

// [START region_fillform]
function fillInAddress() {

var place = autocomplete.getPlace();
/* for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
} */

// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
// alert(myForm[addressType])
document.getElementById(myForm[addressType]).value = val;
}
}
}

function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = new google.maps.LatLng(
position.coords.latitude, position.coords.longitude);
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}

google api autocomplete的更多相关文章

  1. 借用Google API在线生成网站二维码地址方法

    二维码其实很早就出现了,在国外很多年前就已经在应用了,国内这两年才开始异常的火爆,智能手机的发展,以及微博.微信等移动应用带动了二维码的普及.那么,如果为网址在线生成二维码呢?下面我们就来介绍一下Go ...

  2. [Java] - Google API调用

    由于Google已经完成被墙,要上Google必需使用代理或VPN. 这里使用的是Google的GoAgent代理做开发.(如何使用GoAgent,这里不写了,忽略500字.....) 本地测试的Go ...

  3. PHP二维码生成的方法(google APi,PHP类库,libqrencode等)

    原文地址: http://blog.csdn.net/liuxinmingcode/article/details/7910975 ================================== ...

  4. Google API在线生成二维码的方法

    1.先看一个实例,是用Google API生成西部e网的网站地址www.weste.net二维码的方法: http://chart.apis.google.com/chart?cht=qr&c ...

  5. 缺少google api密钥,因此chromium的部分功能将无法使用”的解决办法

            使用Chromium时会遇到 "缺少google api密钥,因此chromium的部分功能将无法使用"提示,google了一下 setx Google_API_K ...

  6. 利用google api生成二维码名片例子

    二维条码/二维码可以分为堆叠式/行排式二维条码和矩阵式二维条码.堆叠式/行排式二维条码形态上是由多行短截的一维条码堆叠而成:矩阵式二维条码以矩阵的形式组成,在矩阵相应元素位置上用“点”表示二进制“1” ...

  7. google API的.NET库

    Goolge发布了一个新的google API .NET库,是一个Portable Class Library,所以无论是.NET,WinTRy,Windows Phone或者Silverlight都 ...

  8. Google API v3 设置Icon问题处理

    1.查看API实现 //虽然比较符合API实现的思想但这个没法; //会产生Uncaught TypeError: undefined is not a function //google API n ...

  9. ecshop使用Google API及OAuth2.0登录授权(PHP)

    一.申请clientID https://console.developers.google.com/project 二.开启Google+ API权限 https://console.develop ...

随机推荐

  1. 网络HTTP协议

    WebView:在应用中嵌入一个浏览器 ...... webView = (webView)findViewById(R.id.web_view); webView.getSettings().set ...

  2. 5.对与表与表之间的关系,efcore是如何处理的

    public class Account { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Accoun ...

  3. 图解MySQL5.5详细安装与配置过程

    MySQL是一个开源的关系型数据库管理系统,原由瑞典MySQL AB公司开发,目前属于Oracle公司旗下.MySQL是目前世界上开源数据库中最受欢迎的产品之一,是应用最为广泛的开源数据库.MySQL ...

  4. 基于EasyUI的Web应用程序及过去一年的总结

    前言 在这家公司服务了一年时间,一个多月之前已经提交了离职申请,好在领导都已经批准了,过几天就办理手续了,在此感谢领导的栽培与挽留,感谢各位同事在工作中的给我的帮助,感谢师傅(在我心中当他是我师傅,跟 ...

  5. html select的事件 方法 属性

    事件 onactivate 当对象设置为活动元素时触发. onafterupdate 当成功更新数据源对象中的关联对象后在数据绑定对象上触发. onbeforeactivate 对象要被设置为当前元素 ...

  6. 关于 jquery和js获取宽度时只能取整数,取不到小数点

    最近在改版自已的一个网站的时候,遇到了一个问题. 用jquery的width()函数获取元素宽度的时候,返回得到的是整数,而不是小数. 如下图,谷歌上显示的宽度为1078.89px 而我用控制台输出了 ...

  7. 为什么不要使用Response.Close()

    不要使用Response.Close()    不要使用Response.Close()    不要使用Response.Close()        1  因为:Close()方法会调用HttpWo ...

  8. Java里面获取当前服务器的IP地址

    public static void main(String[] args) { try { InetAddress address = InetAddress.getLocalHost();//获取 ...

  9. 命名空间$.fn

    $.fn.xxx是可以用对象来调用的命名空间,例如 $.fn.input() 在声明时就可以用 $('abc').input()    $.fx是指jquery的特效. 如果使用显示.滑动.淡入淡出. ...

  10. Linux提权基础

    英文原文: Basic Linux Privilege Escalation 在开始之前,我想指出 - 我不是专家. 据我所知,在这个巨大的领域没有一个“魔法”的答案. 这只是我的发现,写出来,共享而 ...