反向地理编码回调函数

时间:2014-05-02 17:17:59

标签: javascript jquery google-maps geocoding reverse-geocoding

我遇到了反向地理编码的问题。

这是地图的来源:

<script type="text/javascript" charset="UTF-8" src="https://maps.gstatic.com/cat_js/intl/it_it/mapfiles/api-3/15/6/%7Bcommon,util,geocoder%7D.js"></script>
<script type="text/javascript" charset="UTF-8" src="https://maps.gstatic.com/cat_js/intl/it_it/mapfiles/api-3/15/6/%7Bstats%7D.js"></script>

这是反向地理编码的代码:

$(document).ready(function() {

if(!navigator.geolocation) { alert("La geolocalizzazione non è possibile"); };

navigator.geolocation.getCurrentPosition(function(pos) {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude);
    geocoder.geocode({'latLng': latlng}, function(results,status) {
                   //alert("test");
        if (status == google.maps.GeocoderStatus.OK)  {
            var result = results[0];
            var city = "";
            var state = "";
            for(var i=0, len=result.address_components.length; i<len; i++) {
                var ac = result.address_components[i];
                if(ac.types.indexOf("locality") >= 0) city = ac.long_name;
                if(ac.types.indexOf("administrative_area_level_1") >= 0) state = ac.long_name;
            } 

            var time = new Date();
            var ora = time.getHours();
            var saluto;

            if (ora < 12) { saluto = "Buongiorno"; } else { saluto = "Buonasera"; };
            if(city != '' && state != '') {
                $("#main-geolocation").html(saluto + ",ci stai visitando da "+ city +", "+ state +"!");
            }
        } else { alert ("geocoding non riuscito");}
    }); 
});
});

现在,如果我尝试启动脚本警报&#34;测试&#34; doesen不会出现在屏幕上。当脚本运行时,不要输入地理编码的回调函数,任何人都有遇到此错误的经验?因为它是1小时我试图在代码中找到错误,但我没有找到任何东西o.O 或者谷歌的新api或方法使用反向地理? 大家都想帮助我

0 个答案:

没有答案