/** * Javascript functions for Address component * * @package Simple Admin Pages */ jQuery(document).ready(function ($) { /** * Set coordinate that have been received */ function sap_address_set_coords( control, lat, lon ) { control.find( '.sap-coords-result' ).remove(); control.find( 'input.lat' ).val( lat ); control.find( 'input.lon' ).val( lon ); if ( lat == '' && lon == '' ) { control.find( '.sap-map-coords' ).text( lat + sap_address.strings['no-setting'] + lon ).attr( 'style', '' ); } else { control.find( '.sap-map-coords' ).text( lat + sap_address.strings['sep-lat-lon'] + lon ).attr( 'style', '' ); } var url = 'https://maps.google.com/maps?q=' + lat + ',' + lon; if ( control.find( '.sap-view-coords' ).length ) { control.find( '.sap-view-coords' ).attr( 'href', url ); } else { control.find( '.sap-map-coords-wrapper' ).append( '' + sap_address.strings.view + '' ); } } /** * Retrieve coordinates */ $('.sap-get-coords').click( function(e) { e.stopPropagation(); e.preventDefault(); var control = $(this).parent().parent(); var address = control.find( 'textarea' ).val(); var params = { sensor: false, address: address, }; if ( sap_address.api_key_selector ) { var $input = $( sap_address.api_key_selector ); if ( $input.length && $input.val() ) { params.key = $input.val(); } } else if ( sap_address.api_key ) { params.key = sap_address.api_key; } // Reset messages control.find( '.sap-coords-result' ).remove(); control.find( '.error' ).remove(); control.find( '.sap-view-coords' ).remove(); control.find( '.sap-map-coords' ).text( sap_address.strings.retrieving ).attr( 'style', 'opacity: 0.3' ); // Call Google Maps geocoding API // See: https://developers.google.com/maps/documentation/geocoding/ var req = $.get( 'https://maps.googleapis.com/maps/api/geocode/json', params, function( data ) { if ( data.status == 'OK' ) { if ( data.results.length == 1 ) { sap_address_set_coords( control, data.results[0].geometry.location.lat, data.results[0].geometry.location.lng ); } else { for ( var key in data.results ) { control.append( '
' + data.results[key].formatted_address + ' Set
' ); } control.find( '.sap-map-coords' ).text( sap_address.strings.select ).attr( 'style', '' ); control.find( '.sap-coords-result a' ).click( function() { sap_address_set_coords( control, $(this).data( 'lat' ), $(this).data( 'lon' ) ); }); } } else { sap_address_set_coords( control, control.find( 'input.lat' ).val(), control.find( 'input.lon' ).val() ); if ( data.status == 'UNKNOWN_ERROR' ) { control.find( '.sap-coords-action-wrapper' ).prepend( '