Making a custom property editor for looking up addresses using Google Maps API
Hi all
I'm trying to do the following.
I want a custom property editor that contains an address lookup feature.
It consists of an input field, coupled to the GoogleMaps api. When a user clicks on an item from the autocomplete dropdown, then it fills out a small form with all the address info like so:
I've already fiddled around on a local instance of umbraco v7. I can recreate the form in a property Editor, I can use the api in a seperate .js file and from there fill out the form. This approach gives one problem. Saving. Because in the example the input fields are manipulated by javascript, AngularJS will not count this as a change. Apparantly I cannot use functions like $setDirty() to fix this.
How should I solve this according to umbraco convention? use the Google api in the controller en return values from there instead of a seperate javascript file? Or is the previous approach also plausible but am I missing something?
I would love to hear your thoughts/advice on this.
this is the function that programmatically changes the form input fields + what I entered to get into my controller to get it to save. (starting at "var scope") (thanks for the hint about $scope.apply() !!). But this approach feels 'wrong' as it is not in the spirit of MVC...
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
var scope = angular.element(document.getElementById(addressType)).scope();
scope.$apply(function () {
scope.model.value.address = document.getElementById("autocomplete").value;
scope.model.value.streetNumber = document.getElementById("street_number").value;
scope.model.value.route = document.getElementById("route").value;
scope.model.value.locality = document.getElementById("locality").value;
scope.model.value.state = document.getElementById("administrative_area_level_1").value;
scope.model.value.zipcode = document.getElementById("postal_code").value;
scope.model.value.country = document.getElementById("country").value;
})
Making a custom property editor for looking up addresses using Google Maps API
Hi all
I'm trying to do the following.
I want a custom property editor that contains an address lookup feature. It consists of an input field, coupled to the GoogleMaps api. When a user clicks on an item from the autocomplete dropdown, then it fills out a small form with all the address info like so:
https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
I've already fiddled around on a local instance of umbraco v7. I can recreate the form in a property Editor, I can use the api in a seperate .js file and from there fill out the form. This approach gives one problem. Saving. Because in the example the input fields are manipulated by javascript, AngularJS will not count this as a change. Apparantly I cannot use functions like $setDirty() to fix this.
How should I solve this according to umbraco convention? use the Google api in the controller en return values from there instead of a seperate javascript file? Or is the previous approach also plausible but am I missing something?
I would love to hear your thoughts/advice on this.
/Bert
I think you are missing $scope.apply ? :)
I prefer wrapping the "back and forth"-stuff in $timeout(function(){[...handler...})-blocks
In my own directive for doing this (outside umbraco) I have:
The above code can then be used like
I solved it as follows.
this is the function that programmatically changes the form input fields + what I entered to get into my controller to get it to save. (starting at "var scope") (thanks for the hint about $scope.apply() !!). But this approach feels 'wrong' as it is not in the spirit of MVC...
}
Hi
Where do you place the API Key (https://maps.googleapis.com/maps/api/js?key=*)
regards Nitin
is working on a reply...