├── cftp_map_widget.js ├── cftp_map_widget.php ├── cftp_map_widget_admin.js ├── composer.json ├── plugin.php ├── readme.md └── screenshot.png /cftp_map_widget.js: -------------------------------------------------------------------------------- 1 | jQuery(document).ready(function() { 2 | jQuery('.cftp_map_widget_container').each( function ( index ) { 3 | var el = jQuery( this ); 4 | var mlat = el.data('lat'); 5 | var mlong = el.data('long'); 6 | var zoom = el.data('zoom'); 7 | var latlng = new google.maps.LatLng( mlat, mlong ); 8 | var mapOptions = { 9 | center: latlng, 10 | zoom: zoom, 11 | zoomControl: true, 12 | panControl: false 13 | }; 14 | var map = new google.maps.Map( this, mapOptions ); 15 | 16 | var mmarker = el.data( 'marker' ); 17 | if ( mmarker == true ) { 18 | var marker = new google.maps.Marker( { 19 | position: latlng, 20 | map: map 21 | }); 22 | } 23 | }); 24 | }); -------------------------------------------------------------------------------- /cftp_map_widget.php: -------------------------------------------------------------------------------- 1 | __('Embed a google map') ); 7 | WP_Widget::__construct( 'cftp_map_widget_', __('Map Widget'), $widget_ops ); 8 | } 9 | 10 | public function widget( $args, $instance ) { 11 | $options = get_option( 'cftp_gmap_options' ); 12 | wp_enqueue_script( 'google_map_api', 'https://maps.googleapis.com/maps/api/js');//?key='.$options['api_key'] ); 13 | wp_enqueue_script( 'cftp_map_widget', plugin_dir_url( __FILE__ ).'cftp_map_widget.js', array( 'jquery' ) ); 14 | $title = apply_filters( 'widget_title', $instance['title'] ); 15 | 16 | $lat = 0; 17 | if ( isset( $instance[ 'lat' ] ) ) { 18 | $lat = $instance[ 'lat' ]; 19 | } 20 | $long = 0; 21 | if ( isset( $instance[ 'long' ] ) ) { 22 | $long = $instance[ 'long' ]; 23 | } 24 | $zoom = 10; 25 | if ( isset( $instance[ 'zoom' ] ) ) { 26 | $zoom = $instance[ 'zoom' ]; 27 | } 28 | 29 | $marker = true; 30 | if ( isset( $instance[ 'marker' ] ) ) { 31 | $marker = $instance[ 'marker' ]; 32 | } else { 33 | $marker = false; 34 | } 35 | 36 | echo $args['before_widget']; 37 | if ( ! empty( $title ) ) { 38 | echo $args['before_title'] . esc_html( $title ) . $args['after_title']; 39 | } 40 | ?> 41 | 42 |
43 | 51 | 106 | None Found'); 123 | } else { 124 | results = results.slice( 0, 6 ); 125 | results.forEach( jQuery.proxy( this.processGeoCoderResult, this ) ); 126 | } 127 | } 128 | 129 | MapWidget.prototype.processGeoCoderResult = function ( result ) { 130 | var pos = result.geometry.location; 131 | var el = jQuery( '').text( result.formatted_address ); 132 | el.data( 'lat', pos.lat() ); 133 | el.data( 'lng', pos.lng() ); 134 | el.click( jQuery.proxy( this.onGeoCoderResultClick, this ) ); 135 | this.geoResults.append( el ); 136 | } 137 | 138 | MapWidget.prototype.onGeoCoderResultClick = function ( event ) { 139 | if ( this.idle == false ) { 140 | return; 141 | } 142 | var target = event.target || event.srcElement; 143 | target = jQuery( target ); 144 | var lat = target.data( 'lat' ); 145 | var lng = target.data( 'lng' ); 146 | 147 | this.setPosition( lat, lng ); 148 | } 149 | 150 | MapWidget.prototype.setPosition = function ( lat, lng ) { 151 | this.idle = false; 152 | var position = new google.maps.LatLng( lat, lng ); 153 | this.marker.setMap( null ); 154 | this.setMarkerPosition( position ); 155 | this.map.panTo( position ); 156 | this.marker.setMap( this.map ); 157 | this.resizeMap(); 158 | } 159 | 160 | MapWidget.prototype.setMarkerPosition = function ( position ) { 161 | this.marker.setPosition( position ); 162 | } 163 | 164 | MapWidget.prototype.center_changed = function () { 165 | if ( this.idle == false ) { 166 | return; 167 | } 168 | var center = this.map.getCenter(); 169 | this.latInput.val( center.lat() ); 170 | this.longInput.val( center.lng() ); 171 | } 172 | 173 | MapWidget.prototype.dragend = function () { 174 | if ( this.idle == false ) { 175 | return; 176 | } 177 | var center = this.map.getCenter(); 178 | this.setMarkerPosition( center ); 179 | } 180 | 181 | MapWidget.prototype.zoom_changed = function () { 182 | if ( this.idle == false ) { 183 | return; 184 | } 185 | var zoom = this.map.getZoom(); 186 | this.zoomInput.val( zoom ); 187 | } 188 | 189 | MapWidget.prototype.getZoom = function () { 190 | var z = this.zoomInput.val(); 191 | return z; 192 | } 193 | 194 | 195 | jQuery(document).ready(function() { 196 | var mw = new MapWidgets(); 197 | mw.init(); 198 | }); 199 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cftp/map-widget", 3 | "description": "A widget for embedding google maps", 4 | "license": "GPL-2.0+", 5 | "type": "wordpress-plugin", 6 | "authors": [ 7 | { 8 | "name": "Tom J Nowell", 9 | "email": "contact@tomjn.com", 10 | "homepage": "http://tomjn.com" 11 | }, 12 | { 13 | "name" : "Code For The People", 14 | "homepage": "http://codeforthepeople.com/" 15 | } 16 | ], 17 | "require": { 18 | "composer/installers": "~1.0" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /plugin.php: -------------------------------------------------------------------------------- 1 |