$(function() { ListingEditor.register('pageMap', { fields: ['latitude', 'longitude', 'map_latitude', 'map_longitude', 'map_zoom'], refresh: function(){ ListingMap.refresh(); } }); }); Geocoder = { refresh: function(ignoreSuburb){ var ignoreSuburb = ignoreSuburb; if (ListingEditor.options['mode'] !== "selected"){ var isBlank = function(text){ if (text == null) return true; $.trim(text) == ""; } var listing = ListingEditor.workingListing(); var addressLines = []; var context = this; var showMarker = true; if (!isBlank(listing.street_name)) { addressLines.push((listing.street_number || '') + ' ' + listing.street_name); } if (!isBlank(listing.location_name) && !ignoreSuburb) { addressLines.push(listing.location_name); } if (!isBlank(listing.area_name)) { addressLines.push(listing.area_name.replace(/\s(region|district)$/i, '')); } if (addressLines.length < 1){ if (!isBlank(listing.postal_address_suburb)) { addressLines.push(listing.postal_address_suburb); } if (!isBlank(listing.postal_address_city)) { addressLines.push(listing.postal_address_city); } } if (addressLines.length < 1) { ListingEditor.updateSubmission('latitude', 0); ListingEditor.updateSubmission('longitude', 0); ListingEditor.updateSubmission('map_zoom', 0); } var geocoder = new GClientGeocoder(); geocoder.setBaseCountryCode('nz'); addressLines.push("NZ"); geocoder.getLocations(addressLines.join(', '), function(locations) { if (locations.Placemark && locations.Placemark.length > 0) { var newLat = locations.Placemark[0].Point.coordinates[1]; var newLng = locations.Placemark[0].Point.coordinates[0]; if (listing.latitude !== newLat && listing.longitude !== newLng) { var difference = listing.latitude - newLat; if ((difference < 0 || difference > 0.0001) && (difference >= 0 || difference < -0.0001)) { ListingEditor.updateSubmission('map_latitude', newLat); ListingEditor.updateSubmission('map_longitude', newLng); ListingEditor.updateSubmission('latitude', newLat); ListingEditor.updateSubmission('longitude', newLng); if (listing.map_zoom <= 8) { if (addressLines.length <= 1) { ListingEditor.updateSubmission('map_zoom', 8); } else { if (addressLines.length <= 3){ ListingEditor.updateSubmission('map_zoom', 14); } else{ ListingEditor.updateSubmission('map_zoom', 15); } } } } } if (locations.Placemark[0].AddressDetails.Country.Locality && locations.Placemark[0].AddressDetails.Country.Locality.PostalCode) { var postcode = locations.Placemark[0].AddressDetails.Country.Locality.PostalCode.PostalCodeNumber; ListingEditor.updateSubmission('street_address_postcode', postcode); } else { ListingEditor.updateSubmission('street_address_postcode', null); } ListingEditor.refresh('#physicalAddress'); } else { if (!ignoreSuburb){ Geocoder.refresh(true); } else{ ListingEditor.updateSubmission('latitude', 0); ListingEditor.updateSubmission('longitude', 0); ListingEditor.updateSubmission('map_zoom', 8); } } ListingMap.refresh("#pageMap"); }); } } }