• This repository has been archived on 09/Nov/2017
  • Stars
    star
    1,227
  • Rank 38,235 (Top 0.8 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

jQuery Geocoding and Places Autocomplete Plugin

$.geocomplete() - Version 1.7.0

jQuery Geocoding and Places Autocomplete Plugin

An advanced jQuery plugin that wraps the Google Maps API's Geocoding and Places Autocomplete services. You simply provide an input that lets you search for locations with a nice autocomplete dropdown. Optionally add a container to show an interactive map and a form that will be populated with the address details.

View the annotated source.

⚠️ NOT MAINTAINED

Please note: This project is not maintained anymore. We do not use jQuery in production and switched to React about two years ago. Feel free to discuss issues here but please do not expect new features or changes in the default behavior.

For a new and way more flexible solution, see our React Geosuggest plugin.

If you would like to take over the project, please let us know! There are many people out there who would be happy to have new supporters.

Basic Usage

To convert an input into an autocomplete field, simply call the Geocomplete plugin:

$("input").geocomplete();  // Option 1: Call on element.
$.fn.geocomplete("input"); // Option 2: Pass element as argument.

Examples

Here is a list of basic uses:

  • Simple - Single input with an event logger.
  • Map - Adding a map.
  • Location - Adding a default location.
  • Form - Populate form fields.
  • Attribute - Using custom attributes to populate data.
  • Multiple results - Handling multiple results from the geocoder.
  • Draggable - A draggable marker to redefine the position.
  • Styled - A styled map example.

Requirements

Make sure you include the Google Maps API with the Places Library before loading this plugin as described here.

<script src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
<script src="jquery.geocomplete.js"></script>

If you use the plugin without showing a map you must display the "powered by Google" logo under the text field.

Trigger Request

To trigger a geocoding request from outside (eg. when hitting the "find" button), simply trigger the "geocode" event on the element.

$("input").geocomplete();

// Trigger geocoding request.
$("button.find").click(function(){
  $("input").trigger("geocode");
});

Adding a Map Preview

To link the geocode results with an interactive map, you can pass map as an option to the plugin.

$("#my_input").geocomplete({
  map: "#my_map"
});

The map option might be a selector, a jQuery object or a DOM element.

Populate Form Data

You can pass details as an option to specify a container that will be populated when a geocoding request was successful.

By default the plugin analyses the name attribute of the containers child nodes and replaces the content. You can override the detailsAttribute to use another attribute such as data-geo.

If the element is an input, the value will be replaced otherwise the plugin overrides the current text.

If you have multiple geocomplete fields on a page, use detailsScope option scope your 'details' container.

Note: Some address components such as "country" return an additional short_name. You can access them by simply adding _short at the end of the type.

Simple Example:

<form>
  Latitude:   <input name="lat" type="text" value="">
  Longitude:  <input name="lng" type="text" value="">
  Address:    <input name="formatted_address" type="text" value="">
</form>
$("input").geocomplete({ details: "form" });

Advanced Example:

<div class="details">
  Latitude:     <span data-geo="lat" />
  Longitude:    <span data-geo="lng" />
  Address:      <span data-geo="formatted_address" />
  Country Code: <span data-geo="country_short" />
</div>
$("input").geocomplete({
  details: ".details",
  detailsAttribute: "data-geo"
});

List of Options

The following options might be passed to the plugin call. If you omit them, they fall back to the default.

Example:

$("#my_input").geocomplete({
  map: "#my_map",
  mapOptions: {
    zoom: 10
  },
  markerOptions: {
    draggable: true
  },
  details: "#my_form"
});
  • map - Might be a selector, a jQuery object or a DOM element. Default is false which shows no map.
  • details - The container that should be populated with data. Defaults to false which ignores the setting.
  • 'detailsScope' - Allows you to scope the 'details' container and have multiple geocomplete fields on one page. Must be a parent of the input. Default is 'null'
  • location - Location to initialize the map on. Might be an address string or an array with [latitude, longitude] or a google.maps.LatLngobject. Default is false which shows a blank map.
  • bounds - Whether to snap geocode search to map bounds. Default: true if false search globally. Alternatively pass a custom LatLngBounds object
  • detailsAttribute - The attribute's name to use as an indicator. Default: "name"
  • mapOptions - Options to pass to the google.maps.Map constructor. See the full list here.
  • mapOptions.zoom - The inital zoom level. Default: 14
  • mapOptions.scrollwheel - Whether to enable the scrollwheel to zoom the map. Default: false
  • mapOptions.mapTypeId - The map type. Default: "roadmap"
  • markerOptions - The options to pass to the google.maps.Marker constructor. See the full list here.
  • markerOptions.draggable - If the marker is draggable. Default: false. Set to true to enable dragging.
  • markerOptions.disabled - Do not show marker. Default: false. Set to true to disable marker.
  • maxZoom - The maximum zoom level to zoom in after a geocoding response. Default: 16
  • componentRestrictions - Option for Google Places Autocomplete to restrict results by country. See the docs
  • types - An array containing one or more of the supported types for the places request. Default: ['geocode'] See the full list here.
  • blur - Defaults to false. When enabled it will trigger the geocoding request whenever the geofield is blured. (See jQuery .blur())

Events

You can subscribe to events of the geocode plugin by using the default jQuery syntax:

$("input")
  .geocomplete()
  .bind("geocode:result", function(event, result){
    console.log(result);
  });

The following events are supported:

  • "geocode:result" - Geocode was successful. Passes the original result as described here.
  • "geocode:error" - Fired when the geocode returns an error. Passes the current status as listed here.
  • "geocode:multiple" - Fired immediately after the "result" event if multiple results were found. Passes an array of all results.
  • "geocode:dragged" - Fired when the marker's position was modified manually. Passes the updated location.
  • "geocode:click" - Fired when 'click' event occurs on the map. Passes the location where the click had place.
  • "geocode:mapdragged" - Fired when the map bounds are modified by dragging manually. Passes the location of the current map center.
  • "geocode:idle" - Fired when the map becomes idle after panning or zooming. Passes the location of the current map center.

Methods and Properties

You can access all properties and methods of the plugin from outside. Simply add a string as the first argument to the .geocomplete method after you initialized the plugin.

Example:

// Initialize the plugin.
$("input").geocomplete({ map: ".map_canvas" });

// Call the find method with the parameter "NYC".
$("input").geocomplete("find", "NYC");

// Get the map and set a new zoom level.
var map = $("input").geocomplete("map");
map.setZoom(3);

Address and Places Specific Component Types

The following types are supported by the geocoder and will be passed to the provided form or container:

street_address, route, intersection, political, country, administrative_area_level_1, administrative_area_level_2, administrative_area_level_3, colloquial_area, locality, sublocality, neighborhood, premise, subpremise, postal_code, natural_feature, airport, park, point_of_interest, post_box, street_number, floor, room, lat, lng, viewport, location, formatted_address, location_type, bounds

For more information about address components visit http://code.google.com/apis/maps/documentation/geocoding/#Types

Additionally the following details are passed when the Places API was requested:

id, url, website, vicinity, reference, rating, international_phone_number, icon, formatted_phone_number

More information can be found here: https://developers.google.com/maps/documentation/javascript/places#place_details_responses

About

Developed by Martin Kleppe at Ubilabs.

More Repositories

1

react-geosuggest

A React autosuggest for the Google Maps Places API.
TypeScript
1,021
star
2

kd-tree-javascript

JavaScript k-d Tree Implementation
JavaScript
607
star
3

axidraw

Use JavaScript to draw on any flat surface with the friendly AxiDraw robot
JavaScript
161
star
4

google-maps-api-threejs-layer

Google Maps API layer that uses Three.js to for super fast animation
JavaScript
159
star
5

google-maps-react-hooks

The JavaScript library to easily implement a Google Maps map into your react application. It comes with a collection of React hooks to access the map instance or different Maps JavaScript Services.
TypeScript
73
star
6

threejs-overlay-view

A wrapper for the Google Maps WebglOverlayView that takes care of the integration between three.js and the Google Maps JavaScript API. It lets you create a Google Map overlays directly with three.js.
TypeScript
55
star
7

google.maps.polyline.edit

Enables Google Maps API V3 Polyline Editing
JavaScript
33
star
8

mobile-range-slider

A lightweight JavaScript range slider that works on mobile devices such as iOS or Android.
JavaScript
20
star
9

google-maps-api-svg-overlay

SVG Overlay for the Google Maps JavaScript API v3
JavaScript
16
star
10

google-maps-deckgl-overlay

An example for using a Google Map with Deck.gl
JavaScript
13
star
11

geolocation-remote

Geolocation Remote - Control the Location of Your Mobile Device
JavaScript
11
star
12

node-geobatch

Batch geocode addresses from multiple sources.
JavaScript
10
star
13

esa-climate-from-space

Climate from Space application for ESA's CCI+ program.
TypeScript
9
star
14

project-template

The project creation tooling used at Ubilabs
JavaScript
9
star
15

node-stagger

Execute a stack with a given "request-per-seconds" and "max" rate.
JavaScript
9
star
16

refinery-custom-page-parts

Custom page parts for refinery
Ruby
8
star
17

soccer-debs-challenge

The ACM DEBS 2013 Grand Challenge
JavaScript
8
star
18

webpack-node-modules-list

Exports all used node modules of a webpack bundle to a file.
JavaScript
8
star
19

node-batch-geocoder

Node.js Batch Geocoder for the Google Maps API
JavaScript
7
star
20

node-image-saver

Saves an Image Data URI Back to the File System
JavaScript
7
star
21

node-parallel-transform-stream

A NodeJS transform stream which runs transformations in parallel and preserves input order.
JavaScript
6
star
22

sunzi-recipes

Our server provisioning recipes for sunzi.
Shell
6
star
23

flickr_geocoding_bookmarklet

A Bookmarklet for Better Geocoding within Flickr using Google Maps
JavaScript
6
star
24

grunt-gcloud

A wrapper for the google-gcloud node module.
JavaScript
5
star
25

icons_generator

Multiple Icons Generator using Ruby and ImageMagick
Ruby
5
star
26

touchstates

jQuery Touch State Plugin
JavaScript
4
star
27

kirby-object-storage-stream-wrappers

Prototype to run a Kirby CMS instance with data on Object Storage (GCS)
4
star
28

google-map-bounds-limit

Limits zoom and panning of a google map
JavaScript
4
star
29

cookbooks

Our Fancy Chef Cookbooks
Ruby
3
star
30

fastbillr

Ruby Api wrapper for the fastbill.com API
Ruby
3
star
31

stackenblochen

A grid system for rectanglers.
CSS
2
star
32

jquery-touchevents

jQuery Plugin to Proxy Touch Events
JavaScript
2
star
33

esa-webgl-globe

TypeScript
2
star
34

google-maps-visualrefresh

Comparing the new and old Google Maps styles.
1
star
35

node-google-maps-api-stream

A streaming, rate-limited, and caching interface to Google Maps APIs.
JavaScript
1
star
36

image-performance

Test Image Rendering Performance on Various Browsers
1
star
37

nagios-plugins

A collection of custom Nagios plugins that we use @ubilabs.
Shell
1
star
38

fusion_wiki

1
star
39

storycamp

Basecamp Story Card Printer for Google Chrome
JavaScript
1
star
40

retrobox

Ruby
1
star
41

drone_elixir_example

Example to show some examples on drone.io. This one is for elixir
Elixir
1
star
42

ubilabs.github.com

1
star
43

basic_server_stack

Ruby
1
star
44

gdd

1
star
45

node-api-stream

Create streaming, rate-limited APIs with ease.
JavaScript
1
star
46

binary-view.js

Binary schemes for JavaScript. Let's you define (write and load) binary formats
JavaScript
1
star
47

babylonian

A Mixed Languages Map Type
1
star
48

template

HTML, CSS and JavaScript Templates
JavaScript
1
star