GMapPanel.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @class Ext.ux.GMapPanel
  3. * @extends Ext.Panel
  4. * @author Shea Frederick
  5. */
  6. Ext.define('Ext.ux.GMapPanel', {
  7. extend: 'Ext.panel.Panel',
  8. alias: 'widget.gmappanel',
  9. requires: ['Ext.window.MessageBox'],
  10. initComponent : function(){
  11. Ext.applyIf(this,{
  12. plain: true,
  13. gmapType: 'map',
  14. border: false
  15. });
  16. this.callParent();
  17. },
  18. afterFirstLayout : function(){
  19. var center = this.center;
  20. this.callParent();
  21. if (center) {
  22. if (center.geoCodeAddr) {
  23. this.lookupCode(center.geoCodeAddr, center.marker);
  24. } else {
  25. this.createMap(center);
  26. }
  27. } else {
  28. Ext.Error.raise('center is required');
  29. }
  30. },
  31. createMap: function(center, marker) {
  32. options = Ext.apply({}, this.mapOptions);
  33. options = Ext.applyIf(options, {
  34. zoom: 14,
  35. center: center,
  36. mapTypeId: google.maps.MapTypeId.HYBRID
  37. });
  38. this.gmap = new google.maps.Map(this.body.dom, options);
  39. if (marker) {
  40. this.addMarker(Ext.applyIf(marker, {
  41. position: center
  42. }));
  43. }
  44. Ext.each(this.markers, this.addMarker, this);
  45. },
  46. addMarker: function(marker) {
  47. marker = Ext.apply({
  48. map: this.gmap
  49. }, marker);
  50. if (!marker.position) {
  51. marker.position = new google.maps.LatLng(marker.lat, marker.lng);
  52. }
  53. var o = new google.maps.Marker(marker);
  54. Ext.Object.each(marker.listeners, function(name, fn){
  55. google.maps.event.addListener(o, name, fn);
  56. });
  57. return o;
  58. },
  59. lookupCode : function(addr, marker) {
  60. this.geocoder = new google.maps.Geocoder();
  61. this.geocoder.geocode({
  62. address: addr
  63. }, Ext.Function.bind(this.onLookupComplete, this, [marker], true));
  64. },
  65. onLookupComplete: function(data, response, marker){
  66. if (response != 'OK') {
  67. Ext.MessageBox.alert('Error', 'An error occured: "' + response + '"');
  68. return;
  69. }
  70. this.createMap(data[0].geometry.location, marker);
  71. },
  72. afterComponentLayout : function(w, h){
  73. this.callParent(arguments);
  74. this.redraw();
  75. },
  76. redraw: function(){
  77. var map = this.gmap;
  78. if (map) {
  79. google.maps.event.trigger(map, 'resize');
  80. }
  81. }
  82. });