【问题标题】:AngularJS & Google Maps API V3 Cannot Geocode an Address in a LoopAngularJS 和 Google Maps API V3 无法在循环中对地址进行地理编码
【发布时间】:2014-02-09 14:14:16
【问题描述】:

我正在尝试为 AngularJS 范围内的每个标记进行地理编码,可以使用 $scope.locations 获取这些标记

我不断收到以下错误TypeError: Cannot call method 'geocode' of undefined

我在文件顶部设置了var geocoder, geocode, geocode_results;

在一个例子中,$scope.location 在我 console.log 时有以下数据。

$$hashKey: "009"    
desc: "Closest test"
gps: "ab42 2dl"
title: "A place"

我用来尝试遍历位置循环的代码如下。

$scope.plotmarkers = function() {
    console.log($scope);
//          var temp_addresses = document.getElementById("gps").value.split("\n");
            var temp_addresses = $scope.locations;
            console.log(temp_addresses);

            for(var i=0;i<temp_addresses.length;i++){
        //      addresses.push(temp_addresses[i]);
console.log(temp_addresses[i].gps);
                geocoder.geocode( { 'address': temp_addresses[i].gps}, function(response, status) {
                    geocode_results[i] = new Array();
                    geocode_results[i]['status'] = status;
console.log('2')
                    if (!response || status != google.maps.GeocoderStatus.OK) {
                        if(status == google.maps.GeocoderStatus.ZERO_RESULTS){
                            geocode_results[i]['lat'] = 0;
                            geocode_results[i]['lng'] = 0;
                        } else {
                            timeouts++;
                            if(timeouts>6){
                                alert("You have reached the limit of of requests that you can make to google from this IP address in one day.");
                            }
                        }
                    } else {
                        timeouts = 0;
                        top_location = response[0];
                        var lat = Math.round(top_location.geometry.location.lat() * 1000000)/1000000;
                        var lng = Math.round(top_location.geometry.location.lng() * 1000000)/1000000;
                        geocode_results[i]['lat'] = lat;
                        geocode_results[i]['lng'] = lng;
                        geocode_results[i]['l_type'] = top_location.geometry.location_type;
                        marker = new google.maps.Marker({
                            icon: mapIcon,
                            position: new google.maps.LatLng(lat,lng),
                            map: map
                        });
                        arrayLocation.push(top_location.address_components[0].long_name);
        //              console.log(top_location.address_components[0].long_name);
                    }
                });
            }

    };

【问题讨论】:

  • 你是如何初始化地理编码器的?如果在 plotmarkers 中调用 geocode 时,它​​看起来好像是未定义的。

标签: javascript angularjs google-maps-api-3 for-loop


【解决方案1】:

我错过了

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

【讨论】:

    猜你喜欢
    • 2013-10-05
    • 2013-01-03
    • 1970-01-01
    • 2013-12-18
    • 2013-04-05
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 2013-06-27
    相关资源
    最近更新 更多