Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

SS3: Using google geocoder in CMS


Go to End


1066 Views

Avatar
Elzabar

Community Member, 2 Posts

10 September 2012 at 2:26pm

Edited: 10/09/2012 2:59pm

I am trying to use googles geocoder service to store the latitude and longitude of a location once it is saved in the CMS. These will then be used in the front end to place markers on the map. I've gotten it to work on page load, but I want it to happen automatically before a location is saved. I've tried using SilverStripes beforeSave function but with no luck. Help would be greatly appreciated :D

Here is the JavaScript I am using in the backend:

jQuery.noConflict();

(function($) {
$(document).ready(function() {
	
	Behaviour.register({
	
		'#Form_ItemEditForm' : {
	        initialize : function() {
	            this.observeMethod('PageLoaded', this.pageLoaded);
	            this.observeMethod('BeforeSave', this.beforeSave);
	            this.pageLoaded(); // call pageload initially too.
	        },
	        
	        pageLoaded : function() {
	            alert("You loaded a page");
	        },
	        
			beforeSave: function() {
				var geocoder = new google.maps.Geocoder();
			
				var country = $('#Form_ItemEditForm_Country').val();
				var city = $('#Form_ItemEditForm_City').val();
				var address = country + ', ' + city;
				alert(address);
				
				var request = {
					address: address
				};
				
				geocoder.geocode(request, function(results, status) {
					
					var coords = results[0].geometry.location;
					console.log(coords);
					
					$('#Form_ItemEditForm_Lat').val(coords.Xa);
					$('#Form_ItemEditForm_Lng').val(coords.Ya);
					
				});
			}
	    } // #Form_EditForm
    });
});
}(jQuery));

Edit: Ahh, so behaviour.js has been removed for SS3. What might I be able to use in place of Behaviour?