|
| 1 | + |
| 2 | +function awpp_wrapper ($) { |
| 3 | + var awpp = { |
| 4 | + init: function() { |
| 5 | + this.name = 'Awpp'; |
| 6 | + //this.canvas = $('#awpp-map-dom-element'); |
| 7 | + // see https://groups.google.com/forum/?fromgroups=#!topic/google-maps-api/hf9h71UVBx0 |
| 8 | + this.canvas = document.getElementById( 'awpp-map-dom-element' ); |
| 9 | + if( this.canvas ) |
| 10 | + this.buildMap(); |
| 11 | + else |
| 12 | + $( this.canvas ).html( awpp.name + " error: couldn't retrieve DOM elements." ); |
| 13 | + }, |
| 14 | + buildMap : function() { |
| 15 | + var mapOptions; |
| 16 | + mapOptions = { |
| 17 | + 'zoom' : parseInt( awppMapData.options.zoom ), |
| 18 | + 'center' : new google.maps.LatLng( |
| 19 | + parseFloat( awppMapData.options.latitude ), |
| 20 | + parseFloat( awppMapData.options.longitude ) ), |
| 21 | + 'mapTypeId' : google.maps.MapTypeId[ awppMapData.options.type ], |
| 22 | + 'mapTypeControl' : awppMapData.options.typeControl == 'off' ? false : true, |
| 23 | + 'mapTypeControlOptions' : { style: google.maps.MapTypeControlStyle[ awppMapData.options.typeControl ] }, |
| 24 | + 'navigationControl' : awppMapData.options.navigationControl == 'off' ? false : true, |
| 25 | + 'navigationControlOptions' : { style: google.maps.NavigationControlStyle[ awppMapData.options.navigationControl ] } |
| 26 | + }; |
| 27 | + // Override default width/heights from settings |
| 28 | + $( '#awpp-map-dom-element' ).css( 'width', awppMapData.options.mapWidth ); |
| 29 | + $( '#awpp-map-dom-element' ).css( 'height', awppMapData.options.mapHeight ); |
| 30 | + // Create the map |
| 31 | + try { |
| 32 | + map = new google.maps.Map( this.canvas, mapOptions ); |
| 33 | + } catch( e ) { |
| 34 | + $( this.canvas ).html( awpp.name + " error: couln't build map." ); |
| 35 | + if( window.console ) |
| 36 | + console.log( 'awpp_buildMap: '+ e ); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + this.addPlacemarks( map ); |
| 41 | + }, |
| 42 | + addPlacemarks : function( map ) { |
| 43 | + if( awppMapData.markers.length > 0 ) |
| 44 | + for( var m in awppMapData.markers ) { |
| 45 | + this.createMarker( |
| 46 | + map, |
| 47 | + awppMapData.markers[m]['title'], |
| 48 | + awppMapData.markers[m]['latitude'], |
| 49 | + awppMapData.markers[m]['longitude'], |
| 50 | + awppMapData.markers[m]['details'], |
| 51 | + awppMapData.markers[m]['icon'], |
| 52 | + parseInt( awppMapData.markers[m]['zIndex'] ) |
| 53 | + ); |
| 54 | + } |
| 55 | + }, |
| 56 | + createMarker : function( map, title, latitude, longitude, details, icon, zIndex ) { |
| 57 | + var infowindowcontent, infowindow, marker; |
| 58 | + |
| 59 | + if( isNaN( latitude ) || isNaN( longitude ) ) { |
| 60 | + if( window.console ) |
| 61 | + console.log( "awpp_createMarker(): "+ title +" latitude and longitude weren't valid." ); |
| 62 | + return false; |
| 63 | + } |
| 64 | + infowindowcontent = '<div class="awpp_placemark"> <h3>'+ title +'</h3> <div>'+ details +'</div> </div>'; |
| 65 | + try { |
| 66 | + infowindow = new google.maps.InfoWindow( { |
| 67 | + content : infowindowcontent, |
| 68 | + maxWidth : awppMapData.options.infoWindowMaxWidth |
| 69 | + } ); |
| 70 | + // Replace commas with periods. Some (human) languages use commas to delimit the fraction from the whole number, but Google Maps doesn't accept that. |
| 71 | + //latitude = parseFloat( latitude.replace( ',', '.' ) ); |
| 72 | + //longitude = parseFloat( longitude.replace( ',', '.' ) ); |
| 73 | + |
| 74 | + marker = new google.maps.Marker( { |
| 75 | + 'position' : new google.maps.LatLng( latitude, longitude ), |
| 76 | + 'map' : map, |
| 77 | + 'icon' : icon, |
| 78 | + 'title' : title, |
| 79 | + 'zIndex' : 0 |
| 80 | + } ); |
| 81 | + |
| 82 | + google.maps.event.addListener( marker, 'click', function() { |
| 83 | + if( this.previousInfoWindow != undefined ) |
| 84 | + this.previousInfoWindow.close(); |
| 85 | + infowindow.open( map, marker ); |
| 86 | + this.previousInfoWindow = infowindow; |
| 87 | + } ); |
| 88 | + |
| 89 | + return true; |
| 90 | + |
| 91 | + } catch( e ) { |
| 92 | + $( this.canvas ).append( '<p>' + this.name + " error: couldn't add map placemarks.</p>"); |
| 93 | + if( window.console ) |
| 94 | + console.log( 'awpp_createMarker: '+ e ); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Kick things off... (wordpress uses jquery in no-conflict mode |
| 100 | + // (http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_wrappers) |
| 101 | + $( document ).ready( function() { |
| 102 | + awpp.init(); |
| 103 | + } ); |
| 104 | +} |
| 105 | + |
| 106 | +awpp_wrapper( jQuery ); |
0 commit comments