I'm currently attempting to geocode markers for each location in an AngularJS scope accessible through $scope.locations
Unfortunately, I keep encountering the error message
TypeError: Cannot call method 'geocode' of undefined
To address this issue, I have initialized
var geocoder, geocode, geocode_results;
at the beginning of my file
For example, when I log $scope.location
, it displays the following data:
$$hashKey: "009"
desc: "Closest test"
gps: "ab42 2dl"
title: "A place"
The snippet of code I'm using to iterate through the locations is as follows:
$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);
}
});
}
};