简体   繁体   English

GoogleMaps徒手绘制多边形的创建之前(而不是现在)有效

[英]GoogleMaps freehand polygon creation worked before, not now

I created a page to search customers by a Google map with freehand area selection 1 years ago. 我在1年前创建了一个页面,用于通过Google地图选择手绘区域来搜索客户。 I used GDouglasPeucker to do this and when I created the page, everythings work well, but now is not working and I don't understand why! 我使用GDouglasPeucker进行此操作,当我创建页面时,一切正常,但是现在不起作用,我也不明白为什么!

this is the GDouglasPeucker function: 这是GDouglasPeucker函数:

function GDouglasPeucker(source, kink)
/* source[] Input coordinates in GLatLngs   */
/* kink in metres, kinks above this depth kept  */
/* kink depth is the height of the triangle abc where a-b and b-c are two consecutive line segments */ {
var n_source, n_stack, n_dest, start, end, i, sig;
var dev_sqr, max_dev_sqr, band_sqr;
var x12, y12, d12, x13, y13, d13, x23, y23, d23;
var F = ((Math.PI / 180.0) * 0.5);
var index = new Array(); /* aray of indexes of source points to include in the reduced line */
var sig_start = new Array(); /* indices of start & end of working section */
var sig_end = new Array();

/* check for simple cases */

if (source.length < 3)
    return (source);    /* one or two points */

/* more complex case. initialize stack */

n_source = source.length;
band_sqr = kink * 360.0 / (2.0 * Math.PI * 6378137.0);  /* Now in degrees */
band_sqr *= band_sqr;
n_dest = 0;
sig_start[0] = 0;
sig_end[0] = n_source - 1;
n_stack = 1;

/* while the stack is not empty  ... */
while (n_stack > 0) {

    /* ... pop the top-most entries off the stacks */

    start = sig_start[n_stack - 1];
    end = sig_end[n_stack - 1];
    n_stack--;

    if ((end - start) > 1) {  /* any intermediate points ? */

        /* ... yes, so find most deviant intermediate point to
               either side of line joining start & end points */

        x12 = (source[end].lng() - source[start].lng());
        y12 = (source[end].lat() - source[start].lat());
        if (Math.abs(x12) > 180.0)
            x12 = 360.0 - Math.abs(x12);
        x12 *= Math.cos(F * (source[end].lat() + source[start].lat()));/* use avg lat to reduce lng */
        d12 = (x12 * x12) + (y12 * y12);

        for (i = start + 1, sig = start, max_dev_sqr = -1.0; i < end; i++) {

            x13 = (source[i].lng() - source[start].lng());
            y13 = (source[i].lat() - source[start].lat());
            if (Math.abs(x13) > 180.0)
                x13 = 360.0 - Math.abs(x13);
            x13 *= Math.cos(F * (source[i].lat() + source[start].lat()));
            d13 = (x13 * x13) + (y13 * y13);

            x23 = (source[i].lng() - source[end].lng());
            y23 = (source[i].lat() - source[end].lat());
            if (Math.abs(x23) > 180.0)
                x23 = 360.0 - Math.abs(x23);
            x23 *= Math.cos(F * (source[i].lat() + source[end].lat()));
            d23 = (x23 * x23) + (y23 * y23);

            if (d13 >= (d12 + d23))
                dev_sqr = d23;
            else if (d23 >= (d12 + d13))
                dev_sqr = d13;
            else
                dev_sqr = (x13 * y12 - y13 * x12) * (x13 * y12 - y13 * x12) / d12;// solve triangle

            if (dev_sqr > max_dev_sqr) {
                sig = i;
                max_dev_sqr = dev_sqr;
            }
        }

        if (max_dev_sqr < band_sqr) {   /* is there a sig. intermediate point ? */
            /* ... no, so transfer current start point */
            index[n_dest] = start;
            n_dest++;
        }
        else {
            /* ... yes, so push two sub-sections on stack for further processing */
            n_stack++;
            sig_start[n_stack - 1] = sig;
            sig_end[n_stack - 1] = end;
            n_stack++;
            sig_start[n_stack - 1] = start;
            sig_end[n_stack - 1] = sig;
        }
    }
    else {
        /* ... no intermediate points, so transfer current start point */
        index[n_dest] = start;
        n_dest++;
    }
}

/* transfer last point */
index[n_dest] = n_source - 1;
n_dest++;

/* make return array */
var r = new Array();
for (var i = 0; i < n_dest; i++)
    r.push(source[index[i]]);
return r;

}

and this the function that before works: 这是之前起作用的功能:

function drawFreeHand() {

        //the polygon
        poly = new google.maps.Polyline({ map: map, clickable: false });

        //move-listener
        var move = google.maps.event.addListener(map, 'mousemove', function (e) {
            poly.getPath().push(e.latLng);
        });

        //mouseup-listener

        google.maps.event.addListenerOnce(map, 'mouseup', function (e) {
            google.maps.event.removeListener(move);
            var path = poly.getPath();
            poly.setMap(null);


            var theArrayofLatLng = path.j;

            console.log(path.j);
            var ArrayforPolygontoUse = GDouglasPeucker(theArrayofLatLng, 50);
            console.log("ArrayforPolygontoUse", ArrayforPolygontoUse);

            if (poly && poly.getPaths) {
                multi_poly = poly.getPaths();
                multi_poly_path.push(ArrayforPolygontoUse);
            } else {
                multi_poly = ArrayforPolygontoUse;
            }

            var polyOptions = {
                map: map,
                //fillColor: '#0099FF',
                fillOpacity: 0.45,
                //strokeColor: '#AA2143',
                strokeWeight: 0,
                clickable: false,
                zIndex: 1,
                path: multi_poly,
                editable: false
            }

            poly = new google.maps.Polygon(polyOptions);

            setSelection(poly);
            google.maps.event.clearListeners(map, 'mousedown');
            SetNavigaMode();
            enable();

            VisualizzaControlliCustom(false);
        });
    }

the line who generates the error is poly = new google.maps.Polygon(polyOptions); 产生错误的行是poly = new google.maps.Polygon(polyOptions);

and this is the error! 这就是错误!

在此处输入图片说明

somebody can help me?? 有人可以帮助我吗?

var theArrayofLatLng = path.j;

Here j is minimized field name. 这里j是最小化的字段名称。 It is undocumented and should never be used. 它没有记录,永远不要使用。 Most probably, Google released new version of their API, and this field now has different name, and it will change again in future. Google最有可能发布了他们API的新版本,并且此字段现在具有不同的名称,并且将来会再次更改。

You should use var theArrayofLatLng = path.getArray(); 您应该使用var theArrayofLatLng = path.getArray(); instead 代替

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM