单击按钮,在脚本中停止功能

时间:2014-02-10 21:03:29

标签: jquery function

有没有办法停止功能? 按钮启动功能..如何停止或禁用/结束此功能? 使用此代码将崩溃Firefox  我会切换Drwingmanager 更改为完整代码:

     navigator.geolocation.getCurrentPosition(function (position) {
        var latx = position.coords.latitude
        var lngy = position.coords.longitude;
        var newPoint = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        var element = document.getElementById("mapdbk");
        var directionsService = new google.maps.DirectionsService();
        var directionsDisplay = new google.maps.DirectionsRenderer();
        var geocoder;

        var drawingManager;
        var selectedShape;


        var mapTypeIds = [];
        for (var type in google.maps.MapTypeId) {
            mapTypeIds.push(google.maps.MapTypeId[type]);
        }
        mapTypeIds.push("OSM");



        var map = new google.maps.Map(element, {
            center: new google.maps.LatLng(latx, lngy),
            zoom: 16,
            mapTypeId: "OSM",
            panControl: true,
            zoomControl: true,
            mapTypeControl: true,
            scaleControl: true,
            streetViewControl: true,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                position: google.maps.ControlPosition.TOP_LEFT,
                mapTypeIds: mapTypeIds
            }
        });






        $('#zoomOMS').click(function () {
            var newcenterx = $('#selomsx').val();
            var newcentery = $('#selomsy').val();
            var panXY = new google.maps.LatLng(newcenterx, newcentery);
            map.panTo(panXY);
        });


        map.mapTypes.set("OSM", new google.maps.ImageMapType({
            getTileUrl: function (coord, zoom) {
                return "http://tile.openstreetmap.org/" + zoom + "/" + coord.x + "/" + coord.y + ".png";
            },
            tileSize: new google.maps.Size(256, 256),
            name: "EddictT.com",
            maxZoom: 21
        }));
        $.ajax({
            type: 'GET',
            url: '/api/OMSActive/',
            dataType: 'json'
        }).done(function (citizens) {

            $.each(citizens, function (i, c) {
                var locations = [
[c.OmsObjectName, c.OmsXcoord, c.OmsYcoord],
 ]; //leave this here
                var image = { url: '/Content/Marker/' + c.IconId + '.png' }; // When filname saved in DB ' + c.IconId + '.png' add this
                var infowindow = new google.maps.InfoWindow();
                var marker, j;
                var markers = new Array();
                for (j = 0; j < locations.length; j++) {
                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng(locations[j][1], locations[j][2]),
                        map: map,
                        icon: image
                    });

                    markers.push(marker);
                    google.maps.event.addListener(marker, 'click', (function (marker, j) {
                        return function () {
                            infowindow.setContent(locations[j][0]);
                            infowindow.open(map, marker);
                        }
                    })(marker, j));
                }
            });
        }); 

            // MArker
        function drawmarker() {
            while (continueMarker)
            var drawingManager = new google.maps.drawing.DrawingManager({
                //    drawingMode: google.maps.drawing.OverlayType.POLYGON,
                drawingControl: true,
                drawingControlOptions: {
                    position: google.maps.ControlPosition.TOP_CENTER,
                    //  drawingModes: [

                    drawingModes: [google.maps.drawing.OverlayType.MARKER]
                },
                markerOptions: {
                    icon: '/content/marker/dbkmarker/bmc.png',
                    draggable: true
                }
            });
            //                     google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
            drawingManager.setMap(map);

            google.maps.event.addListener(drawingManager, 'markercomplete', function (marker) {
                // markerArray.push(marker.getPosition());
                alert(marker.getPosition().lat().toFixed(6));
                drawingManager.setDrawingMode(null);
            });
        };

        function drawline() {
            while (continueLine)
            var drawingManager = new google.maps.drawing.DrawingManager({
                //    drawingMode: google.maps.drawing.OverlayType.POLYGON,
                drawingControl: true,
                drawingControlOptions: {
                    position: google.maps.ControlPosition.TOP_CENTER,
                    //  drawingModes: [

                    drawingModes: [google.maps.drawing.OverlayType.POLYLINE]
                },
                polygonOptions: {
                    editable: true,
                    draggable: true,
                    fillColor: '#9B0000',
                    fillOpacity: 0.5,
                    strokeColor: '#9B0000',
                    strokeOpacity: 0.8,
                    strokeWeight: 2
                }
            });
            //                     google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
            drawingManager.setMap(map);

            google.maps.event.addListener(drawingManager, 'polylinecomplete', function (line) {
                alert(line.getPath().getArray().toString());
                drawingManager.setDrawingMode(null);
            });
        };

        $("#linebut").click(function () {
            continueMarker = false;
            drawline();
            continueMarker = true;
        });

        //I assume this is a different button and the originally posted code is a typo
        $("#markerbut").click(function () {
            continueLine = false;
            drawmarker();
            continueLine = true;
        });

1 个答案:

答案 0 :(得分:0)

这是代码,虽然您应该将其视为未经测试的伪代码。

var continueLine = true;
var continueMarker = true;

function drawline() {
    while(continueLine)
    {
        //follow mouse pointer to next pixel and draw pencil to it
    }
}

function drawmarker() {
    while(continueMarker)
    {
        //follow mouse pointer to next pixel and draw marker to it
    }
}

$("#linebut").click(function  () {
    continueMarker = false;
    drawline();
    continueMarker = true;
});

//I assume this is a different button and the originally posted code is a typo
$("#markerbut").click(function () {  
    continueLine = false;
    drawmarker();
    continueLine = true;
});