绘制多边形时出现Google Maps API问题

时间:2010-07-11 09:54:59

标签: javascript google-maps

所以,当我使用谷歌地图API绘制一个多边形时,最奇怪的事情发生在:每次点击,我用当前设定点的位置更新textarea,当我,我想说,绘制一个完整的形状,但想要点击在它里面,整个形状消失了,它没有看到我点击谷歌坐标,造成致命错误。如果我点击它旁边,形状将重新出现,但在此之后点击的所有点都不会因为错误而起作用。

有关于此的任何想法吗?

代码:

<script type="text/javascript">
if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(44.435251, 26.102829), 13);
    map.setUIToDefault();
    GEvent.addListener(map, "click", mapClick);
    geocoder = new GClientGeocoder();
}

// mapClick - Handles the event of a user clicking anywhere on the map
// Adds a new point to the map and draws either a new line from the last point
// or a new polygon section depending on the drawing mode.
function mapClick(marker, clickedPoint) {
    polygonMode = true;

    // Push onto polypoints of existing polygon
    polyPoints.push(clickedPoint);
    drawCoordinates();
}

function drawCoordinates(){
    //Re-create Polyline/Polygon
    if (polygonMode) {
        polyShape = new GPolygon(polyPoints,lineColor,lineWeight,opacity,fillColor,opacity);
    } else {
        polyShape = new GPolyline(polyPoints,lineColor,lineWeight,opacity);
    }
    map.clearOverlays();

    // Grab last point of polyPoints to add marker
    marker = new GMarker(polyPoints[polyPoints.length -1]);
    map.addOverlay(marker);
    map.addOverlay(polyShape);
    logCoordinates();
}


function logCoordinates(){

    var header = "";

    // check mode
    if (polygonMode){
        header += "" + "";

        var footer = "";

        // print coords header
        document.getElementById("coords").value =  header;

        // loop to print coords
        for (var i = 0; i<(polyPoints.length); i++) {
            var lat = polyPoints[i].lat();
            var longi = polyPoints[i].lng();
            document.getElementById("coords").value += longi + ", " + lat + " ;";
        }
    } else {
        alert('error in google maps /zone - invalid draw mode')
    }
    document.getElementById("coords").value +=  footer;
}

由于

0 个答案:

没有答案