Giter Site home page Giter Site logo

jquery-gmap's Introduction

jQuery.gmap

David Dev

Stop wasting your time typing Google Maps API code. Be more efficient.

To display a neat Google Map you have to write the same basic code every time. This plugin helps to create Google Maps with less code.

Dependencies

As this is a jQuery plugin it depends on the jQuery library and of course the Google Maps API v3. Thats it.

Usage

Include the Google Maps API v3 and jquery.gmap.min.js somewhere on your site, preferably before the closing body tag.

<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places" type="text/javascript"></script>
<script src="path/to/jquery.gmap.min.js"></script>

Create an element:

<div id="map"></div>

Initialize your map:

var $map = $('#map').gmap({
  lat: 52.5075419,
  lng: 13.4251364,
  options: {
    disableDefaultUI: true,
    zoom: 13
  }
});

$map.gmap('addMarker', {
  lat: 52.5075419,
  lng: 13.4251364,
  title: 'I am the title'
  infoWindow: {
    content: 'This is an info popup',
    opened: true
  }
});

The same map code without jquery.gmap would look like this:

var options = {
  disableDefaultUI: true,
  zoom: 13,
  center: new google.maps.LatLng(52.5075419, 13.4251364)
}
var map = new google.maps.Map(document.getElementById('map'), options);

var marker = new google.maps.Marker({
  position: new google.maps.LatLng(52.5075419, 13.4251364),
  title: 'I am the title',
  map: map
});

var infowindow = new google.maps.InfoWindow({
  content: 'This is an info popup'
});

google.maps.event.addListener(marker, 'click', function() {
  infowindow.open(map, marker);
});

infowindow.open(map, marker);

You can also specify options inline which will overwrite the default value and the value specified on initialization. You still have to initialize the map.

<div id="map" data-gmap-options='{"zoom":8}' data-gmap-lat="52.5075419" data-gmap-lng="13.4251364"></div>
$('#map').gmap();

Options

centerOnResize

Type: boolean
Default: true

Recenter the map to the current center if map gets resized.

singleInfoWindow

Type: boolean
Default: true

Only open one infoWindow at most. Automatically closes opened infoWindow if you click on a marker to open a different.

API

jQuery.gmap offers several methods which are wrappers for common Google Map API functions.

addMarker

Add a marker to a previously initialized map. You can attach an infoWindow with some content, set the marker icon and add a title. The optional infoWindow can be toggle via click on the marker.

$map.gmap('addMarker', {
  lat: 52.5075419,
  lng: 13.4251364,
  title: 'I\'m a title!',
  icon: '/path/to/icon.ext',
  infoWindow: { // optional
    content: 'Some content',
    opened: true // whether it should be opened on init
  }
});

setOptions

Set options after initialization. Extends current options, does not replace.

$('#map').gmap('setOptions', {
  scrollwheel: false,
  draggable: true
});

getMap

Not all features of Google Maps API v3 are covered by jquery.gmap. Therefore you can get the raw Map object and work with it as normal.

var $gmap = $('#map').gmap({
  ...
});

var map = $gmap.getMap();
// do whatever you like
var id = map.getDiv();

getMarker

Get all initialized marker objects.

jquery-gmap's People

Contributors

lgraubner avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.