Created
December 9, 2014 11:04
-
-
Save ahmadmilzam/848974427af109894b77 to your computer and use it in GitHub Desktop.
Usefull wrapper module for Google Map API V3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Usefull wrapper module for Google Map API V3 | |
* @param {object} win | |
* @param {object} google | |
* @return {object} | |
* | |
* Author: Ahmad Milzam (http://ahmadmilzam.com) | |
*/ | |
var Mapper = (function(win, google){ | |
var moduleObj = {}, | |
deviceWidth = (win.innerWidth > 0) ? win.innerWidth : screen.width, | |
isDraggable = deviceWidth >= 1024 ? true : false, | |
map, | |
marker, | |
directionsService = new google.maps.DirectionsService(), | |
directionsDisplay = new google.maps.DirectionsRenderer(); | |
var standardOptions = { | |
center:{ | |
lat: -6.226787, | |
lng: 106.80393 | |
}, | |
zoom: 16, | |
scrollwheel: false, | |
disableDefaultUI: true, | |
draggable: isDraggable | |
}, | |
userOptions; | |
var checkGeolocationSupport = function(){ | |
if (navigator.geolocation){ | |
return true; | |
} | |
else{ | |
return false; | |
} | |
} | |
var calcRoute = function(position){ | |
var start = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); | |
var end = new google.maps.LatLng(userOptions.center.lat, userOptions.center.lng); | |
var request = { | |
origin:start, | |
destination:end, | |
travelMode: google.maps.TravelMode.DRIVING | |
}; | |
directionsService.route(request, function(result, status) { | |
if (status == google.maps.DirectionsStatus.OK) { | |
directionsDisplay.setDirections(result); | |
} | |
else{ | |
console.log("Can't calculate the route."); | |
} | |
}); | |
} | |
// ini funtion yang ngejalanin calculate route nya, attach event listener ke funtion ini | |
var getUserLocation = function(){ | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(calcRoute); | |
} | |
else { | |
console.log("Geolocation is not supported by this browser."); | |
} | |
} | |
var extend = function(out) { | |
out = out || {}; | |
for (var i = 1; i < arguments.length; i++) { | |
if (!arguments[i]) | |
continue; | |
for (var key in arguments[i]) { | |
if (arguments[i].hasOwnProperty(key)) | |
out[key] = arguments[i][key]; | |
} | |
} | |
return out; | |
} | |
moduleObj.checkGeolocationSupport = function(){ | |
return checkGeolocationSupport(); | |
} | |
moduleObj.getUserLocation = function(){ | |
getUserLocation(); | |
} | |
moduleObj.getUserOptions = function(){ | |
return userOptions; | |
} | |
moduleObj.create = function(el, opts){ | |
if (typeof google !== 'object' && typeof google.maps !== 'object') | |
{ | |
console.log('Google map API is not loaded'); | |
return; | |
} | |
if (typeof opts === 'undefined' || typeof opts !== 'object'){ | |
userOptions = standardOptions; | |
} | |
else{ | |
userOptions = extend({}, standardOptions, opts); | |
} | |
map = new google.maps.Map(el, userOptions); | |
directionsDisplay.setMap(map); | |
// google.maps.event.addListener(directionsDisplay, 'directions_changed', function(){ | |
// Layout.scrollTop(); | |
// }); | |
return map; | |
// google.maps.event.addDomListener(window, 'load', create(selector, myLat, myLong)); | |
} | |
return moduleObj; | |
}(window, google)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment