문제

I am drawing a list of zip code areas in google maps and I am trying to write a label with the zip code over the area. I can do this just fine, except that I am using the centriod of the area to position the label, but some areas are shaped irregularly and the centroid falls outside the area itself. Here is an example of what I mean:

enter image description here

Watch zip code 81151, how it shows up on top of the area for zip code 81123 rather than on top of his own area.

Does anyone know of a better algorithm to correctly position the zip code on top of an irregular area, rather than using the area's centroid?

도움이 되었습니까?

해결책

You can use the google.maps.geometry.poly.containsLocation and google.maps.geometry.spherical.interpolate functions.

for example. get a boundary point and the center point of the polygon and use the function 'google.maps.geometry.spherical.interpolate ({centerpoint}, {bundaypoint}, {distance})' then you verify that the resulting point is in the area of ​​the polygon with 'google.maps.geometry.poly.containsLocation ({resultPoint}, {polygonObject})' function.

GetBounds function:

if (!google.maps.Polygon.prototype.getBounds) {
            google.maps.Polygon.prototype.getBounds = function (latLng) {
                var bounds = new google.maps.LatLngBounds();
                var paths = this.getPaths();
                var path;
                for (var p = 0; p < paths.getLength() ; p++) {
                    path = paths.getAt(p);
                    for (var i = 0; i < path.getLength() ; i++) {
                        bounds.extend(path.getAt(i));
                    }
                }
                return bounds;
            }
        }

다른 팁

I had a similar problem a couple of weeks ago and didn't find any algorithm(maybe there isn't any).

Finally I found a fusionTable with the desired data(I've used a fusionTable too to store the boundaries, so it was easy to merge the tables).

Maybe you can do the same, a quick search gave the following result: https://www.google.com/fusiontables/DataSource?docid=1VwApbjvZ--YKE6fRT_StuJ7jd2o0r2jLUmGwQnI

e.g. for the zip 81151 the coordinate is placed there more to the west, maybe this table fits your needs

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top