Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleaned up JShint errors. Added fancy animation for dropping the marker.
- Loading branch information
1 parent
eb3c853
commit 5092f5b
Showing
1 changed file
with
114 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,120 @@ | ||
|
||
var marker, map, geocoder; | ||
|
||
jQuery( document ).ready( function () | ||
{ | ||
var latlng = new google.maps.LatLng( 53.346881, -6.258860 ); | ||
map = new google.maps.Map( jQuery( '.rwmb-map-canvas' )[0], { | ||
zoom : 8, | ||
center : latlng, | ||
streetViewControl : 0, | ||
mapTypeId : google.maps.MapTypeId.ROADMAP | ||
}); | ||
marker = new google.maps.Marker( {position: latlng, map: map, draggable: true} ); | ||
geocoder = new google.maps.Geocoder(); | ||
|
||
google.maps.event.addListener( map, 'click', function ( event ) | ||
{ | ||
marker.setPosition( event.latLng ); | ||
updatePositionInput( event.latLng ); | ||
} ); | ||
google.maps.event.addListener( marker, 'drag', function ( event ) | ||
{ | ||
updatePositionInput( event.latLng ); | ||
} ); | ||
updatePositionMarker(); | ||
|
||
autoCompleteAddress(); | ||
} ); | ||
/*jshint jquery: true, devel:true, browser: true, newcap: true, noempty: true, strict: true, undef: true */ | ||
"use strict"; | ||
var latlng = new window.google.maps.LatLng( 53.346881, -6.258860 ); | ||
map = new window.google.maps.Map( jQuery( '.rwmb-map-canvas' )[0], { | ||
zoom : 8, | ||
center : latlng, | ||
streetViewControl : 0, | ||
mapTypeId : window.google.maps.MapTypeId.ROADMAP | ||
}); | ||
marker = new window.google.maps.Marker( {position: latlng, map: map, draggable: true} ); | ||
geocoder = new window.google.maps.Geocoder(); | ||
|
||
function updatePositionInput( latLng ) | ||
{ | ||
jQuery( '#rwmb-map-coordinate' ).val( latLng.lat() + ',' + latLng.lng() ); | ||
} | ||
window.google.maps.event.addListener( map, 'click', function ( event ) | ||
{ | ||
marker.setPosition( event.latLng ); | ||
updatePositionInput( event.latLng ); | ||
} ); | ||
window.google.maps.event.addListener( marker, 'drag', function ( event ) | ||
{ | ||
updatePositionInput( event.latLng ); | ||
} ); | ||
updatePositionMarker(); | ||
|
||
function updatePositionMarker() | ||
{ | ||
var coord = jQuery( '#rwmb-map-coordinate' ).val(), | ||
addressField = jQuery( '#rwmb-map-goto-address-button' ).val(), | ||
l, zoom; | ||
|
||
if ( coord ) | ||
{ | ||
l = coord.split( ',' ); | ||
marker.setPosition( new google.maps.LatLng( l[0], l[1] ) ); | ||
zoom = l.length > 2 ? parseInt( l[2] ) : 15; | ||
|
||
map.setCenter( marker.position ); | ||
map.setZoom( zoom ); | ||
} | ||
else | ||
if ( addressField ){ | ||
geocodeAddress( addressField ); | ||
console.log(ad); | ||
} | ||
} | ||
|
||
function geocodeAddress( addressField ) | ||
{ | ||
console.log(addressField); | ||
var address = '', | ||
fieldList = addressField.split( ',' ), | ||
loop; | ||
|
||
for ( loop = 0; loop < fieldList.length; loop++ ) | ||
{ | ||
address += jQuery( '#' + fieldList[loop] ).val(); | ||
} | ||
|
||
address = address.replace( /\n/g, ',' ); | ||
address = address.replace( /,,/g, ',' ); | ||
geocoder.geocode( {'address': address}, function ( results, status ) | ||
{ | ||
if ( status == google.maps.GeocoderStatus.OK ) | ||
{ | ||
updatePositionInput( results[0].geometry.location ); | ||
marker.setPosition( results[0].geometry.location ); | ||
map.setCenter( marker.position ); | ||
map.setZoom( 15 ); | ||
} | ||
} ); | ||
} | ||
|
||
|
||
function autoCompleteAddress(){ | ||
var addressField = jQuery( '#rwmb-map-goto-address-button' ).val(); | ||
if (!addressField) return null; | ||
|
||
jQuery( '#' + addressField).autocomplete({ | ||
source: function(request, response) { | ||
// TODO: add 'region' option, to help bias geocoder. | ||
geocoder.geocode( {'address': request.term }, function(results, status) { | ||
response(jQuery.map(results, function(item) { | ||
return { | ||
label : item.formatted_address, | ||
value : item.formatted_address, | ||
latitude : item.geometry.location.lat(), | ||
longitude : item.geometry.location.lng() | ||
} | ||
})); | ||
}) | ||
}, | ||
select: function(event, ui) { | ||
|
||
jQuery("#rwmb-map-coordinate").val(ui.item.latitude + ',' + ui.item.longitude ); | ||
|
||
var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude); | ||
marker.setPosition(location); | ||
map.setCenter(location); | ||
} | ||
}); | ||
} | ||
autoCompleteAddress(); | ||
|
||
function updatePositionInput( latLng ) | ||
{ | ||
jQuery( '#rwmb-map-coordinate' ).val( latLng.lat() + ',' + latLng.lng() ); | ||
} | ||
|
||
function updatePositionMarker() | ||
{ | ||
var coord = jQuery( '#rwmb-map-coordinate' ).val(), | ||
addressField = jQuery( '#rwmb-map-goto-address-button' ).val(), | ||
l, zoom; | ||
|
||
if ( coord ) | ||
{ | ||
l = coord.split( ',' ); | ||
marker.setPosition( new window.google.maps.LatLng( l[0], l[1] ) ); | ||
|
||
zoom = l.length > 2 ? parseInt( l[2], 10 ) : 15; | ||
|
||
map.setCenter( marker.position ); | ||
map.setZoom( zoom ); | ||
} | ||
else | ||
if ( addressField ){ | ||
geocodeAddress( addressField ); | ||
} | ||
} | ||
|
||
function geocodeAddress( addressField ) | ||
{ | ||
console.log(addressField); | ||
var address = '', | ||
fieldList = addressField.split( ',' ), | ||
loop; | ||
|
||
for ( loop = 0; loop < fieldList.length; loop++ ) | ||
{ | ||
address += jQuery( '#' + fieldList[loop] ).val(); | ||
} | ||
|
||
address = address.replace( /\n/g, ',' ); | ||
address = address.replace( /,,/g, ',' ); | ||
geocoder.geocode( {'address': address}, function ( results, status ) | ||
{ | ||
if ( status == window.google.maps.GeocoderStatus.OK ) | ||
{ | ||
updatePositionInput( results[0].geometry.location ); | ||
marker.setPosition( results[0].geometry.location ); | ||
map.setCenter( marker.position ); | ||
map.setZoom( 15 ); | ||
} | ||
} ); | ||
} | ||
|
||
|
||
function autoCompleteAddress(){ | ||
var addressField = jQuery( '#rwmb-map-goto-address-button' ).val(); | ||
if (!addressField) return null; | ||
|
||
jQuery( '#' + addressField).autocomplete({ | ||
source: function(request, response) { | ||
// TODO: add 'region' option, to help bias geocoder. | ||
geocoder.geocode( {'address': request.term }, function(results, status) { | ||
response(jQuery.map(results, function(item) { | ||
return { | ||
label : item.formatted_address, | ||
value : item.formatted_address, | ||
latitude : item.geometry.location.lat(), | ||
longitude : item.geometry.location.lng() | ||
}; | ||
})); | ||
}); | ||
}, | ||
select: function(event, ui) { | ||
|
||
jQuery("#rwmb-map-coordinate").val(ui.item.latitude + ',' + ui.item.longitude ); | ||
|
||
var location = new window.google.maps.LatLng(ui.item.latitude, ui.item.longitude); | ||
|
||
map.setCenter(location); | ||
// Drop the Marker | ||
setTimeout( function(){ | ||
marker.setValues({ | ||
position : location, | ||
animation : window.google.maps.Animation.DROP | ||
}); | ||
}, 1500); | ||
} | ||
}); | ||
} | ||
} ); |