【发布时间】:2014-02-16 17:15:57
【问题描述】:
我在这篇文章中找到了一些代码:How can I get the country and region code using geolocation instead of full country and region names
我想获取用户 iso 国家代码(2 位)。
但我不知道为什么它不起作用?在 chrome 上的控制台中,我在第 40 行得到“;uncaught SyntaxError: Unexpected token ;”
jsfiddle:http://jsfiddle.net/M9kzT/
<script>
var region = "";
var country = "";
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,noGeolocation);
} else {
console.log('nope');
}
}
function showPosition(position)
{
var geocoder = new google.maps.Geocoder();
var latlong = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
geocoder.geocode({'latLng': latlong}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
for (var i = 0; i < results[0].address_components.length; i++)
{
var longname = results[0].address_components[i].long_name;
var type = results[0].address_components[i].types;
if (type.indexOf("administrative_area_level_1") != -1)
{
region = longname;
}
if (type.indexOf("country") != -1)
{
country = short_name;
}
}
}
});
}
}
function noGeolocation()
{
console.log('nope2');
}
getLocation();
</script>
【问题讨论】:
标签: javascript jquery geolocation