2010-10-22 41 views
0

你好,我不明白爲什麼我得到這個錯誤。'myFunction不是函數',JS和Google Maps v 3?

我有一個輸入列表,其中包含緯度/長/名稱/地址的輸入。

在地圖上打印標記時工作正常,使用infowin workED罰款,直到我意識到它只在所有窗口中打開相同的信息。很明顯,我需要附加this(我剛剛點擊的標記)到eventListner。

但是,當這樣做時,我得到一個錯誤,說view.createMarkerHTML is not a function

問題:我應該如何在正確的標記上附上要打開的正確信息?

view = { 
     map: { 
      init: function init(){ 
       view.map.initMap(); 
      }, 

      initMap: function initMap() { 
       if(navigator.geolocation) { 
        function hasPosition(position) { 
         var point = new google.maps.LatLng(position.coords.latitude, position.coords.longitude),  
         myOptions = { 
          zoom: 12, 
          center: point, 
          mapTypeId: google.maps.MapTypeId.ROADMAP 
         }, 
         mapDiv = document.getElementById("map"), 
         map = new google.maps.Map(mapDiv, myOptions);       
         view.map.handleMarkers(map); 

        }      
        navigator.geolocation.getCurrentPosition(hasPosition); 

       } 
      }, 


      handleMarkers: function handleMarkers(map) { 
       var list = $('#pois input'), 
        l = list.length, 
        i = 0, lat = '', long = '', marker = {}, theName = '', address = '', info = {}; 



       for (i = 0; i < l; i += 1) { 
        info = $(list[i]).val().split('|'); 
        lat = parseFloat(info[0], 10); 
        long = parseFloat(info[1], 10); 
        theName = info[2]; 
        address = info[3]; 

        marker = new google.maps.Marker({ 
         position: new google.maps.LatLng(lat, long), 
         map: map, 
         icon: icon 
        }); 



        google.maps.event.addListener(marker, 'click', function() { 
         currentMarker = this;view.map.createMarkerHTML(map, theName, address); 
        });     
       } 
      } , 

      createMarkerHTML: function createMarkerHTML(map, theName, address) { 

       var contentString = 
        '<div id="gMapInfoWin">' + 
         '<h1>' + theName + '</h1>' + 
         '<ul>' + 
          '<li>' + address + '</li>' + 
         '</ul>' + 
        '</div>' 
       ; 

       currentMarker.infowindow = new google.maps.InfoWindow({ 
        content: contentString 
       }); 

       currentMarker.infowindow.open(map, currentMarker); 
      } 
     } 
}; 
+0

我沒有在任何地方看到'view',這個問題很難回答。對於踢,雖然,嘗試'view.map.createMarkerHTML(地圖,theName,地址);' – 2010-10-22 09:35:45

+0

尼克,你應該張貼作爲答案,因爲這是答案。 – patad 2010-10-22 09:44:29

+0

當然它是view.map.function()我完全忘記了地圖對象。嘿嘿,盯着自己的代碼很容易。非常感謝。 – patad 2010-10-22 09:45:45

回答

0
for (i = 0; i < l; i += 1) { 
        info = $(list[i]).val().split('|'); 
        lat = parseFloat(info[0], 10); 
        long = parseFloat(info[1], 10); 

        marker = new google.maps.Marker({ 
         position: new google.maps.LatLng(lat, long), 
         map: map, 
         icon: icon 
        }); 

        marker.theName = info[2]; 
        marker.address = info[3]; 

        google.maps.event.addListener(marker, 'click', function() { 
         currentMarker = this; 
         m_name = this.theName;  
         m_address = this.address   
         view.map.createMarkerHTML(map, m_name, m_address); 
        });     
       } 

marker.theName =信息[2];
marker.address = info [3];
currentMarker = this;
m_name = this.theName;
m_address = this.address

...是營養!

1

不應該使用閉包來保持變量的值在循環中設置嗎?

(function(theName, address) { 
    google.maps.event.addListener(marker, 'click', function() { 
     currentMarker = this;view.map.createMarkerHTML(map, theName, address); 
    }); 
}(theName, address)); 

你甚至可以添加你的標誌參數,而不是使用currentMarker

+0

部分是一個封閉,但由於反正 – patad 2010-10-22 14:47:29

+0

嗯......但即使這個代碼是在一個閉包中......我想你需要另一個來保存變量'theName'和'address'的值。你試過了嗎?使用google.maps.event.addListener( – 2010-10-22 16:19:49

0

我敢打賭,這是一個範圍界定問題。由於沒有訪問您的庫我不能肯定,這將工作,但嘗試更換:

google.maps.event.addListener(marker, 'click', function() { 
    currentMarker = this;view.map.createMarkerHTML(map, theName, address); 
}); 

與此:

google.maps.event.addListener(marker, 
           'click', 
           createClickListener( this, 
                 map, 
                 theName, 
                 address)); 

,然後在程序中其他地方添加這個功能:

function createClickListener(cm, map, theName, address) 
{ 
    return function() { 
     currentMarker = cm; 
     view.map.createMarkerHTML(map, theName, address); 
    } 
} 
+0

感謝,但IM),這是在谷歌的標準是將V 3 – patad 2010-10-22 14:47:02

+0

好,但這並不意味着它不能成爲一個作用域的問題。 – cwallenpoole 2010-10-23 16:22:03

相關問題