【问题标题】:Get iso code using reverse lookup with google api使用谷歌 api 反向查找获取 iso 代码
【发布时间】: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


    【解决方案1】:

    您有 2 个} 放错了位置:一个丢失了一个没有必要:

        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;
                            }
                        }
                    }
                } 
            });
        }
    
    ///}
    

    更新:更改函数showPosition() 以记录长短国家名称:

    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 types = results[0].address_components[i].types;
    
                        for (var typeIdx = 0; typeIdx < types.length; typeIdx++) {
                            if (types[typeIdx] == 'country') {
                                console.log(results[0].address_components[i].long_name);
                                console.log(results[0].address_components[i].short_name);
                            }
                        }
                    }
                }
            } 
        });
    }
    

    【讨论】:

    • 太好了,谢谢。我有点希望这段代码能以某种方式给我一些输出数据,但我不知道该怎么做。我认为 country = short_name 很容易通过 console.log(country) 输出,但它没有显示任何内容。例如,我如何让这个脚本通过控制台日志向我显示国家代码?
    • 对于我的地方,我得到了 results[0].address_components[3].short_name。你必须检查types'country'。
    猜你喜欢
    • 2018-05-10
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多