2014-02-09 90 views
0

我試圖進行地址解析爲每一個標記在AngularJS範圍可以通過使用$scope.locationsAngularJS與谷歌地圖API V3無法地理編碼的地址在循環

拾起我不斷收到以下錯誤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); 
        } 
       }); 
      } 

    }; 
+0

你是如何初始化地理編碼?如果您在plotmarkers中調用地理編碼時看起來好像尚未定義。 –

回答

0

我失蹤

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