1$(document).ready(function () { 2 matriz.common.events.on("matriz.externalapis.google.maps.ready", function () { 3 matriz.utils.maps = { 4 consts: { 5 mapTypes: { 6 "HYBRID": google.maps.MapTypeId.HYBRID, 7 "ROADMAP": google.maps.MapTypeId.ROADMAP, 8 "SATELLITE": google.maps.MapTypeId.SATELLITE, 9 "TERRAIN": google.maps.MapTypeId.TERRAIN 10 }, 11 "ZOOM": 15, 12 "STREETVIEW": false, 13 "ZOOM_CONTROL": false, 14 "MAX_RESULTS": 5, 15 "drawModes": { 16 "NONE": 0, 17 "EDIT": 1, 18 "DRAW": 2 19 }, 20 "DEFAULT_LOCATION": new google.maps.LatLng(-30.03861402269737, -51.230708272140134) 21 }, 22 /** 23 * @param DomElement options.divMapElement 24 * @param matriz.utils.maps.consts.mapTypes options.mapType 25 * @param string options.markerImageURL 26 * @param int options.maxResults 27 * @param Function options.onPositionChange 28 * @param int options.zoom 29 * @param Function options.onAddMarker 30 * @param Function options.onAddPolygon 31 */ 32 GMap: function (options) { 33 /** 34 * Inicialização das variáveis do objeto 35 */ 36 var divMapElement = options.divMapElement; 37 var drawingOptions = { 38 "drawingControl": false 39 }; 40 var drawManager = new google.maps.drawing.DrawingManager(drawingOptions); 41 var geocoder = new google.maps.Geocoder(); 42 var map = null; 43 var mapBounds; 44 var mapOptions = { 45 "mapTypeControl": false, 46 "streetViewControl": options.streetView !== undefined ? options.streetView : matriz.utils.maps.consts.STREETVIEW, 47 "zoom": options.zoom !== undefined ? options.zoom : matriz.utils.maps.consts.ZOOM, 48 "zoomControl": options.zoomControl !== undefined ? options.zoomControl : matriz.utils.maps.consts.ZOOM_CONTROL, 49 }; 50 var marker = null; 51 var markers = {}; 52 var markerOptions = { 53 "draggable": false, 54 "icon": options.markerImageURL 55 }; 56 var markerDragStart = -1; 57 var markerDragEnd = -1; 58 var markerDragCircle = -1; 59 var markerDragCircleListener = -1; 60 var maxResults = options.maxResults !== undefined ? options.maxResults : matriz.utils.maps.consts.MAX_RESULTS; 61 var onPositionChange = options.onPositionChange; 62 var polygon = null; 63 var polygonOptions = { 64 "draggable": false, 65 "editable": false, 66 "strokeColor": "#000000" 67 }; 68 var startLat = -1; 69 var startLng = -1; 70 var infoWindow = new google.maps.InfoWindow(); 71 var self = this; 72 73 function getAddressComponentType(type) { 74 switch (type) { 75 case "administrative_area_level_1": 76 return "uf"; 77 case "street_address": 78 case "route": 79 case "intersection": 80 return "logradouro"; 81 case "locality": 82 case "administrative_area_level_2": 83 return "municipio"; 84 case "neighborhood": 85 return "complemento"; 86 case "country": 87 return "pais"; 88 case "postal_code": 89 return "cep"; 90 case "sublocality_level_1": 91 case "sublocality": 92 case "political": 93 return "bairro"; 94 case "street_number": 95 return "numero"; 96 default: 97 return undefined; 98 } 99 } 100 101 function addressComponentToLocal(addressComponent) { 102 var localData = { 103 "logradouro": "", 104 "complemento": "", 105 "municipio": "", 106 "uf": "", 107 "pais": "", 108 "cep": "", 109 "pontos": "", 110 "bairro": "", 111 "numero": "" 112 }; 113 for (var i = 0; i < addressComponent.length; i++) { 114 var addressType = getAddressComponentType.call(this, addressComponent[i].types[0]); 115 if (addressType === "complemento") { 116 localData[addressType] = localData[addressType] + localData[addressType] !== "" ? " - " : "" + addressComponent[i].long_name; 117 } else if (addressType === "uf") { 118 localData[addressType] = addressComponent[i].short_name; 119 } else if (addressType === "cep") { 120 localData[addressType] = addressComponent[i].long_name; 121 } else if (addressType === "bairro") { 122 localData[addressType] = addressComponent[i].long_name; 123 } else if (addressType === "numero") { 124 localData[addressType] = addressComponent[i].long_name; 125 } else if (addressType !== undefined) { 126 localData[addressType] = addressComponent[i].long_name; 127 } 128 } 129 return localData; 130 } 131 132 function resultsToLocalDataArray(results) { 133 var n = Math.min(maxResults, results.length); 134 var ret = []; 135 for (var i = 0; i < n; i++) { 136 var localData = addressComponentToLocal.call(this, results[i].address_components) 137 localData.pontos = pathToPoints.call(this, new google.maps.MVCArray([results[i].geometry.location]));
138 ret.push(localData); 139 } 140 return ret; 141 } 142 143 /** 144 * Atualiza o atributo geopositionamento do modelo 145 */ 146 function updateModelLocation() { 147 var path; 148 if (polygon instanceof Object) { 149 path = polygon.getPath(); 150 } else { 151 path = new google.maps.MVCArray(); 152 if (marker) { 153 path.push(marker.getPosition()); 154 } 155 } 156 if (typeof(onPositionChange) !== "undefined") { 157 onPositionChange(pathToPoints(path)); 158 } 159 } 160 161 /** 162 * Transforma o path em um ojeto LatLngBounds 163 * @param {type} path 164 * @returns {google.maps.LatLngBounds|matriz.utils.maps.GMap.pathToBounds.bounds} 165 */ 166 function pathToBounds(path) { 167 var bounds = new google.maps.LatLngBounds(); 168 path.forEach(function (element) { 169 bounds.extend(element); 170 }); 171 return bounds; 172 } 173 174 /** 175 * Move o marcador para o centro do polÃgono desenhado 176 */ 177 function moveMarkerToPolyCenter() { 178 if (polygon !== null && marker !== null) { 179 var bounds = pathToBounds.call(this, polygon.getPath()); 180 marker.setPosition(bounds.getCenter()); 181 } 182 } 183 184 /** 185 * Move o polÃgono para junto do marcador 186 */ 187 function movePolyToMarker() { 188 if (polygon instanceof Object) { 189 var lat = startLat - marker.getPosition().lat(); 190 var lng = startLng - marker.getPosition().lng(); 191 var newpath = new google.maps.MVCArray(); 192 for (var i = 0; i < polygon.getPath().length; i++) { 193 var polyPoint = polygon.getPath().getAt(i); 194 newpath.push(new google.maps.LatLng(polyPoint.lat() - lat, polyPoint.lng() - lng)); 195 } 196 polygon.setPath(newpath); 197 subscribePathEvents(newpath); 198 } 199 } 200 201 /** 202 * Atualiza o polÃgono do mapa 203 * @param {type} newPolygon 204 * @returns {undefined} 205 */ 206 function renewPolygon(newPolygon) { 207 if (polygon instanceof Object) { 208 polygon.setMap(null); 209 } 210 polygon = newPolygon; 211 if (polygon instanceof Object) { 212 polygon.setMap(map); 213 } 214 if (marker) { 215 moveMarkerToPolyCenter.call(this); 216 } 217 } 218 219 /** 220 * Evento disparado quando ocorre uma mudança no posicionamento dos objetos do mapa 221 * @param {type} _polygon 222 * @param {type} donotRenew 223 * @returns {undefined} 224 */ 225 function positionChangedEvent(_polygon, donotRenew) { 226 if (donotRenew !== true) { 227 renewPolygon.call(this, _polygon); 228 } 229 updateModelLocation.call(this); 230 } 231 232 this.addListener = function(marker, name, callback) { 233 return google.maps.event.addListener(marker, name, callback); 234 }; 235 236 this.removeListener = function(marker, listener) { 237 if (listener !== undefined) { 238 google.maps.event.clearListeners(marker, listener); 239 } else { 240 google.maps.event.clearInstanceListeners(marker); 241 } 242 }; 243 244 function subscribeMarkerEvents() { 245 if (markerDragCircle === -1) { 246 markerDragCircle = new google.maps.Marker({ 247 zIndex: -999, 248 icon: { 249 anchor: new google.maps.Point(0, 0.5), 250 path: google.maps.SymbolPath.CIRCLE, 251 fillColor: 'white', 252 fillOpacity: 0.3, 253 scale: 30, 254 strokeColor: 'white', 255 strokeWeight: 4, 256 strokeOpacity: 0.8 257 }, 258 draggable: false, 259 }); 260 } 261 262 markerDragCircle.setPosition(marker.getPosition());
263 markerDragCircle.setMap(map); 264 265 markerDragStart = google.maps.event.addListener(marker, "dragstart", (function (mouseEvent) { 266 startLat = marker.getPosition().lat(); 267 startLng = marker.getPosition().lng(); 268 }).bind(this)); 269 270 markerDragCircleListener = google.maps.event.addListener(marker, "drag", (function (mouseEvent) { 271 markerDragCircle.setPosition(marker.getPosition()); 272 }).bind(this)); 273 274 markerDragEnd = google.maps.event.addListener(marker, "dragend", (function (mouseEvent) { 275 movePolyToMarker.call(this); 276 positionChangedEvent.call(this, polygon, true); 277 }).bind(this)); 278 } 279 280 281 function unscribeMarkerEvents() { 282 if (markerDragCircle !== -1) { 283 markerDragCircle.setMap(null); 284 } 285 if (markerDragCircleListener !== -1) { 286 google.maps.event.removeListener(markerDragCircleListener); 287 } 288 if (markerDragStart !== -1) { 289 google.maps.event.removeListener(markerDragStart); 290 } 291 if (markerDragEnd !== -1) { 292 google.maps.event.removeListener(markerDragEnd); 293 } 294 } 295 296 /** 297 * Faz a inscrição nos eventos de alteração do path do polÃgono 298 * 299 * @param {type} path 300 * @returns {undefined} 301 */ 302 function subscribePathEvents(path) { 303 google.maps.event.addListener(path, 'set_at', (function () { 304 positionChangedEvent.call(this, polygon); 305 moveMarkerToPolyCenter.call(this); 306 }).bind(this)); 307 308 google.maps.event.addListener(path, 'remove_at', (function () { 309 positionChangedEvent.call(this, polygon); 310 moveMarkerToPolyCenter.call(this); 311 }).bind(this)); 312 313 google.maps.event.addListener(path, 'insert_at', (function () { 314 positionChangedEvent.call(this, polygon); 315 moveMarkerToPolyCenter.call(this); 316 }).bind(this)); 317 } 318 319 /** 320 * Faz a inscrição nos eventos de alteração do polÃgono 321 * @param {type} _polygon 322 * @returns {undefined} 323 */ 324 function subscribePolygonEvents(_polygon) { 325 google.maps.event.addListener(_polygon, 'dragend', (function (mouseEvent) { 326 positionChangedEvent.call(this, polygon); 327 subscribePathEvents.call(this, polygon.getPath()); 328 }).bind(this)); 329 330 subscribePathEvents.call(this, _polygon.getPath()); 331 } 332 333 /** 334 * Formato dos pontos: 335 * (-29.92139479824387,-51.17865890264511),(-29.921422694626127,-51.17819756269455),(-29.92208755608967,-51.178168058395386),(-29.92208290672423,-51.178699135780334) 336 * @param {type} path 337 * @returns {String} 338 */ 339 function pathToPoints(path) { 340 var points = []; 341 path.forEach(function (element) { 342 points.push("(" + element.lat() + "," + element.lng() + ")"); 343 }); 344 return points.join(","); 345 } 346 347 /** 348 * Transforma os pontos em um path 349 * @param {type} points 350 * @returns {matriz.utils.maps.GMap.pointsToPath.path|google.maps.MVCArray} 351 */ 352 function pointsToPath(points) { 353 var path = new google.maps.MVCArray(); 354 if (points.length !== 0) { 355 var aPoints = points.substr(1, points.length - 2).split("),("); 356 for (var i = 0; i < aPoints.length; i++) { 357 var aLatLng = aPoints[i].split(","); 358 path.push(new google.maps.LatLng(aLatLng[0], aLatLng[1])); 359 } 360 } 361 return path; 362 } 363 364 function processSearch(request, onSearchComplete) { 365 geocoder.geocode(request, function (result, status) { 366 if (status === google.maps.GeocoderStatus.OK) { 367 var locals = resultsToLocalDataArray.call(this, result); 368 if (onSearchComplete instanceof Function) { 369 onSearchComplete(locals); 370 } 371 } else { 372 onSearchComplete([]); 373 } 374 }); 375 } 376 377 this.centerMap = function () { 378 map.panTo(mapBounds.getCenter()); 379 }; 380 381 this.centerMapOnMarker = function(marker) { 382 map.setCenter(marker.getPosition()); 383 }; 384 385 this.centerMapOnLatLong = function(lat, long) { 386 map.panTo({lat: lat, lng: long});
387 }; 388 389 this.getCenterPolygon = function(polygon) { 390 var boundaries = new google.maps.LatLngBounds(); 391 var path = polygon.getPath().getArray(); 392 for (var key in path) { 393 boundaries.extend(new google.maps.LatLng(path[key].lat(), path[key].lng())); 394 } 395 return boundaries.getCenter(); 396 }; 397 398 this.centerMapOnPolygon = function(polygon) { 399 map.setCenter(this.getCenterPolygon(polygon)); 400 }; 401 402 this.getPointsFromMarker = function(marker) { 403 return pathToPoints.call(this, [marker.getPosition()]); 404 }; 405 406 this.getPointsFromPolygon = function(polygon) { 407 return pathToPoints.call(this, polygon.getPath()); 408 }; 409 410 this.clearMap = function() { 411 this.clearMarkers(); 412 this.clearPolygons(); 413 }; 414 415 this.clearPolygons = function () { 416 if (polygon instanceof Object) { 417 polygon.setMap(null); 418 polygon = undefined; 419 } 420 }; 421 422 this.clearMarkers = function () { 423 if (marker instanceof Object) { 424 marker.setMap(null); 425 marker = undefined; 426 } 427 428 markers = this.getMarkers(); 429 for (var markerIndex in markers) { 430 var marker = markers[markerIndex]; 431 marker.setMap(null); 432 marker = undefined; 433 } 434 markers = {}; 435 }; 436 437 function adicionaPontos(pontos) { 438 var path = pointsToPath.call(this, pontos); 439 440 if (path.getLength() > 1) { 441 addPolygon(pontos); 442 } else { 443 addMarker(pontos); 444 } 445 } 446 447 function addMarker(pontos, callback) { 448 var path = pointsToPath.call(this, pontos); 449 var bounds = pathToBounds.call(this, path); 450 451 marker = new google.maps.Marker(markerOptions); 452 marker.setMap(map); 453 marker.setPosition(bounds.getCenter()); 454 markers[pontos] = marker; 455 456 mapBounds.extend(marker.getPosition()); 457 458 if (typeof options.onAddMarker === "function") { 459 options.onAddMarker(marker, pontos); 460 } 461 462 if (typeof callback === "function") { 463 callback.apply(this, [marker]); 464 } 465 } 466 467 function addPolygon(pontos) { 468 var path = pointsToPath.call(this, pontos); 469 470 polygon = new google.maps.Polygon(polygonOptions); 471 polygon.setPath(path); 472 polygon.setMap(map); 473 polygon.getPosition = function() { 474 return self.getCenterPolygon(this); 475 }; 476 477 subscribePolygonEvents(polygon); 478 479 var paths = polygon.getPath().getArray(); 480 for (var key in paths) { 481 var path = paths[key]; 482 mapBounds.extend(new google.maps.LatLng(path.lat(), path.lng())); 483 } 484 485 if (typeof options.onAddPolygon === "function") { 486 options.onAddPolygon(polygon, pontos);
487 } 488 }; 489 490 /** 491 * Método acionado pelo cliente para carregar o map 492 */ 493 this.loadMap = function (localData) { 494 /** 495 * Inicialização do mapa 496 */ 497 map = new google.maps.Map(divMapElement, mapOptions); 498 markers = {}; 499 mapBounds = new google.maps.LatLngBounds(); 500 drawManager.setMap(map); 501 502 if (typeof (localData) !== "undefined") { 503 this.loadLocalData(localData); 504 } else { 505 var zoom = parseInt($("#bibliotecaLocalMapa").attr("data-zoom")); 506 if (!zoom.isInteger()) { 507 zoom = 8; 508 } 509 map.setZoom(zoom); 510 map.panTo(matriz.utils.maps.consts.DEFAULT_LOCATION); 511 //TODO: Centralizar de acordo com a localização do usuário 512 } 513 }; 514 515 this.loadLocalData = function(localData) { 516 if (localData.pontos instanceof Array) { 517 for (var i = 0; i < localData.pontos.length; i++) { 518 adicionaPontos(localData.pontos[i]); 519 } 520 } else { 521 adicionaPontos(localData.pontos);
522 } 523 524 if (polygon) { 525 this.fitMarkers(); 526 } 527 528 if (marker) { 529 this.centerMapOnMarker(marker); 530 } 531 532 this.centerMap(); 533 }; 534 535 this.addLocal = function(local, callback) { 536 addMarker(local.pontos, callback); 537 }; 538 539 /** 540 * Método acionado pelo cliente para localizar o mapa de acordo com as coordenadas 541 * @param Object localData 542 * @param Function onSearchComplete 543 */ 544 this.searchByCoordinates = function (localData, onSearchComplete) { 545 var request = { 546 bounds: pathToBounds.call(this, pointsToPath.call(this, localData.pontos)) 547 }; 548 processSearch.call(this, request, onSearchComplete); 549 }; 550 551 this.searchByLocation = function (localData, onSearchComplete) { 552 var latLng = pointsToPath.call(this, localData.pontos);
553 var request = { 554 location: { 555 lat: latLng.getAt(0).lat(), 556 lng: latLng.getAt(0).lng() 557 } 558 }; 559 processSearch.call(this, request, onSearchComplete); 560 }; 561 562 /** 563 * Método acionado pelo cliente para localizar o mapa de acordo com os critérios de pesquisa 564 * @param Object localData 565 * @param Function onSearchComplete 566 */ 567 this.searchByAddress = function (localData, onSearchComplete) { 568 var request = { 569 address: 570 (localData.logradouro === undefined || localData.logradouro === "" ? "" : localData.logradouro + "+") + 571 (localData.complemento === undefined || localData.complemento === "" ? "" : localData.complemento + "+") + 572 (localData.municipio === undefined || localData.municipio === "" ? "" : localData.municipio + "+") + 573 (localData.uf === undefined || localData.uf === "" ? "" : localData.uf + "+") + 574 (localData.pais === undefined || localData.pais === "" ? "" : localData.pais + "+") + 575 (localData.cep === undefined || localData.cep === "" ? "" : localData.cep) 576 }; 577 processSearch.call(this, request, onSearchComplete); 578 }; 579 580 this.IsEscapeKeyCode = function (e) { 581 var code = e.keyCode || e.which; 582 return (code === 27); 583 }; 584 585 this.IsDeleteKeyCode = function (e) { 586 var code = e.keyCode || e.which; 587 return (code === 46); 588 }; 589 590 this.switchMode = function (mode, type, drawerCallback) { 591 drawingOptions["drawingMode"] = null; 592 drawingOptions["markerOptions"] = {}; 593 drawingOptions["polygonOptions"] = {}; 594 595 switch (mode) { 596 597 case matriz.utils.maps.consts.drawModes.NONE: 598 599 this.removeListener(map, "click"); 600 this.removeListener(document, "keyup"); 601 this.removeListener(drawManager, "markercomplete"); 602 this.removeListener(drawManager, "polygoncomplete"); 603 604 if (marker instanceof Object) { 605 marker.setOptions(markerOptions); 606 } 607 if (polygon instanceof Object) { 608 polygon.setOptions(polygonOptions); 609 } 610 611 unscribeMarkerEvents.call(this); 612 break; 613 614 case matriz.utils.maps.consts.drawModes.DRAW: 615 616 drawingOptions["drawingMode"] = type; 617 switch (type) { 618 619 case google.maps.drawing.OverlayType.POLYGON: 620 621 drawingOptions["polygonOptions"] = polygonOptions; 622 623 this.removeListener(drawManager, "polygoncomplete"); 624 this.addListener(drawManager, "polygoncomplete", (function (newPolygon) { 625 positionChangedEvent.call(this, newPolygon); 626 subscribePolygonEvents.call(this, newPolygon); 627 if (typeof(drawerCallback) !== "undefined") { 628 drawerCallback.call(this, newPolygon); 629 } 630 }).bind(this)); 631 break; 632 633 case google.maps.drawing.OverlayType.MARKER: 634 635 drawingOptions["markerOptions"]
635= markerOptions; 636 637 this.removeListener(drawManager, "markercomplete"); 638 this.addListener(drawManager, "markercomplete", (function (newMarker) { 639 markers[pathToPoints.call(this, [newMarker.getPosition()])] = newMarker; 640 marker = newMarker; 641 if (typeof(drawerCallback) !== "undefined") { 642 drawerCallback.call(this, newMarker); 643 } 644 }).bind(this)); 645 break; 646 } 647 break; 648 649 case matriz.utils.maps.consts.drawModes.EDIT: 650 651 switch (type) { 652 653 case google.maps.drawing.OverlayType.POLYGON: 654 655 polygon.setOptions({ 656 editable: true, 657 draggable: true, 658 strokeColor: "#FFFFFF" 659 }); 660 661 if (typeof(drawerCallback) !== "undefined") { 662 this.removeListener(map, "click"); 663 this.addListener(map, "click", drawerCallback.bind(this, polygon)); 664 665 google.maps.event.addDomListener(document, "keyup", (function(e) { 666 if (this.IsEscapeKeyCode(e)) { 667 drawerCallback.call(this, polygon); 668 } 669 }).bind(this)); 670 } 671 672 break; 673 674 case google.maps.drawing.OverlayType.MARKER: 675 676 drawingOptions["markerOptions"] = markerOptions; 677 678 marker.setDraggable(true); 679 680 subscribeMarkerEvents.call(this); 681 682 if (typeof(drawerCallback) !== "undefined") { 683 this.removeListener(map, "click"); 684 this.addListener(map, "click", drawerCallback.bind(this, marker)); 685 686 google.maps.event.addDomListener(document, "keyup", (function(e) { 687 if (this.IsEscapeKeyCode(e)) { 688 drawerCallback.call(this, marker); 689 } 690 }).bind(this)); 691 } 692 693 break; 694 } 695 break; 696 } 697 698 drawManager.setOptions(drawingOptions); 699 }; 700 701 this.zoomIn = function () { 702 map.setZoom(map.getZoom() + 1); 703 }; 704 705 this.zoomOut = function () { 706 map.setZoom(map.getZoom() - 1); 707 }; 708 709 this.getMap = function () { 710 return map; 711 }; 712 713 this.getMarkers = function () { 714 return markers; 715 }; 716 717 this.fitMarkers = function () { 718 map.fitBounds(mapBounds); 719 }; 720 721 this.fitVisibleMarkers = function () { 722 var bounds = new google.maps.LatLngBounds(); 723 var markersVisible = 0; 724 725 for (var key in markers) { 726 var marker = markers[key]; 727 if (marker.getVisible()) { 728 bounds.extend(marker.getPosition()); 729 markersVisible++; 730 } 731 } 732 733 if (markersVisible > 1) { 734 map.fitBounds(bounds); 735 } else if (markersVisible === 1) { 736 map.setCenter(bounds.getCenter()); 737 } 738 }; 739 740 this.openWindow = function (marker, content) { 741 infoWindow.setContent(content); 742 infoWindow.open(map, marker); 743 }; 744 745 this.closeWindow = function () { 746 if (infoWindow) { 747 infoWindow.close(); 748 } 749 }; 750 751 this.findDistance = function (point1, point2) { 752 var lat1 = deg2rad(point1.lat()); 753 var lon1 = deg2rad(point1.lng()); 754 var lat2 = deg2rad(point2.lat()); 755 var lon2 = deg2rad(point2.lng()); 756 757 var dlat = lat2 - lat1; 758 var dlon = lon2 - lon1; 759 760 var a = Math.pow(Math.sin(dlat / 2), 2) + Math.
760cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(dlon / 2), 2); 761 var distanciaEmRadianos = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 762 var distanciaEmKm = distanciaEmRadianos * 6373; 763 764 return round(distanciaEmKm); 765 }; 766 767 function deg2rad(deg) { 768 rad = deg * Math.PI / 180; 769 return rad; 770 } 771 772 function round(x) { 773 return Math.round(x * 100) / 100; 774 } 775 }, 776 LocalEditor: function (local, self) { 777 this.state = null; 778 this.states = { 779 "CLEAN": 0, 780 "MARKER_VISIBLE": 1, 781 "MARKER_EDIT": 2, 782 "POLYGON_VISIBLE": 3, 783 "POLYGON_EDIT": 4 784 }; 785 786 this.changeState = function (state) { 787 this.state = state; 788 switch (this.state) { 789 case this.states.MARKER_EDIT: 790 case this.states.POLYGON_EDIT: 791 self.containers.$clearMapButton().attr('disabled', true); 792 self.containers.$drawMarkerButton().attr('disabled', true); 793 if (self.containers.hasOwnProperty('$drawPolygonButton')) { 794 self.containers.$drawPolygonButton().attr('disabled', true); 795 } 796 break; 797 case this.states.MARKER_VISIBLE: 798 case this.states.POLYGON_VISIBLE: 799 self.containers.$clearMapButton().attr('disabled', false); 800 self.containers.$drawMarkerButton().attr('disabled', true); 801 if (self.containers.hasOwnProperty('$drawPolygonButton')) { 802 self.containers.$drawPolygonButton().attr('disabled', true); 803 } 804 break; 805 case this.states.CLEAN: 806 self.containers.$clearMapButton().attr('disabled', true); 807 self.containers.$drawMarkerButton().attr('disabled', false); 808 if (self.containers.hasOwnProperty('$drawPolygonButton')) { 809 self.containers.$drawPolygonButton().attr('disabled', false); 810 } 811 break; 812 } 813 }; 814 815 this.displayMessage = function (message) { 816 matriz.box.notify.warning(message); 817 }; 818 819 this.confirmMessage = function (message, callback) { 820 matriz.box.obtrusive.confirm(message, callback); 821 }; 822 823 this.adicionaLocal = function(local) { 824 gmap.clearMap(); 825 gmap.addLocal(local, function(marker) { 826 marker.local = local; 827 828 var html = "<b>Localização</b>"; 829 html += "<br>" + local.logradouro; 830 if (local.logradouro && local.numero) { 831 html += ", " + local.numero; 832 } 833 if (local.logradouro && local.bairro) { 834 html += " - " + local.bairro; 835 } 836 if (local.complemento) { 837 html += "<br>" + local.complemento; 838 } 839 html += "<br>" + local.municipio + " - " + local.uf; 840 if (local.cep) { 841 html += "<br>" + local.cep; 842 } 843 html += "<br>" + local.pais; 844 845 html += "<br><a href='javascript: void(0)' data-matriz-action='update-local' data-matriz-local='" + JSON.stringify(local) + "'><i class='fa fa-exchange'></i> Usar informações no formulário</a>"; 846 847 gmap.openWindow(marker, html); 848 }); 849 }; 850 851 this.findInMap = function () { 852 gmap.searchByAddress(self.getLocalData(), (function (locals) { 853 if (locals.length > 0) { 854 matriz.ui.scrollTo("#bibliotecaLocalMapa"); 855 this.adicionaLocal(locals[0]); 856 this.changeState(this.states.MARKER_VISIBLE); 857 } else { 858 this.displayMessage("Sua busca não retornou resultados."); 859 } 860 }).bind(this)); 861 }; 862 863 this.updateLocal = function(evento) { 864 var dataMatrizLocal = $(evento.currentTarget).attr("data-matriz-local"); 865 var local = JSON.parse(dataMatrizLocal); 866 self.setLocalData(local); 867 }; 868 869 this.updateLocalPontos = function(pontos) { 870 var $pontos = self.containers.$form().find("input[name='local[pontos]']"); 871 if ($pontos.length) { 872 $pontos.val(pontos);
873 } 874 $pontos = self.containers.$form().find("#sTxtGeorreferenciamento") 875 if ($pontos.length) { 876 $pontos.val(pontos); 877 } 878 }; 879 880 this.updateLocalDataByMap = function () { 881 var localData = self.getLocalData(); 882 883 gmap.searchByLocation(localData, (function(locals) { 884 if (locals.length > 0) { 885 this.adicionaLocal(locals[0]); 886 } 887 }).bind(this)); 888 }; 889 890 this.preparePolygonClickListener = function (polygon) { 891 this.changeState(this.states.POLYGON_VISIBLE); 892 gmap.switchMode(matriz.utils.maps.consts.drawModes.DRAW); 893 gmap.removeListener(polygon, "click"); 894 gmap.addListener(polygon, "click", this.editPolygon.bind(this)); 895 this.updateLocalPontos(gmap.getPointsFromPolygon(polygon)); 896 }; 897 898 this.drawPolygon = function () { 899 this.changeState(this.states.POLYGON_EDIT); 900 gmap.switchMode(matriz.utils.maps.consts.drawModes.DRAW, google.maps.drawing.OverlayType.POLYGON, this.preparePolygonClickListener.bind(this)); 901 }; 902 903 this.editPolygon = function () { 904 this.changeState(this.states.POLYGON_EDIT); 905 gmap.switchMode(matriz.utils.maps.consts.drawModes.EDIT, google.maps.drawing.OverlayType.POLYGON, this.preparePolygonClickListener.bind(this)); 906 }; 907 908 this.prepareMarkerClickListener = function (marker) { 909 var lastState = this.state; 910 this.changeState(this.states.MARKER_VISIBLE); 911 gmap.switchMode(matriz.utils.maps.consts.drawModes.DRAW); 912 gmap.removeListener(marker, "click"); 913 gmap.addListener(marker, "click", this.editMarker.bind(this)); 914 this.updateLocalPontos(gmap.getPointsFromMarker(marker)); 915 916 if ([this.states.MARKER_EDIT, this.states.POLYGON_EDIT].indexOf(lastState) !== -1) { 917 this.updateLocalDataByMap(); 918 } 919 }; 920 921 this.drawMarker = function () { 922 this.changeState(this.states.MARKER_EDIT); 923 gmap.switchMode(matriz.utils.maps.consts.drawModes.DRAW, google.maps.drawing.OverlayType.MARKER, this.prepareMarkerClickListener.bind(this)); 924 }; 925 926 this.editMarker = function () { 927 this.changeState(this.states.MARKER_EDIT); 928 gmap.switchMode(matriz.utils.maps.consts.drawModes.EDIT, google.maps.drawing.OverlayType.MARKER, this.prepareMarkerClickListener.bind(this)); 929 }; 930 931 this.clearMap = function () { 932 this.changeState(this.states.CLEAN); 933 this.updateLocalPontos(null); 934 gmap.clearMap(); 935 }; 936 937 this.renderMap = function (locals, onSuccess, fixAtPoint) { 938 if (locals.length > 0) { 939 if (fixAtPoint !== undefined) { 940 locals[0].pontos = fixAtPoint; 941 } 942 if (typeof(locals) !== "undefined") { 943 gmap.loadMap(locals[0]); 944 } else { 945 gmap.loadMap(); 946 } 947 if (onSuccess instanceof Function) { 948 onSuccess(); 949 } 950 } else { 951 this.displayMessage("Não foram encontrados locais correspondentes à pesquisa."); 952 } 953 }; 954 955 this.initEvents = function () { 956 self.containers.$findInMapButton().off("click").on("click", this.findInMap.bind(this)); 957 if (self.containers.hasOwnProperty('$drawPolygonButton')) { 958 self.containers.$drawPolygonButton().off("click").on("click", this.drawPolygon.bind(this)); 959 } 960 self.containers.$drawMarkerButton().off("click").on("click", this.drawMarker.bind(this)); 961 self.containers.$clearMapButton().off("click").on("click", this.clearMap.bind(this)); 962 self.containers.$mapa().off("click").on("click", "a[data-matriz-action='update-local']", this.updateLocal.bind(this)); 963 }; 964 965 this.changeState(this.states.CLEAN); 966 var gmap = new matriz.utils.maps.GMap({ 967 "divMapElement": self.containers.$mapa()[0], 968 "markerImageURL": window.location.origin + (matriz.global.theme.uri ? matriz.global.theme.uri : "/themes/admin") + "/images/markers/gradient.png", 969 "streetView": true, 970 "zoomControl": true, 971 "onAddMarker": this.prepareMarkerClickListener.bind(this), 972 "onAddPolygon": this.preparePolygonClickListener.bind(this) 973 }); 974 975 this.getGmap = function () { 976 return gmap; 977 }; 978 979 this.initEvents(); 980 } 981 }; 982 });
vendor: 3 bytes, line 983
983});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.