Skip to content

Instantly share code, notes, and snippets.

@unusorin
Created September 19, 2012 12:57
Show Gist options
  • Save unusorin/3749499 to your computer and use it in GitHub Desktop.
Save unusorin/3749499 to your computer and use it in GitHub Desktop.
Google Maps helper functions
var GeoPosition;
/**
* get browser geolocation via html5
* @constructor
*/
function GetGeolocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
GeoPosition = position;
})
}
}
/**
* google maps coordinates set
* @param Latitude
* @param Longitude
* @return {google.maps.LatLng}
* @constructor
*/
function MapCoords(Latitude, Longitude) {
return new google.maps.LatLng(Latitude, Longitude);
}
/**
* build a google map canvas
* @param Options
* @return {google.maps.Map}
* @constructor
*/
function GoogleMapFactory(Options) {
if (Options.MapID == null) {
alert("Map ID must be specified");
return null;
}
if (Options.Latitude == null || Options.Longitude == null) {
alert("Map coordinates must be specified");
return null;
}
if (Options.MapType == null) {
Options.MapType = google.maps.MapTypeId.ROADMAP;
}
if (Options.Zoom == null) {
Options.Zoom = 8;
}
if (Options.UI == null) {
Options.disableUI = false;
} else {
Options.disableUI = true;
}
if (Options.zoomControl == null) {
Options.zoomControl = true;
}
if (Options.panControl == null) {
Options.panControl = true;
}
if (Options.scaleControl == null) {
Options.scaleControl = true;
}
if (Options.typeControl == null) {
Options.typeControl = true;
}
var mapOptions = {
zoom:Options.Zoom,
center:new google.maps.LatLng(Options.Latitude, Options.Longitude),
mapTypeId:Options.MapType,
disableDefaultUI:Options.disableUI,
zoomControl:Options.zoomControl,
panControl:Options.panControl,
scaleControl:Options.scaleControl,
mapTypeControl:Options.typeControl
};
return new google.maps.Map(document.getElementById(Options.MapID),
mapOptions);
}
/**
* build a marker for google maps
* @param Options
* @return {*}
* @constructor
*/
function MarkerFactory(Options) {
if (Options.Map == null) {
alert("Invalid MAP");
return null;
}
if (Options.Latitude == null || Options.Longitude == null) {
alert("Map coordinates must be specified");
return null;
}
if (Options.Title == null) {
Options.Title = "";
}
var marker = new google.maps.Marker({
map:Options.Map,
position:new google.maps.LatLng(Options.Latitude, Options.Longitude),
title:Options.Title,
icon:Options.Image,
draggable:Options.Draggable
});
return marker;
}
/**
* build a window for google maps
* @param Options
* @constructor
*/
function InfoWindowFactory(Options) {
if (Options.Map == null) {
alert("Invalid MAP");
return null;
}
if (Options.Marker == null) {
alert("Invalid MARKER");
return null;
}
if (Options.Content == null) {
Options.Content = "";
}
var infowindow = new google.maps.InfoWindow();
infowindow.setContent(Options.Content);
google.maps.event.addListener(Options.Marker, 'click', function () {
infowindow.open(Options.Map, Options.Marker);
});
}
/**
* build a google maps circle
* @param Options
* @return {*}
* @constructor
*/
function CircleFactory(Options) {
if (Options.Latitude == null || Options.Longitude == null) {
alert("Map coordinates must be specified");
return null;
}
if (Options.Radius == null) {
alert("Circle radius must be specified");
return null;
}
if (Options.Map == null) {
alert("Invalid MAP");
return null;
}
var strokeColor = "#FF00000";
var strokeOpacity = 0.8;
var strokeWeight = 2;
var fillColor = "#FF0000";
var fillOpacity = 0.35;
var center = new google.maps.LatLng(Options.Latitude, Options.Longitude);
if (Options.strokeColor == null) {
Options.strokeColor = strokeColor;
}
if (Options.strokeOpacity == null) {
Options.strokeOpacity = strokeOpacity;
}
if (Options.strokeWeight == null) {
Options.strokeWeight = strokeWeight;
}
if (Options.fillColor == null) {
Options.fillColor = fillColor;
}
if (Options.fillOpacity == null) {
Options.fillOpacity = fillOpacity;
}
if (Options.center == null) {
Options.center = center;
}
cityCircle = new google.maps.Circle(Options);
return cityCircle;
}
/**
* build a google maps polygon
* @param Options
* @return {*}
* @constructor
*/
function PolygonFactory(Options) {
var polygon;
if (Options.Paths == null) {
alert("You must specify polygon points");
return null;
} else {
Options.path = Options.Paths;
}
if (Options.Map == null) {
alert("Invalid MAP");
return null;
}
if (Options.strokeColor == null) {
Options.strokeColor = "#FF0000";
}
if (Options.strokeOpacity == null) {
Options.strokeOpacity = 0.8;
}
if (Options.strokeWeight == null) {
Options.strokeWeight = 2;
}
if (Options.fillColor == null) {
Options.fillColor = "#FF0000";
}
if (Options.fillOpacity == null) {
Options.fillOpacity = 0.35;
}
if (Options.Editable != null) {
Options.editable = Options.Editable;
}
polygon = new google.maps.Polygon(Options);
return polygon;
}
/**
* build a google maps polyline
* @param Options
* @return {*}
* @constructor
*/
function PolylineFactory(Options) {
if (Options.Paths == null) {
alert("You must specify polygon points");
return null;
} else {
Options.path = Options.Paths;
}
if (Options.Map == null) {
alert("Invalid MAP");
return null;
}
if (Options.strokeColor == null) {
Options.strokeColor = "#FF0000";
}
if (Options.strokeOpacity == null) {
Options.strokeOpacity = 0.8;
}
if (Options.strokeWeight == null) {
Options.strokeWeight = 2;
}
if (Options.Editable != null) {
Options.editable = Options.Editable;
}
var polyline;
polyline = new google.maps.Polyline(Options);
return polyline;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment