Skip to content

Instantly share code, notes, and snippets.

@jupazave
Last active September 7, 2016 15:29
Show Gist options
  • Save jupazave/7112526 to your computer and use it in GitHub Desktop.
Save jupazave/7112526 to your computer and use it in GitHub Desktop.
Mapa con ruta - store locator
var me = new Object();
var directionsService = new google.maps.DirectionsService();
var directionsDisplay;
function calcRoute() {
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(mapaGlobal);
var request = {
origin:new google.maps.LatLng(me.latitud, me.longitud),
destination:new google.maps.LatLng(me.nearStore.latitud, me.nearStore.longitud),
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
function successAjax(data){
var items = [];
var GeoCodeCalc = {};
GeoCodeCalc.EarthRadiusInMiles = 3956.0;
GeoCodeCalc.EarthRadiusInKilometers = 6367.0;
GeoCodeCalc.ToRadian = function(v) { return v * (Math.PI / 180);};
GeoCodeCalc.DiffRadian = function(v1, v2) {
return GeoCodeCalc.ToRadian(v2) - GeoCodeCalc.ToRadian(v1);
};
GeoCodeCalc.CalcDistance = function(lat1, lng1, lat2, lng2, radius) {
return radius * 2 * Math.asin( Math.min(1, Math.sqrt( ( Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.cos(GeoCodeCalc.ToRadian(lat1)) * Math.cos(GeoCodeCalc.ToRadian(lat2)) * Math.pow(Math.sin((GeoCodeCalc.DiffRadian(lng1, lng2)) / 2.0), 2.0) ) ) ) );
};
$.each( data, function( key, val ) {
var elemento = {};
elemento.latitud = val.lat;
elemento.longitud = val.lng;
console.log(window.me);
elemento.distancia = GeoCodeCalc.CalcDistance(window.me.latitud, window.me.longitud, val.latitud , val.longitud , GeoCodeCalc.EarthRadiusInKilometers);
items.push( elemento );
});
$.each(items, function(index, val) {
val.distancia = GeoCodeCalc.CalcDistance(me.latitud, me.longitud, val.latitud , val.longitud ,GeoCodeCalc.EarthRadiusInKilometers);
console.log(val.distancia);
});
items.sort(function(a,b){
return a.distancia-b.distancia;
});
me.nearStore = new Object();
me.nearStore.latitud = items[0].latitud;
me.nearStore.longitud = items[0].longitud;
calcRoute();
};
navigator.geolocation.getCurrentPosition(function(v) {
me.latitud = v.coords.latitude;
me.longitud = v.coords.longitude;
$.getJSON("donde_comprar/popularTodosVet", successAjax);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment