2013-02-27 58 views
7

我目前正在爲我們正在處理的網站構建「搜索區域」功能。有點像Rightmove如何使用Google Maps API跟蹤事件對多邊形的更改

除了跟蹤事件對多邊形變化(設置新點和改變現有點)的能力之外,我已經啓動了所有功能。我需要能夠將提交的座標發佈到表單中。

我試過谷歌代碼文檔editing events。每次我嘗試它時,我都會收到一條關於'set_at'的消息不可能或者我的對象沒有被定義。

我想我知道有點不對是沒有穿過新google.maps.event.addListener(thePolygon, 'set_at', function() { // grab paths for infoWindow grabPaths(thePath); });

thePolygon變量,但我不知道爲什麼。這是一個全球變量。或者不是?

所以,問題是,如何跟蹤多邊形的更改以將更新的座標傳遞到我的表單?

非常感謝所有幫助。

這裏是我目前擁有的代碼:

var mapOptions = { 
    // options 
}; 

var map = new google.maps.Map(document.getElementById('map'), mapOptions); 

var drawingManager = new google.maps.drawing.DrawingManager({ 
    drawingMode: google.maps.drawing.OverlayType.POLYGON, 
    drawingControl: false, 
    polygonOptions: { 
    // drawing options 
    } 
}); 

drawingManager.setMap(map); 

google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) { 
    // complete functions 
}); 

google.maps.event.addListener(thePolygon, 'set_at', function() { 
    // complete functions 
}); 

google.maps.event.addListener(thePolygon, 'insert_at', function() { 
    // complete functions 
}); 

回答

22

這些事件爲MVCArray,因此定義,折線是不是都是MVCArray。您必須觀察折線(這是一個MVCArray)的path的事件。此外,您不能將偵聽器添加到尚未創建的對象。多邊形不polygoncomplete之前,所以你必須添加偵聽器的polygoncomplete回調中:

google.maps.event.addListener(drawingManager,'polygoncomplete',function(polygon) { 

    // complete functions 

    google.maps.event.addListener(thePath, 'set_at', function() { 
    // complete functions 
    }); 

    google.maps.event.addListener(thePath, 'insert_at', function() { 
    // complete functions 
    }); 

}); 
+0

哈哈,作品一種享受。我很親密,但迄今爲止。謝謝你的幫助。非常感謝。這是抓住了我無法理解的路徑變化。但它非常簡單。 – 2013-03-01 10:14:39

+0

請參閱http://stackoverflow.com/questions/12515748/event-after-modifying-polygon-in-google-maps-api-v3 – jjwdesign 2013-12-17 15:19:34

+5

使用'polygon.getPath()'而不是'thePath' – 2013-12-19 13:25:07

相關問題