Skip to content

Instantly share code, notes, and snippets.

@radum
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radum/6b14bc625534390201f7 to your computer and use it in GitHub Desktop.
Save radum/6b14bc625534390201f7 to your computer and use it in GitHub Desktop.
Google Maps API wrapper

This creates a Google Map and lets you add 5 markers (this can be customized) on the map using click events.

GoogleMap is the main constructor.

Example on how to use: (code also found in the js file)

var googleMap = new GoogleMap({
    onMarkerAdd: function(marker) {
        var position = marker.getPosition();
        $('#locations ul').append('<li>Lat: ' + position.lat + ' Lng: ' + position.lng + '</li>');

        marker.getFormattedAddress(function(address){
            $('#locations ul').append('<li>Address: ' + address + '</li>');
        });
    }
});

What happens above is simple:

We have create a new instance of GoogleMap and we are passing only 1 option, which in fact its a callback function for the onMarkerAdd event.

Basicaly everytime you click the map the script checks if the number of maximum markers (5 default) has been reached if not adds a marker and calls the onMarkerAdd function.

You can use that to get the position or the formated address of the marker and use it as you need.

Here is another example on how you can change the number of maximum markers:

var googleMap = new GoogleMap({
    maxMarkers: 3,
    onMarkerAdd: function(marker) {
        var position = marker.getPosition();
        $('#locations ul').append('<li>Lat: ' + position.lat + ' Lng: ' + position.lng + '</li>');

        marker.getFormattedAddress(function(address){
            $('#locations ul').append('<li>Address: ' + address + '</li>');
        });
    }
});

The complete list of options:

option description
VERBOSE Show log information in the console, use false to disable (boolean)
mapContainer Div ID where the map will be generated. Ex: 'map-canvas' (string)
maxMarkers Maximum number of markers (number)
mapOptions Google maps options, see their API for more (object)
mapOptions.zoom Zoom level Ex: 14 (number)
mapOptions.center Location where to center Ex: new google.maps.LatLng(-36.910504, 174.819958) (object)
onMarkerAdd Callback function for when a marker is added (function)
/**
* @author Radu Micu
* GoogleMap constructor
*/
function GoogleMap(customConfig) {
var config = {
VERBOSE: true,
mapContainer: 'map-canvas',
maxMarkers: 5,
mapOptions: {
zoom: 14,
center: new google.maps.LatLng(-36.910504, 174.819958)
},
onMarkerAdd: null
};
if (customConfig && typeof(customConfig) === 'object') {
$.extend(config, customConfig);
}
this._config = config;
this._markers = {};
this.init();
}
GoogleMap.prototype.init = function() {
this.mapDiv = document.getElementById(this._config.mapContainer);
this.map = new google.maps.Map(this.mapDiv, this._config.mapOptions);
this._initClickEvent();
if (this._config.VERBOSE) {
console.log('GoogleMap initialized');
}
};
GoogleMap.prototype._addMarker = function(event) {
var id = this._generateUUID();
var marker = new GoogleMapMarker({ position: event.latLng, map: this.map});
this._markers[id] = marker;
this._initMarkerClickEvent(id, marker.getGMarkerObj());
if (this._config.VERBOSE) {
console.log('new marker: ' + id);
}
this._config.onMarkerAdd.call(this, marker);
return id;
};
GoogleMap.prototype._removeMarker = function(id) {
this._markers[id].getGMarkerObj().setMap(null);
if (this._config.VERBOSE) {
console.log('removed marker ' + id);
}
return delete this._markers[id];
};
GoogleMap.prototype._initMarkerClickEvent = function(id, marker) {
var _this = this;
google.maps.event.addListener(marker, 'click', function() {
_this._removeMarker(id);
});
};
GoogleMap.prototype._initClickEvent = function() {
var _this = this;
return google.maps.event.addListener(this.map, 'click', function(event) {
if (_this._objectSize(_this._markers) >= _this._config.maxMarkers) {
if (_this._config.VERBOSE) {
console.log('max(' + _this._config.maxMarkers + ') markers added');
}
return null;
} else {
_this._addMarker(event);
}
});
};
GoogleMap.prototype._generateUUID = function(event) {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
};
GoogleMap.prototype._objectSize = function(object) {
/* function to validate the existence of each key in the object to get the number of valid keys. */
var objectSize = 0;
for (key in object) {
if (object.hasOwnProperty(key)) {
objectSize++;
}
}
return objectSize;
};
/**
* @author Radu Micu
* GoogleMapMarker constructor
*/
function GoogleMapMarker(_options) {
this.options = {
animation: google.maps.Animation.DROP
};
$.extend(this.options, _options);
this.geocoder = new google.maps.Geocoder();
this.marker = new google.maps.Marker(this.options);
}
GoogleMapMarker.prototype.getGMarkerObj = function() {
return this.marker;
}
GoogleMapMarker.prototype.getPosition = function() {
return {
lat: this.marker.position.k,
lng: this.marker.position.D
}
};
GoogleMapMarker.prototype.getFormattedAddress = function(cb) {
this.geocoder.geocode({'latLng': this.getPosition()}, function(results, status) {
if (results[0]) {
cb(results[0].formatted_address);
} else {
cb(null);
}
});
};
/**
* Demo on how to use `GoogleMap`
*/
var googleMap = new GoogleMap({
onMarkerAdd: function(marker) {
var position = marker.getPosition();
$('#locations ul').append('<li>Lat: ' + position.lat + ' Lng: ' + position.lng + '</li>');
marker.getFormattedAddress(function(address){
$('#locations ul').append('<li>Address: ' + address + '</li>');
});
}
});
/*! normalize.css v3.0.2 | MIT License | git.io/normalize */html{font-family:sans-serif;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%}body{margin:0}article,aside,details,figcaption,figure,footer,header,hgroup,main,menu,nav,section,summary{display:block}audio,canvas,progress,video{display:inline-block;vertical-align:baseline}audio:not([controls]){display:none;height:0}[hidden],template{display:none}a{background-color:transparent}a:active,a:hover{outline:0}abbr[title]{border-bottom:1px dotted}b,strong{font-weight:700}dfn{font-style:italic}h1{font-size:2em;margin:.67em 0}mark{background:#ff0;color:#000}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}img{border:0}svg:not(:root){overflow:hidden}figure{margin:1em 40px}hr{-moz-box-sizing:content-box;box-sizing:content-box;height:0}pre{overflow:auto}code,kbd,pre,samp{font-family:monospace,monospace;font-size:1em}button,input,optgroup,select,textarea{color:inherit;font:inherit;margin:0}button{overflow:visible}button,select{text-transform:none}button,html input[type=button],input[type=reset],input[type=submit]{-webkit-appearance:button;cursor:pointer}button[disabled],html input[disabled]{cursor:default}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}input{line-height:normal}input[type=checkbox],input[type=radio]{box-sizing:border-box;padding:0}input[type=number]::-webkit-inner-spin-button,input[type=number]::-webkit-outer-spin-button{height:auto}input[type=search]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box}input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-decoration{-webkit-appearance:none}fieldset{border:1px solid silver;margin:0 2px;padding:.35em .625em .75em}legend{border:0;padding:0}textarea{overflow:auto}optgroup{font-weight:700}table{border-collapse:collapse;border-spacing:0}td,th{padding:0}
/* --------------------------------------------------
Container
-------------------------------------------------- */
div#container, div.container { position: relative; width: 909px; margin: 0 auto; padding: 0; }
/* --------------------------------------------------
Grid
-------------------------------------------------- */
.column-row, .row { margin: 0 0 18px -15px; }
.column, .columns { float: left; display: inline; margin: 0 0 0 15px; }
.one { width: 62px; }
.two { width: 139px; }
.three { width: 216px; }
.four { width: 293px; }
.five { width: 370px; }
.six { width: 447px; }
.seven { width: 524px; }
.eight { width: 601px; }
.nine { width: 678px; }
.ten { width: 755px; }
.eleven { width: 832px; }
.twelve { width: 909px; }
.offset-by-one-half { margin-left: 38.5px; }
.offset-by-one { margin-left: 77px; }
.offset-by-two { margin-left: 154px; }
.offset-by-three { margin-left: 231px; }
.offset-by-four { margin-left: 308px; }
.offset-by-five { margin-left: 385px; }
.offset-by-six { margin-left: 462px; }
.offset-by-seven { margin-left: 539px; }
.offset-by-eight { margin-left: 616px; }
.offset-by-nine { margin-left: 693px; }
.offset-by-ten { margin-left: 770px; }
/* Clear the columns automagically, thanks @kneath */
.column-row:after, .row:after, .clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
* html .column-row, * html .row, * html .clearfix { height: 1%; }
.column-row, .row, .clearfix { display: inline-block; }
.column-row, .row, .clearfix { display: block; }
/* IE6 Problems ---------- */
body.ie .column-row { width: 924px; }
body.ie .eight .column-row { width: 616px; }
body.ie .nine .column-row { width: 693px; }
body.ie .ten .column-row { width: 770px; }
body.ie .eleven .column-row { width: 847px; }
body.ie .two-thirds .column-row { width: 631px; }
h1 {
margin-bottom: 15px;
}
#map-canvas {
height: 500px;
margin: 0px;
padding: 0px
}
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>map by radu micu</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="map-demo.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="twelve"><h1>Google Maps demo</h1></div>
</div>
<div class="row">
<div class="twelve">
<div id="map-canvas"></div>
</div>
</div>
<div class="row">
<div class="twelve">
<div id="locations"><ul></ul></div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
<script src="gmaps-wrapper.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment