0

This is not a 'how-to' kind of question, but rather 'please help me find the bug' kind.

Use-case: The user is free to draw a polygon on the map and search for markers within its bounds. Using Google Maps API v3.

Here is the JS:

function drawPolygon() {
    var polygon = new google.maps.Polygon({
        paths: [],
        strokeColor: '#006A4D',
        strokeOpacity: 0.8,
        strokeWeight: 1,
        fillColor: 'green',
        fillOpacity: 0.35,
        editable: true,
        draggable: true,
        clickable: true
    });
    searchWithinPolygon(polygon);
    return polygon;
}

function searchWithinPolygon(polygon) {
    var markers_within_polygon = [];
    var marker_latlng;

    console.log('1');

    for (var i = 0; i < markers.length; i++) {
        marker_latlng = new google.maps.LatLng(markers[i].position.lat(), markers[i].position.lng());
        if (google.maps.geometry.poly.containsLocation(marker_latlng, polygon)) {
            console.log('2');
            markers_within_polygon.push(markers[i]);
        }
    }

    plotData(map, markers_within_polygon);
}

Problem: For some reason I get an error: "TypeError: b.get is not a function" in main.js (clearly a function that belongs to google maps API) in browser console in this line:

if(google.maps.geometry.poly.containsLocation(marker_latlng, polygon)){

Additional Info: The browser console is printing "1" but not "2". The above error is right after "1".

I am unable to tell why this is failing. Please help. Thanks!

Unfortunately I am unable to post a screenshot because my reputation is too low.

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
  • Please provide a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) that demonstrates the problem. – geocodezip Jun 25 '15 at 21:03
  • The code is certainly minimal because I took only relevant portions from a lot more I have. I am pretty convinced this is complete because this is all one needs to figure out whats wrong. I did describe the problem under 'Problem' (bolded). The only thing this is missing is creating the map and code related to drawingManager. Wouldn't adding it negate 'minimal' criteria? Even more, I think it is too basic to be included in the question. Can you tell what more information you need? – Lightning Evangelist Jun 25 '15 at 21:10
  • I can't run it and see the problem (the reported error code). There is no map, no HTML, no paths for the polygon. – geocodezip Jun 25 '15 at 21:18
  • "no paths for the polygon"... could that be the problem? This is not a set polygon like the bermuda triangle. The user can draw a random polygon on the map. In that case the paths attribute is set to [], is it not? That said, I am now trying to put together everything you will need. – Lightning Evangelist Jun 25 '15 at 21:24
  • What he means is that he should be able to copy your code, test it and see the same error. – MrUpsidown Jun 26 '15 at 09:25
  • 1
    Did you add the geometry library in the API call? – MrUpsidown Jun 26 '15 at 09:26
  • Same error here, **but with static Polygons** obtained by `geom=feature.getGeometry()` from a `Data.feature` object. `console.log(geom.getType())` gives "Polygon"; the Polygons are `Object{ b: Array[1]}`; `b` -> `b[0][b][i].lat()` and `b[0][b][i].lng()` (where [i] is the number of vertices in the polygon). And of course the `geometry` library is installed (as it was for the OP, or they would not have even got to the error). Fortunately I can fall back on PostGIS, but that means a round-trip to the server to query&return data - data that is **already in the bloody DOM somewhere**. – GT. Apr 19 '17 at 06:15
  • Addendum: weirdly, it seems that it might work if you take the 'Polygon' returned by `feature.getGeometry()`, then decompose it by `forEachLatLng()` and stick each `latLng` into an object (say, `pts`), **then** create a new polygon instance with `paths:pts`. So to find *every* polygon that contains a clicked point, you have to iterate through all features in all layers. If you've got ~2000 features on your map, and your layers were originally a PostGIS table, it's **way** quicker to do a one-shot in PostGIS (especially if your PostGIS/PHP is *tizz-ight* and tracks what's in the viewport). – GT. Apr 19 '17 at 07:17

1 Answers1

0

I don't know if it's on purpose, but isn't it supposed to have points in it's "paths" attribute?

Even if the user creates a new polygon, try to insert a static polygon (just for debugging). And go on from there.

Given that, I suggest you see this topic, the answer is great. And it refers to the basics, you never know when you just don't see that simple step.

Community
  • 1
  • 1
Rui Silva
  • 413
  • 1
  • 5
  • 12