Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1 //==========================
A 2 // Polygon containsLatLng
3 // https://github.com/tparkin/Google-Maps-Point-in-Polygon
4 // Poygon getBounds extension - google-maps-extensions
5 // http://code.google.com/p/google-maps-extensions/source/browse/google.maps.Polygon.getBounds.js
6 if (!google.maps.Polygon.prototype.getBounds) {
7   google.maps.Polygon.prototype.getBounds = function(latLng) {
8     var bounds = new google.maps.LatLngBounds();
9     var paths = this.getPaths();
10     var path;
11
12     for (var p = 0; p < paths.getLength(); p++) {
13       path = paths.getAt(p);
14       for (var i = 0; i < path.getLength(); i++) {
15         bounds.extend(path.getAt(i));
16       }
17     }
18
19     return bounds;
20   };
21 }
22
23 if (!google.maps.Polygon.prototype.containsLatLng) {
24   // Polygon containsLatLng - method to determine if a latLng is within a polygon
25   google.maps.Polygon.prototype.containsLatLng = function(latLng) {
26     // Exclude points outside of bounds as there is no way they are in the poly
27     var bounds = this.getBounds();
28
29     if (bounds !== null && !bounds.contains(latLng)) {
30       return false;
31     }
32
33     // Raycast point in polygon method
34     var inPoly = false;
35
36     var numPaths = this.getPaths().getLength();
37     for (var p = 0; p < numPaths; p++) {
38       var path = this.getPaths().getAt(p);
39       var numPoints = path.getLength();
40       var j = numPoints - 1;
41
42       for (var i = 0; i < numPoints; i++) {
43         var vertex1 = path.getAt(i);
44         var vertex2 = path.getAt(j);
45
46         if (vertex1.lng() < latLng.lng() && vertex2.lng() >= latLng.lng() || vertex2.lng() < latLng.lng() && vertex1.lng() >= latLng.lng()) {
47           if (vertex1.lat() + (latLng.lng() - vertex1.lng()) / (vertex2.lng() - vertex1.lng()) * (vertex2.lat() - vertex1.lat()) < latLng.lat()) {
48             inPoly = !inPoly;
49           }
50         }
51
52         j = i;
53       }
54     }
55
56     return inPoly;
57   };
58 }
59
60 google.maps.LatLngBounds.prototype.containsLatLng = function(latLng) {
61   return this.contains(latLng);
62 };
63
64 google.maps.Marker.prototype.setFences = function(fences) {
65   this.fences = fences;
66 };
67
68 google.maps.Marker.prototype.addFence = function(fence) {
69   this.fences.push(fence);
70 };
71
72 google.maps.Marker.prototype.getId = function() {
73   return this['__gm_id'];
74 };
75
76 //==========================
77 // Array indexOf
78 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
79 if (!Array.prototype.indexOf) {
80   Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
81       "use strict";
82       if (this == null) {
83           throw new TypeError();
84       }
85       var t = Object(this);
86       var len = t.length >>> 0;
87       if (len === 0) {
88           return -1;
89       }
90       var n = 0;
91       if (arguments.length > 1) {
92           n = Number(arguments[1]);
93           if (n != n) { // shortcut for verifying if it's NaN
94               n = 0;
95           } else if (n != 0 && n != Infinity && n != -Infinity) {
96               n = (n > 0 || -1) * Math.floor(Math.abs(n));
97           }
98       }
99       if (n >= len) {
100           return -1;
101       }
102       var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
103       for (; k < len; k++) {
104           if (k in t && t[k] === searchElement) {
105               return k;
106           }
107       }
108       return -1;
109   }
110 }