获取用户的地理位置(zipcode)并在页面加载期间显示它 [英] Fetch the user's geo location (zipcode) and display it during page load

查看:54
本文介绍了获取用户的地理位置(zipcode)并在页面加载期间显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取用户的地理位置(ZipCode)并在页面加载期间显示它。目前我通过2个帖子实现了这个目标,有没有办法在一个帖子中实现。



谢谢



我尝试过:



函数ShowMessages(){

if(navigator。地理定位){

navigator.geolocation.getCurrentPosition(成功);

}否则{

alert(当前不支持地理位置)浏览器!);

}

功能成功(位置){

调试器;

var lat = position .coords.latitude;

var lng = position.coords.longitude;

var latlng = new google.maps.LatLng(lat,lng);

var geocoder = geocoder = new google.maps.Geocoder();

geocoder.geocode({'latLng':latlng},function(results,status){

if(status == google.maps.GeocoderStatus.O K){

if(results [1]){



var searchAddressComponents = results [0] .address_components,

searchPostalCode =;

$ .each(searchAddressComponents,function(){

if(this.types [0] ==postal_code){

searchPostalCode = this.short_name;

}

});



文件.getElementById('hidden1')。value = searchPostalCode

__doPostBack('','');





}

}

});

}



}

I need to fetch the user's geo location (ZipCode) and display it during page load. Currently I am achieving this by 2 post backs, is there any way to achieve within a single post back.

Thanks

What I have tried:

function ShowMessages() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
} else {
alert("Geo Location is not supported on your current browser!");
}
function success(position) {
debugger;
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var latlng = new google.maps.LatLng(lat, lng);
var geocoder = geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {

var searchAddressComponents = results[0].address_components,
searchPostalCode = "";
$.each(searchAddressComponents, function () {
if (this.types[0] == "postal_code") {
searchPostalCode = this.short_name;
}
});

document.getElementById('hidden1').value = searchPostalCode
__doPostBack('', '');


}
}
});
}

}

推荐答案

.each(searchAddressComponents,function(){

if(this.types [0] ==postal_code){

searchPostalCode = this.short_name;

}

}); < br $>


document.getElementById('hidden1')。value = searchPostalCode

__doPostBack('','');





}

}

});

}



}
.each(searchAddressComponents, function () {
if (this.types[0] == "postal_code") {
searchPostalCode = this.short_name;
}
});

document.getElementById('hidden1').value = searchPostalCode
__doPostBack('', '');


}
}
});
}

}


这篇关于获取用户的地理位置(zipcode)并在页面加载期间显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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