AngularJS和Google Maps API v3无法对循环中的地址进行地理编码



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

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

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

在示例中,当我控制台时$scope.location具有以下数据.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);
                    }
                });
            }
    };

我失踪了

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

最新更新