Jump to:

5516 Posts in 1734 Topics by 1221 members

Customising the CMS

SilverStripe Forums » Customising the CMS » SS3: Using google geocoder in CMS

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

Page: 1
Go to End
Author Topic: 335 Views
  • Elzabar
    Avatar
    Community Member
    2 Posts

    SS3: Using google geocoder in CMS Link to this post

    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

    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?

    335 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.