Skip to content

Instantly share code, notes, and snippets.

@LiamChapman
Created February 19, 2018 10:56
Show Gist options
  • Select an option

  • Save LiamChapman/85c2e54200d3454cca210cdb6c0844e3 to your computer and use it in GitHub Desktop.

Select an option

Save LiamChapman/85c2e54200d3454cca210cdb6c0844e3 to your computer and use it in GitHub Desktop.
Google map code. Currently set to London and colours are purple.
// https://maps.googleapis.com/maps/api/js?key=API_KEY (include)
/**
* Google Maps
**/
var GoogleMaps = (function(google, document) {
try {
// Params for offsetting map
var desktopWidth = 768;
// set offset dimensions on load, need to resolve on resize too
if ( window.innerWidth < desktopWidth ) {
// Mobile
window.offsetLat = 0.0025;
window.offsetLong = -0.002;
} else {
// Desktop
window.offsetLat = 0.002;
window.offsetLong = -0.005;
}
// Default options
var mapOptions = {
// How zoomed in you want the map to start at (always required)
zoom: 16,
// The latitude and longitude to center the map (always required)
center: new google.maps.LatLng(51.517796 + offsetLat, -0.141419 + offsetLong),
// center: new google.maps.LatLng(51.517796, -0.141419),
// How you would like to style the map.
// Remove background color.
backgroundColor: 'none',
// This is where you would paste any style found on Snazzy Maps.
styles: [
{
"featureType": "all",
"elementType": "labels.text.fill",
"stylers": [
{
"saturation": 36
},
{
"color": "#000000"
},
{
"lightness": 40
}
]
},
{
"featureType": "all",
"elementType": "labels.text.stroke",
"stylers": [
{
"visibility": "on"
},
{
"color": "#120a38"
}
]
},
{
"featureType": "all",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "administrative",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#1a123e"
}
]
},
{
"featureType": "administrative",
"elementType": "geometry.stroke",
"stylers": [
{
"color": "#1a123e"
},
{
"weight": 1.2
}
]
},
{
"featureType": "landscape",
"elementType": "geometry",
"stylers": [
{
"color": "#1a123e"
}
]
},
{
"featureType": "poi",
"elementType": "geometry",
"stylers": [
{
"color": "#1a123e"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry.fill",
"stylers": [
{
"color": "#120a38"
}
]
},
{
"featureType": "road.highway",
"elementType": "geometry.stroke",
"stylers": [
{
"color": "#120a38"
}
]
},
{
"featureType": "road.arterial",
"elementType": "geometry",
"stylers": [
{
"color": "#120a38"
}
]
},
{
"featureType": "road.local",
"elementType": "geometry",
"stylers": [
{
"color": "#120a38"
}
]
},
{
"featureType": "transit",
"elementType": "geometry",
"stylers": [
{
"color": "#120a38"
}
]
},
{
"featureType": "water",
"elementType": "geometry",
"stylers": [
{
"color": "#584d89"
}
]
}
],
// diable controls etc..
disableDefaultUI: true,
scrollwheel: false,
navigationControl: false,
mapTypeControl: false,
scaleControl: false,
draggable: true
};
var public = {};
var __ = {};
public.init = function(options, lat, long) {
// Get the HTML DOM element that will contain your map
// We are using a div with id="map" seen below in the <body>
var mapElement = document.getElementById('map');
if (mapElement) {
// Create the Google Map using our element and options defined above
var map = new google.maps.Map(mapElement, options||mapOptions);
// Let's also add a marker while we're at it
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat||51.517796, long||-0.141419),
map: map,
icon: '/images/marker.png',
title: 'NuFu'
});
}
}
public.update = function (lat, long) {
// mapOptions.center = new google.maps.LatLng(lat, long),
// Set center with optional offsets
mapOptions.center = new google.maps.LatLng(parseFloat(lat) + offsetLat, parseFloat(long) + offsetLong);
public.init( mapOptions, lat, long );
};
return public;
} catch (e) {
console.log(e);
}
})(google, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment