first commit
This commit is contained in:
@@ -0,0 +1,310 @@
|
||||
/*
|
||||
* jQuery UI addresspicker @VERSION
|
||||
*
|
||||
* Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
|
||||
* Dual licensed under the MIT or GPL Version 2 licenses.
|
||||
* http://jquery.org/license
|
||||
*
|
||||
* Depends:
|
||||
* jquery.ui.core.js
|
||||
* jquery.ui.widget.js
|
||||
* jquery.ui.autocomplete.js
|
||||
*/
|
||||
(function( $, undefined ) {
|
||||
|
||||
$.widget( "ui.addresspicker", {
|
||||
options: {
|
||||
appendAddressString: "",
|
||||
draggableMarker: true,
|
||||
regionBias: null,
|
||||
bounds: '',
|
||||
componentsFilter:'',
|
||||
updateCallback: null,
|
||||
reverseGeocode: false,
|
||||
autocomplete: 'default',
|
||||
language: '',
|
||||
mapOptions: {
|
||||
zoom: 5,
|
||||
center: new google.maps.LatLng(46, 2),
|
||||
scrollwheel: false,
|
||||
mapTypeId: google.maps.MapTypeId.ROADMAP
|
||||
},
|
||||
elements: {
|
||||
map: false,
|
||||
lat: false,
|
||||
lng: false,
|
||||
street_number: false,
|
||||
route: false,
|
||||
locality: false,
|
||||
sublocality: false,
|
||||
administrative_area_level_3: false,
|
||||
administrative_area_level_2: false,
|
||||
administrative_area_level_1: false,
|
||||
country: false,
|
||||
postal_code: false,
|
||||
type: false
|
||||
|
||||
},
|
||||
autocomplete: '' // could be autocomplete: "bootstrap" to use bootstrap typeahead autocomplete instead of jQueryUI
|
||||
},
|
||||
|
||||
marker: function() {
|
||||
return this.gmarker;
|
||||
},
|
||||
|
||||
map: function() {
|
||||
return this.gmap;
|
||||
},
|
||||
|
||||
updatePosition: function() {
|
||||
this._updatePosition(this.gmarker.getPosition());
|
||||
},
|
||||
|
||||
reloadPosition: function() {
|
||||
this.gmarker.setVisible(true);
|
||||
this.gmarker.setPosition(new google.maps.LatLng(this.lat.val, this.lng.val));
|
||||
this.gmap.setCenter(this.gmarker.getPosition());
|
||||
},
|
||||
|
||||
resize: function() {
|
||||
google.maps.event.trigger(this.gmap, 'resize')
|
||||
},
|
||||
|
||||
selected: function() {
|
||||
return this.selectedResult;
|
||||
},
|
||||
_mapped: {},
|
||||
_create: function() {
|
||||
var self = this;
|
||||
this.geocoder = {
|
||||
geocode: function(options, callback)
|
||||
{
|
||||
jQuery.ajax({
|
||||
url: "https://maps.googleapis.com/maps/api/geocode/json?" + jQuery.param(options) + '&sensor=false',
|
||||
type: "GET",
|
||||
success: function(data) {
|
||||
callback(data.results, data.status);
|
||||
}
|
||||
});
|
||||
}
|
||||
//new google.maps.Geocoder();
|
||||
};
|
||||
|
||||
if (this.options.autocomplete === 'bootstrap') {
|
||||
this.element.typeahead({
|
||||
source: function(query, process) {
|
||||
self._mapped = {};
|
||||
var response = function(results) {
|
||||
var labels = [];
|
||||
for (var i = 0; i < results.length; i++) {
|
||||
self._mapped[results[i].label] = results[i];
|
||||
labels.push(results[i].label);
|
||||
}
|
||||
process(labels);
|
||||
};
|
||||
var request = {term: query};
|
||||
self._geocode(request, response);
|
||||
},
|
||||
updater: function(item) {
|
||||
var ui = {item: self._mapped[item]}
|
||||
self._focusAddress(null, ui);
|
||||
self._selectAddress(null, ui);
|
||||
return item;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
this.element.autocomplete($.extend({
|
||||
source: $.proxy(this._geocode, this),
|
||||
focus: $.proxy(this._focusAddress, this),
|
||||
select: $.proxy(this._selectAddress, this)
|
||||
}), this.options.autocomplete);
|
||||
}
|
||||
|
||||
this.lat = $(this.options.elements.lat);
|
||||
this.lng = $(this.options.elements.lng);
|
||||
this.street_number = $(this.options.elements.street_number);
|
||||
this.route = $(this.options.elements.route);
|
||||
this.locality = $(this.options.elements.locality);
|
||||
this.sublocality = $(this.options.elements.sublocality);
|
||||
this.administrative_area_level_3 = $(this.options.elements.administrative_area_level_3);
|
||||
this.administrative_area_level_2 = $(this.options.elements.administrative_area_level_2);
|
||||
this.administrative_area_level_1 = $(this.options.elements.administrative_area_level_1);
|
||||
this.country = $(this.options.elements.country);
|
||||
this.postal_code = $(this.options.elements.postal_code);
|
||||
this.type = $(this.options.elements.type);
|
||||
if (this.options.elements.map) {
|
||||
this.mapElement = $(this.options.elements.map);
|
||||
this._initMap();
|
||||
}
|
||||
},
|
||||
|
||||
_initMap: function() {
|
||||
if (this.lat && this.lat.val()) {
|
||||
this.options.mapOptions.center = new google.maps.LatLng(this.lat.val(), this.lng.val());
|
||||
}
|
||||
|
||||
this.gmap = new google.maps.Map(this.mapElement[0], this.options.mapOptions);
|
||||
this.gmarker = new google.maps.Marker({
|
||||
position: this.options.mapOptions.center,
|
||||
map:this.gmap,
|
||||
draggable: this.options.draggableMarker});
|
||||
google.maps.event.addListener(this.gmarker, 'dragend', $.proxy(this._markerMoved, this));
|
||||
this.gmarker.setVisible(false);
|
||||
},
|
||||
|
||||
_updatePosition: function(location) {
|
||||
if (this.lat) {
|
||||
this.lat.val(location.lat());
|
||||
}
|
||||
if (this.lng) {
|
||||
this.lng.val(location.lng());
|
||||
}
|
||||
},
|
||||
|
||||
_addressParts: {street_number: null, route: null, locality: null, sublocality: null,
|
||||
administrative_area_level_3: null, administrative_area_level_2: null,
|
||||
administrative_area_level_1: null, country: null, postal_code:null, type: null},
|
||||
|
||||
_updateAddressParts: function(geocodeResult){
|
||||
|
||||
parsedResult = this._parseGeocodeResult(geocodeResult);
|
||||
|
||||
for (addressPart in this._addressParts){
|
||||
if (this[addressPart]){
|
||||
if (parsedResult[addressPart] !== false){
|
||||
this[addressPart].val(parsedResult[addressPart]);
|
||||
} else {
|
||||
this[addressPart].val('');
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_updateAddressPartsViaReverseGeocode: function(location){
|
||||
this.geocoder.geocode({'latlng': location.lat() + "," + location.lng()}, $.proxy(function(results, status){
|
||||
if (status == google.maps.GeocoderStatus.OK){
|
||||
|
||||
this._updateAddressParts(results[0]);
|
||||
this.element.val(results[0].formatted_address);
|
||||
this.selectedResult = results[0];
|
||||
|
||||
if (this.options.updateCallback) {
|
||||
this.options.updateCallback(this.selectedResult, this._parseGeocodeResult(this.selectedResult));
|
||||
}
|
||||
}
|
||||
}, this));
|
||||
},
|
||||
|
||||
_parseGeocodeResult: function(geocodeResult){
|
||||
|
||||
var parsed = this._parseLatAndLng(geocodeResult.geometry.location);
|
||||
|
||||
for (var addressPart in this._addressParts){
|
||||
parsed[addressPart] = this._findInfo(geocodeResult, addressPart);
|
||||
}
|
||||
|
||||
parsed.type = geocodeResult.types[0];
|
||||
|
||||
return parsed;
|
||||
},
|
||||
|
||||
_parseLatAndLng: function(location){
|
||||
var longitude, latitude;
|
||||
|
||||
if(typeof(location.lat) === 'function'){
|
||||
latitude = location.lat();
|
||||
longitude = location.lng();
|
||||
} else {
|
||||
latitude = location.lat;
|
||||
longitude = location.lng;
|
||||
}
|
||||
|
||||
return { lat: latitude, lng: longitude };
|
||||
},
|
||||
|
||||
_markerMoved: function() {
|
||||
this._updatePosition(this.gmarker.getPosition());
|
||||
|
||||
if (this.options.reverseGeocode){
|
||||
this._updateAddressPartsViaReverseGeocode(this.gmarker.getPosition());
|
||||
}
|
||||
},
|
||||
|
||||
// Autocomplete source method: fill its suggests with google geocoder results
|
||||
_geocode: function(request, response) {
|
||||
var address = request.term, self = this;
|
||||
this.geocoder.geocode({
|
||||
'language': this.options.language,
|
||||
'address': address + this.options.appendAddressString,
|
||||
'region': this.options.regionBias,
|
||||
'bounds': this.options.bounds,
|
||||
'components': this.options.componentsFilter
|
||||
}, function(results, status) {
|
||||
if (status == google.maps.GeocoderStatus.OK && results) {
|
||||
for (var i = 0; i < results.length; i++) {
|
||||
result = results[i]
|
||||
g = result.geometry
|
||||
g.location = new google.maps.LatLng(g.location.lat, g.location.lng);
|
||||
g.viewport = new google.maps.LatLngBounds(
|
||||
new google.maps.LatLng(g.viewport.southwest.lat, g.viewport.southwest.lng),
|
||||
new google.maps.LatLng(g.viewport.northeast.lat, g.viewport.northeast.lng)
|
||||
)
|
||||
result.label = results[i].formatted_address;
|
||||
}
|
||||
}
|
||||
response(results);
|
||||
})
|
||||
},
|
||||
|
||||
_findInfo: function(result, type) {
|
||||
for (var i = 0; i < result.address_components.length; i++) {
|
||||
var component = result.address_components[i];
|
||||
if (component.types.indexOf(type) !=-1) {
|
||||
return component.long_name;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
},
|
||||
|
||||
_focusAddress: function(event, ui) {
|
||||
var address = ui.item;
|
||||
if (!address) {
|
||||
return;
|
||||
}
|
||||
if (this.gmarker) {
|
||||
this.gmarker.setPosition(address.geometry.location);
|
||||
this.gmarker.setVisible(true);
|
||||
this.gmap.fitBounds(address.geometry.viewport);
|
||||
}
|
||||
|
||||
this._updatePosition(address.geometry.location);
|
||||
|
||||
this._updateAddressParts(address);
|
||||
|
||||
},
|
||||
|
||||
_selectAddress: function(event, ui) {
|
||||
this.selectedResult = ui.item;
|
||||
if (this.options.updateCallback) {
|
||||
this.options.updateCallback(this.selectedResult, this._parseGeocodeResult(this.selectedResult));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$.extend( $.ui.addresspicker, {
|
||||
version: "@VERSION"
|
||||
});
|
||||
|
||||
// make IE think it doesn't suck
|
||||
if(!Array.indexOf){
|
||||
Array.prototype.indexOf = function(obj){
|
||||
for(var i=0; i<this.length; i++){
|
||||
if(this[i]==obj){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
})( jQuery );
|
||||
@@ -0,0 +1,270 @@
|
||||
(function ( $ ) {
|
||||
|
||||
/**
|
||||
* Holds google map object and related utility entities.
|
||||
* @constructor
|
||||
*/
|
||||
function GMapContext(domElement, options) {
|
||||
var _map = new google.maps.Map(domElement, options);
|
||||
var _marker = new google.maps.Marker({
|
||||
position: new google.maps.LatLng(54.19335, -3.92695),
|
||||
map: _map,
|
||||
title: "Drag Me",
|
||||
draggable: options.draggable
|
||||
});
|
||||
return {
|
||||
map: _map,
|
||||
marker: _marker,
|
||||
circle: null,
|
||||
location: _marker.position,
|
||||
radius: options.radius,
|
||||
locationName: options.locationName,
|
||||
settings: options.settings,
|
||||
domContainer: domElement,
|
||||
geodecoder: new google.maps.Geocoder()
|
||||
}
|
||||
}
|
||||
|
||||
// Utility functions for Google Map Manipulations
|
||||
var GmUtility = {
|
||||
/**
|
||||
* Draw a circle over the the map. Returns circle object.
|
||||
* Also writes new circle object in gmapContext.
|
||||
*
|
||||
* @param center - LatLng of the center of the circle
|
||||
* @param radius - radius in meters
|
||||
* @param gmapContext - context
|
||||
* @param options
|
||||
*/
|
||||
drawCircle: function(gmapContext, center, radius, options) {
|
||||
if (gmapContext.circle != null) {
|
||||
gmapContext.circle.setMap(null);
|
||||
}
|
||||
if (radius > 0) {
|
||||
radius *= 1;
|
||||
options = $.extend({
|
||||
strokeColor: "#0000FF",
|
||||
strokeOpacity: 0.35,
|
||||
strokeWeight: 2,
|
||||
fillColor: "#0000FF",
|
||||
fillOpacity: 0.20
|
||||
}, options);
|
||||
options.map = gmapContext.map;
|
||||
options.radius = radius;
|
||||
options.center = center;
|
||||
gmapContext.circle = new google.maps.Circle(options);
|
||||
return gmapContext.circle;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
/**
|
||||
*
|
||||
* @param gMapContext
|
||||
* @param location
|
||||
* @param callback
|
||||
*/
|
||||
setPosition: function(gMapContext, location, callback) {
|
||||
gMapContext.location = location;
|
||||
gMapContext.marker.setPosition(location);
|
||||
gMapContext.map.panTo(location);
|
||||
this.drawCircle(gMapContext, location, gMapContext.radius, {});
|
||||
if (gMapContext.settings.enableReverseGeocode) {
|
||||
gMapContext.geodecoder.geocode({latLng: gMapContext.location}, function(results, status){
|
||||
if (status == google.maps.GeocoderStatus.OK && results.length > 0){
|
||||
gMapContext.locationName = results[0].formatted_address;
|
||||
}
|
||||
if (callback) {
|
||||
callback.call(this, gMapContext);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (callback) {
|
||||
callback.call(this, gMapContext);
|
||||
}
|
||||
}
|
||||
|
||||
},
|
||||
locationFromLatLng: function(lnlg) {
|
||||
return {latitude: lnlg.lat(), longitude: lnlg.lng()}
|
||||
}
|
||||
}
|
||||
|
||||
function isPluginApplied(domObj) {
|
||||
return getContextForElement(domObj) != undefined;
|
||||
}
|
||||
|
||||
function getContextForElement(domObj) {
|
||||
return $(domObj).data("locationpicker");
|
||||
}
|
||||
|
||||
function updateInputValues(inputBinding, gmapContext){
|
||||
if (!inputBinding) return;
|
||||
var currentLocation = GmUtility.locationFromLatLng(gmapContext.location);
|
||||
if (inputBinding.latitudeInput) {
|
||||
inputBinding.latitudeInput.val(currentLocation.latitude);
|
||||
}
|
||||
if (inputBinding.longitudeInput) {
|
||||
inputBinding.longitudeInput.val(currentLocation.longitude);
|
||||
}
|
||||
if (inputBinding.radiusInput) {
|
||||
inputBinding.radiusInput.val(gmapContext.radius);
|
||||
}
|
||||
if (inputBinding.locationNameInput) {
|
||||
inputBinding.locationNameInput.val(gmapContext.locationName);
|
||||
}
|
||||
}
|
||||
|
||||
function setupInputListenersInput(inputBinding, gmapContext) {
|
||||
if (inputBinding) {
|
||||
if (inputBinding.radiusInput){
|
||||
inputBinding.radiusInput.on("change", function() {
|
||||
gmapContext.radius = $(this).val();
|
||||
GmUtility.setPosition(gmapContext, gmapContext.location, function(context){
|
||||
context.settings.onchanged(GmUtility.locationFromLatLng(context.location), context.radius, false);
|
||||
});
|
||||
});
|
||||
}
|
||||
if (inputBinding.locationNameInput && gmapContext.settings.enableAutocomplete) {
|
||||
gmapContext.autocomplete = new google.maps.places.Autocomplete(inputBinding.locationNameInput.get(0));
|
||||
google.maps.event.addListener(gmapContext.autocomplete, 'place_changed', function() {
|
||||
var place = gmapContext.autocomplete.getPlace();
|
||||
if (!place.geometry) {
|
||||
gmapContext.settings.onlocationnotfound(place.name);
|
||||
return;
|
||||
}
|
||||
GmUtility.setPosition(gmapContext, place.geometry.location, function(context) {
|
||||
updateInputValues(inputBinding, context);
|
||||
context.settings.onchanged(GmUtility.locationFromLatLng(context.location), context.radius, false);
|
||||
});
|
||||
});
|
||||
}
|
||||
if (inputBinding.latitudeInput) {
|
||||
inputBinding.latitudeInput.on("change", function() {
|
||||
GmUtility.setPosition(gmapContext, new google.maps.LatLng($(this).val(), gmapContext.location.lng()), function(context){
|
||||
context.settings.onchanged(GmUtility.locationFromLatLng(context.location), context.radius, false);
|
||||
});
|
||||
});
|
||||
}
|
||||
if (inputBinding.longitudeInput) {
|
||||
inputBinding.longitudeInput.on("change", function() {
|
||||
GmUtility.setPosition(gmapContext, new google.maps.LatLng(gmapContext.location.lat(), $(this).val()), function(context){
|
||||
context.settings.onchanged(GmUtility.locationFromLatLng(context.location), context.radius, false);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialization:
|
||||
* $("#myMap").locationpicker(options);
|
||||
* @param options
|
||||
* @param params
|
||||
* @returns {*}
|
||||
*/
|
||||
$.fn.locationpicker = function( options, params ) {
|
||||
if (typeof options == 'string') { // Command provided
|
||||
var _targetDomElement = this.get(0);
|
||||
// Plug-in is not applied - nothing to do.
|
||||
if (!isPluginApplied(_targetDomElement)) return;
|
||||
var gmapContext = getContextForElement(_targetDomElement);
|
||||
switch (options) {
|
||||
case "location":
|
||||
if (params == undefined) { // Getter
|
||||
var location = GmUtility.locationFromLatLng(gmapContext.location);
|
||||
location.radius = gmapContext.radius;
|
||||
location.name = gmapContext.locationName;
|
||||
return location;
|
||||
} else { // Setter
|
||||
if (params.radius) {
|
||||
gmapContext.radius = params.radius;
|
||||
}
|
||||
GmUtility.setPosition(gmapContext, new google.maps.LatLng(params.latitude, params.longitude), function(gmapContext) {
|
||||
updateInputValues(gmapContext.settings.inputBinding, gmapContext);
|
||||
});
|
||||
}
|
||||
break;
|
||||
case "subscribe":
|
||||
/**
|
||||
* Provides interface for subscribing for GoogleMap events.
|
||||
* See Google API documentation for details.
|
||||
* Parameters:
|
||||
* - event: string, name of the event
|
||||
* - callback: function, callback function to be invoked
|
||||
*/
|
||||
if (options == undefined) { // Getter is not available
|
||||
return null;
|
||||
} else {
|
||||
var event = params.event;
|
||||
var callback = params.callback;
|
||||
if (!event || ! callback) {
|
||||
console.error("LocationPicker: Invalid arguments for method \"subscribe\"")
|
||||
return null;
|
||||
}
|
||||
google.maps.event.addListener(gmapContext.map, event, callback);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return this.each(function() {
|
||||
var $target = $(this);
|
||||
// If plug-in hasn't been applied before - initialize, otherwise - skip
|
||||
if (isPluginApplied(this)) return;
|
||||
// Plug-in initialization is required
|
||||
// Defaults
|
||||
var settings = $.extend({}, $.fn.locationpicker.defaults, options );
|
||||
// Initialize
|
||||
var gmapContext = new GMapContext(this, {
|
||||
zoom: settings.zoom,
|
||||
center: new google.maps.LatLng(settings.location.latitude, settings.location.longitude),
|
||||
mapTypeId: google.maps.MapTypeId.ROADMAP,
|
||||
mapTypeControl: false,
|
||||
disableDoubleClickZoom: false,
|
||||
scrollwheel: settings.scrollwheel,
|
||||
streetViewControl: false,
|
||||
radius: settings.radius,
|
||||
locationName: settings.locationName,
|
||||
settings: settings,
|
||||
draggable: settings.draggable
|
||||
});
|
||||
$target.data("locationpicker", gmapContext);
|
||||
// Subscribe GMap events
|
||||
google.maps.event.addListener(gmapContext.marker, "dragend", function(event) {
|
||||
GmUtility.setPosition(gmapContext, gmapContext.marker.position, function(context){
|
||||
var currentLocation = GmUtility.locationFromLatLng(gmapContext.location);
|
||||
context.settings.onchanged(currentLocation, context.radius, true);
|
||||
updateInputValues(gmapContext.settings.inputBinding, gmapContext);
|
||||
});
|
||||
});
|
||||
GmUtility.setPosition(gmapContext, new google.maps.LatLng(settings.location.latitude, settings.location.longitude), function(context){
|
||||
updateInputValues(settings.inputBinding, gmapContext);
|
||||
context.settings.oninitialized($target);
|
||||
});
|
||||
// Set up input bindings if needed
|
||||
setupInputListenersInput(settings.inputBinding, gmapContext);
|
||||
});
|
||||
};
|
||||
$.fn.locationpicker.defaults = {
|
||||
location: {latitude: 40.7324319, longitude: -73.82480799999996},
|
||||
locationName: "",
|
||||
radius: 500,
|
||||
zoom: 15,
|
||||
scrollwheel: true,
|
||||
inputBinding: {
|
||||
latitudeInput: null,
|
||||
longitudeInput: null,
|
||||
radiusInput: null,
|
||||
locationNameInput: null
|
||||
},
|
||||
enableAutocomplete: false,
|
||||
enableReverseGeocode: true,
|
||||
draggable: true,
|
||||
onchanged: function(currentLocation, radius, isMarkerDropped) {},
|
||||
onlocationnotfound: function(locationName) {},
|
||||
oninitialized: function (component) {}
|
||||
|
||||
}
|
||||
|
||||
}( jQuery ));
|
||||
@@ -0,0 +1,232 @@
|
||||
(function() {
|
||||
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
|
||||
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
||||
hasProp = {}.hasOwnProperty;
|
||||
|
||||
(function($) {
|
||||
this.AddressPickerResult = (function() {
|
||||
function AddressPickerResult(placeResult, fromReverseGeocoding) {
|
||||
this.placeResult = placeResult;
|
||||
this.fromReverseGeocoding = fromReverseGeocoding != null ? fromReverseGeocoding : false;
|
||||
this.latitude = this.placeResult.geometry.location.lat();
|
||||
this.longitude = this.placeResult.geometry.location.lng();
|
||||
}
|
||||
|
||||
AddressPickerResult.prototype.addressTypes = function() {
|
||||
var component, i, j, len, len1, ref, ref1, type, types;
|
||||
types = [];
|
||||
ref = this.addressComponents();
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
component = ref[i];
|
||||
ref1 = component.types;
|
||||
for (j = 0, len1 = ref1.length; j < len1; j++) {
|
||||
type = ref1[j];
|
||||
if (types.indexOf(type) === -1) {
|
||||
types.push(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
return types;
|
||||
};
|
||||
|
||||
AddressPickerResult.prototype.addressComponents = function() {
|
||||
return this.placeResult.address_components || [];
|
||||
};
|
||||
|
||||
AddressPickerResult.prototype.address = function() {
|
||||
return this.placeResult.formatted_address;
|
||||
};
|
||||
|
||||
AddressPickerResult.prototype.nameForType = function(type, shortName) {
|
||||
var component, i, len, ref;
|
||||
if (shortName == null) {
|
||||
shortName = false;
|
||||
}
|
||||
ref = this.addressComponents();
|
||||
for (i = 0, len = ref.length; i < len; i++) {
|
||||
component = ref[i];
|
||||
if (component.types.indexOf(type) !== -1) {
|
||||
return (shortName ? component.short_name : component.long_name);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
AddressPickerResult.prototype.lat = function() {
|
||||
return this.latitude;
|
||||
};
|
||||
|
||||
AddressPickerResult.prototype.lng = function() {
|
||||
return this.longitude;
|
||||
};
|
||||
|
||||
AddressPickerResult.prototype.setLatLng = function(latitude, longitude) {
|
||||
this.latitude = latitude;
|
||||
this.longitude = longitude;
|
||||
};
|
||||
|
||||
AddressPickerResult.prototype.isAccurate = function() {
|
||||
return !this.placeResult.geometry.viewport;
|
||||
};
|
||||
|
||||
AddressPickerResult.prototype.isReverseGeocoding = function() {
|
||||
return this.fromReverseGeocoding;
|
||||
};
|
||||
|
||||
return AddressPickerResult;
|
||||
|
||||
})();
|
||||
return this.AddressPicker = (function(superClass) {
|
||||
extend(AddressPicker, superClass);
|
||||
|
||||
function AddressPicker(options) {
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
this.markerDragged = bind(this.markerDragged, this);
|
||||
this.updateBoundsForPlace = bind(this.updateBoundsForPlace, this);
|
||||
this.updateMap = bind(this.updateMap, this);
|
||||
this.options = $.extend({
|
||||
local: [],
|
||||
datumTokenizer: function(d) {
|
||||
return Bloodhound.tokenizers.whitespace(d.num);
|
||||
},
|
||||
queryTokenizer: Bloodhound.tokenizers.whitespace,
|
||||
autocompleteService: {
|
||||
types: ["geocode"]
|
||||
},
|
||||
zoomForLocation: 16,
|
||||
reverseGeocoding: false,
|
||||
placeDetails: true,
|
||||
remote: 'fakeRemote'
|
||||
}, options);
|
||||
AddressPicker.__super__.constructor.call(this, this.options);
|
||||
if (this.options.map) {
|
||||
this.initMap();
|
||||
}
|
||||
this.placeService = new google.maps.places.PlacesService(document.createElement('div'));
|
||||
}
|
||||
|
||||
AddressPicker.prototype.bindDefaultTypeaheadEvent = function(typeahead) {
|
||||
typeahead.bind("typeahead:selected", this.updateMap);
|
||||
typeahead.bind("typeahead:autocompleted", this.updateMap);
|
||||
return typeahead.bind("typeahead:cursorchanged", this.updateMap);
|
||||
};
|
||||
|
||||
AddressPicker.prototype.initMap = function() {
|
||||
var markerOptions, ref, ref1;
|
||||
if ((ref = this.options) != null ? (ref1 = ref.map) != null ? ref1.gmap : void 0 : void 0) {
|
||||
this.map = this.options.map.gmap;
|
||||
} else {
|
||||
this.mapOptions = $.extend({
|
||||
zoom: 3,
|
||||
center: new google.maps.LatLng(0, 0),
|
||||
mapTypeId: google.maps.MapTypeId.ROADMAP,
|
||||
boundsForLocation: this.updateBoundsForPlace
|
||||
}, this.options.map);
|
||||
this.map = new google.maps.Map($(this.mapOptions.id)[0], this.mapOptions);
|
||||
}
|
||||
this.lastResult = null;
|
||||
markerOptions = $.extend({
|
||||
draggable: true,
|
||||
visible: false,
|
||||
position: this.map.getCenter(),
|
||||
map: this.map
|
||||
}, this.options.marker || {});
|
||||
this.marker = new google.maps.Marker(markerOptions);
|
||||
if (markerOptions.draggable) {
|
||||
return google.maps.event.addListener(this.marker, 'dragend', this.markerDragged);
|
||||
}
|
||||
};
|
||||
|
||||
AddressPicker.prototype.search = function(query, sync, async) {
|
||||
var service;
|
||||
service = new google.maps.places.AutocompleteService();
|
||||
this.options.autocompleteService.input = query;
|
||||
return service.getPlacePredictions(this.options.autocompleteService, (function(_this) {
|
||||
return function(predictions) {
|
||||
$(_this).trigger('addresspicker:predictions', [predictions]);
|
||||
return async(predictions);
|
||||
};
|
||||
})(this));
|
||||
};
|
||||
|
||||
AddressPicker.prototype.updateMap = function(event, place) {
|
||||
if (this.options.placeDetails) {
|
||||
return this.placeService.getDetails(place, (function(_this) {
|
||||
return function(response) {
|
||||
var ref;
|
||||
_this.lastResult = new AddressPickerResult(response);
|
||||
if (_this.marker) {
|
||||
_this.marker.setPosition(response.geometry.location);
|
||||
_this.marker.setVisible(true);
|
||||
}
|
||||
if (_this.map) {
|
||||
if ((ref = _this.mapOptions) != null) {
|
||||
ref.boundsForLocation(response);
|
||||
}
|
||||
}
|
||||
return $(_this).trigger('addresspicker:selected', _this.lastResult);
|
||||
};
|
||||
})(this));
|
||||
} else {
|
||||
return $(this).trigger('addresspicker:selected', place);
|
||||
}
|
||||
};
|
||||
|
||||
AddressPicker.prototype.updateBoundsForPlace = function(response) {
|
||||
if (response.geometry.viewport) {
|
||||
return this.map.fitBounds(response.geometry.viewport);
|
||||
} else {
|
||||
this.map.setCenter(response.geometry.location);
|
||||
return this.map.setZoom(this.options.zoomForLocation);
|
||||
}
|
||||
};
|
||||
|
||||
AddressPicker.prototype.markerDragged = function() {
|
||||
if (this.options.reverseGeocoding) {
|
||||
return this.reverseGeocode(this.marker.getPosition());
|
||||
} else {
|
||||
if (this.lastResult) {
|
||||
this.lastResult.setLatLng(this.marker.getPosition().lat(), this.marker.getPosition().lng());
|
||||
} else {
|
||||
this.lastResult = new AddressPickerResult({
|
||||
geometry: {
|
||||
location: this.marker.getPosition()
|
||||
}
|
||||
});
|
||||
}
|
||||
return $(this).trigger('addresspicker:selected', this.lastResult);
|
||||
}
|
||||
};
|
||||
|
||||
AddressPicker.prototype.reverseGeocode = function(position) {
|
||||
if (this.geocoder == null) {
|
||||
this.geocoder = new google.maps.Geocoder();
|
||||
}
|
||||
return this.geocoder.geocode({
|
||||
location: position
|
||||
}, (function(_this) {
|
||||
return function(results) {
|
||||
if (results && results.length > 0) {
|
||||
_this.lastResult = new AddressPickerResult(results[0], true);
|
||||
return $(_this).trigger('addresspicker:selected', _this.lastResult);
|
||||
}
|
||||
};
|
||||
})(this));
|
||||
};
|
||||
|
||||
AddressPicker.prototype.getGMap = function() {
|
||||
return this.map;
|
||||
};
|
||||
|
||||
AddressPicker.prototype.getGMarker = function() {
|
||||
return this.marker;
|
||||
};
|
||||
|
||||
return AddressPicker;
|
||||
|
||||
})(Bloodhound);
|
||||
})(jQuery);
|
||||
|
||||
}).call(this);
|
||||
Reference in New Issue
Block a user