Last active
August 29, 2015 14:05
-
-
Save JoaoGFarias/2c44ee12c1a84a8037cd to your computer and use it in GitHub Desktop.
Mini-Google Maps API
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
//This program is free software: you can redistribute it and/or modify | |
//it under the terms of the GNU General Public License as published by | |
//the Free Software Foundation, either version 3 of the License, or | |
//at your option) any later version. | |
// | |
//This program is distributed in the hope that it will be useful, | |
//but WITHOUT ANY WARRANTY; without even the implied warranty of | |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
//GNU General Public License for more details. | |
// | |
//You should have received a copy of the GNU General Public License | |
//along with this program. If not, see <http://www.gnu.org/licenses/>. | |
$(function() { | |
// Gets the current position via GPS | |
// Arguments: | |
// success -> Function called if the position is successfully recovered | |
// error -> Function called if is not possible to recoverer the position | |
function getCurrentPosition(success,error){ | |
navigator.geolocation.getCurrentPosition(success, error); | |
} | |
//Use it this way: | |
//createMap( container, [-8.6,11],{ zoom : 8,mayType: google.maps.MapTypeId.HYBRID } ); | |
function createMap(container,center,optionalParameters){ | |
//Setting default values | |
var zoom = optionalParameters.zoom || 15; | |
var mapType = optionalParameters.mapType || google.maps.MapTypeId.ROADMAP; | |
var mapOptions = { | |
zoom: zoom, | |
center: new google.maps.LatLng(center[0], center[1]), | |
mapTypeId: mapType | |
} | |
map = new google.maps.Map(document.getElementById(container), mapOptions); | |
return map; | |
} | |
// Creates a marker into a given position | |
//Use it this way: | |
//createMarker([-8.6,11],{ zoom : 8,mayType: google.maps.MapTypeId.HYBRID } ); | |
function createMarker(position,optionalParameters){ | |
//Setting default values for optional parameters | |
var title = optionalParameters.titile || ""; | |
var animation = optionalParameters.animation || google.maps.Animation.DROP; | |
var optimized = optionalParameters.optimized || false; | |
var draggable = optionalParameters.draggable || true; | |
var markerOptions = { | |
map: map, | |
position: new google.maps.LatLng(position[0], position[1]), | |
title: title, | |
draggable: draggable, | |
animation: animation, | |
optimized: optimized | |
}; | |
var marker = new google.maps.Marker(markerOptions); | |
return marker; | |
} | |
// Adds a marker into a given map. | |
// Position -> Marker position | |
function addMarker(map,position,optionalParameters){ | |
marker = createMarker(position,optionalParameters); | |
marker.setMap(map); | |
return marker; | |
} | |
function getDirections(map,orign,destination,error,optionalParameters){ | |
//Setting optional parameters | |
var travelMode = optionalParameters.travelMode || google.maps.TravelMode.WALKING; | |
var unitSystem = optionalParameters.unitSystem || google.maps.UnitSystem.METRIC; | |
var avoidHighways = optionalParameters.avoidHighways|| false; | |
var avoidTolls = optionalParameters.avoidTolls || false; | |
var request = { | |
origin : new google.maps.LatLng(orign[0], orign[1]), | |
destination : new google.maps.LatLng(destination[0], destination[1]), | |
travelMode : travelMode, | |
unitSystem: unitSystem, | |
avoidHighways: avoidHighways, | |
avoidTolls : avoidTolls | |
}; | |
var directionsDisplay = new google.maps.DirectionsRenderer(); | |
new google.maps.DirectionsService().route(request, | |
function(result,status){ | |
if (status === google.maps.DirectionsStatus.OK) { | |
directionsDisplay.setMap(map); | |
directionsDisplay.setDirections(result); | |
} else { | |
error(status); | |
} | |
}) | |
return directionsDisplay; | |
} | |
function geocoder(placename,successFoo,optionalParameters){ | |
errorFoo = optionalParameters.errorFoo || function(){return{latitude: null,longitude:null}}; | |
new google.maps.Geocoder().geocode({address: placename},function(results,status){ | |
if(status == google.maps.GeocoderStatus.OK){ | |
successFoo(results[0].geometry.location.lat(), | |
results[0].geometry.location.lng()); | |
}else{ | |
return errorFoo(); | |
} | |
}); | |
} | |
function reverseGeocode(point,successFoo,optionalParameters){ | |
errorFoo = optionalParameters.errorFoo || function(){return null;}; | |
new google.maps.Geocoder().geocode({latLng: point}, function(results,status){ | |
if(status == google.maps.GeocoderStatus.OK && results.length > 2){ | |
successFoo(results[0].formatted_address); | |
}else{ | |
errorFoo(); | |
} | |
}); | |
} | |
function addCircle(map,marker,radius,optionalParameters){ | |
draggable = optionalParameters.draggable || true; | |
color = optionalParameters.color || '#AA0000'; | |
var circle = new google.maps.Circle({ | |
map: map, | |
radius: radius, | |
fillColor: color | |
}); | |
circle.bindTo('center', marker, 'position'); | |
return circle; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment