Skip to content

Instantly share code, notes, and snippets.

@almsx
Created August 11, 2016 13:25
Show Gist options
  • Save almsx/c92b8e7c792aaae4d2132ddcabbca094 to your computer and use it in GitHub Desktop.
Save almsx/c92b8e7c792aaae4d2132ddcabbca094 to your computer and use it in GitHub Desktop.
var args = arguments[0] || {};
var latitude;
var longitude;
var mapview;
var annotationUbicacion;
var MapModule = require('ti.map');
var api_key = "AIzaSyDI77iaGnQgo0_M8NZoI5vQW685E0-KGvE";
//var MapModule = require("ti.googlemaps");
//MapModule.setAPIKey(api_key);
var locations = {};
var currentRoute = null;
var ubicationPin = null;
var widthApp = Titanium.Platform.displayCaps.platformWidth;
var heightApp = Titanium.Platform.displayCaps.platformHeight;
var anchoPantalla;
var divisorServicios;
var fuenteServicios;
var anchoBuscador;
var anchoBoton;
var actInd = Ti.UI.createActivityIndicator({
width : Ti.UI.SIZE,
height : Ti.UI.SIZE,
color : '#FFF',
message : 'Cargando, espere por favor',
font : Alloy.Globals.tipografia18Bold
});
$.cargandoNegro.add(actInd);
actInd.show();
function AdaptarPantalla() {
/* iPhone 4/5/5c/5s/6se */
if (widthApp == 320) {
anchoBuscador = 225;
anchoBoton = 50;
$.cajaDireccion.width = anchoBuscador;
$.botonBuscame.width = anchoBoton;
}
/* iPhone 6/6s */
if (widthApp == 375) {
anchoBuscador = 250;
anchoBoton = 80;
$.cajaBusqueda.width = anchoBuscador;
$.botonBuscame.width = anchoBoton;
}
/* iPhone 6/6s Plus */
if (widthApp == 414) {
anchoBuscador = 275;
anchoBoton = 100;
$.cajaDireccion.width = anchoBuscador;
$.botonBuscame.width = anchoBoton;
}
};
AdaptarPantalla();
function ColocarMapaApp() {
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.distanceFilter = 10;
if (Ti.Geolocation.locationServicesEnabled === false) {
var a = Titanium.UI.createAlertDialog({
title : 'Apparel',
message : 'El Dispositivo tiene apagado GPS, por favor enciendelo',
ok : 'Ok'
});
a.show();
} else {
var authorization = Ti.Geolocation.getLocationServicesAuthorization();
if (authorization == Ti.Geolocation.AUTHORIZATION_DENIED) {
var a = Titanium.UI.createAlertDialog({
title : 'Apparel',
message : "No tienes activado los servicios de Geolocalización para Apparel, por favor activalos",
ok : 'Ok'
});
a.show();
} else if (authorization == Ti.Geolocation.AUTHORIZATION_RESTRICTED) {
var a = Titanium.UI.createAlertDialog({
title : 'Apparel',
message : "Han sido restringidos permisos de geolocalización para Apparel",
ok : 'Ok'
});
a.show();
} else {
Ti.API.info('si podemos geolocalizar');
Ti.Geolocation.getCurrentPosition(function(e) {
try {
if (e.coords == undefined) {
latitude = 19.432688;
longitude = -99.133141;
Ti.App.Properties.setString('latitudOrigen', latitude);
Ti.App.Properties.setString('longitudOrigen', longitude);
} else {
latitude = e.coords.latitude;
longitude = e.coords.longitude;
Ti.App.Properties.setString('latitudOrigen', latitude);
Ti.App.Properties.setString('longitudOrigen', longitude);
}
/*Agregamos al Icono para moverse*/
var customAnnotationView = Ti.UI.createView({
backgroundImage : 'img/mastercard.png'
});
annotationUbicacion = MapModule.createAnnotation({
latitude : latitude,
longitude : longitude,
animate : true,
image : 'img/pinGeo.png',
//draggable : true,
title : "Selecciona tu Ubicación",
subtitle : "Apparel",
rightButton : 'img/corteFlecha.png',
centerOffset : {
x : 0,
y : 0
}
//customView : customAnnotationView
});
mapview = MapModule.createView({
mapType : MapModule.NORMAL_TYPE,
animate : true,
regionFit : true,
userLocation : true,
region : {
latitude : latitude,
longitude : longitude,
latitudeDelta : 0.03,
longitudeDelta : 0.03,
},
annotations : [annotationUbicacion]
});
Ti.App.Properties.setString('latitudeSeleccionada', latitude);
Ti.App.Properties.setString('longitudeSeleccionada', longitude);
//mapview.addAnnotation(annotationUbicacion);
/*Escuchamos el movimiento del mapa*/
var pokemon;
/*
mapview.addEventListener("pinchangedragstate", function(e) {
//Ti.API.info("New latitude: " + e.annotation.latitude);
//Ti.API.info("New longitude: " + e.annotation.longitude);
Ti.App.Properties.setString('latitudeSeleccionada', e.annotation.latitude);
Ti.App.Properties.setString('longitudeSeleccionada', e.annotation.longitude);
});
*/
mapview.addEventListener('click', function(evt) {
if (evt.clicksource == "subtitle" || evt.clicksource == "title" || evt.clicksource == "leftPane" || evt.clicksource == "leftView" || evt.clicksource == "leftButton" || evt.clicksource == "rightButton" || evt.clicksource == "rightPane" || evt.clicksource == "rightView" || evt.clicksource == "subtitle") {
var envio = {
'latitudeApp' : latitude,
'longitudApp' : longitude
};
//Ti.API.info(' El seleccionado es ' + fuente);
var menuApp = Alloy.createController('menuApp', {
whoService : envio
}).getView();
menuApp.open();
$.mapaApp.close();
}
});
/*Abrimos el Mapa*/
$.contenedorMapa.add(mapview);
} catch(err) {
var a = Titanium.UI.createAlertDialog({
title : 'Apparel',
message : 'Apparel encontro un error respecto a Geolocalización ' + err,
ok : 'Ok'
});
a.show();
}
});
}
}
};
ColocarMapaApp();
$.mapaApp.addEventListener('singletap', function(e) {
/*var envio = {
'latitudeApp' : latitude,
'longitudApp' : longitude
};
Ti.API.info(' El seleccionado es ' + fuente);
var menuApp = Alloy.createController('menuApp', {
whoService : envio
}).getView();
menuApp.open();
$.mapaApp.close();
*/
var fuente = e.source.id;
Ti.API.info(' El seleccionado es ' + fuente);
if (fuente == 'buscarIMG') {
$.table.visible = false;
if (buscadorCaja == false) {
buscadorCaja = true;
var bajarBus = Titanium.UI.createAnimation({
duration : 300,
top : 70
});
$.buscador.animate(bajarBus);
} else {
buscadorCaja = false;
var subirBus = Titanium.UI.createAnimation({
duration : 300,
top : -70
});
$.buscador.animate(subirBus);
}
};
});
var cambio;
var buscadorCaja = false;
function buscarDireccion() {
var address = $.cajaDireccion.value;
Data = [];
//Aqui evaluamos si hay strings en la address
if ($.cajaDireccion.value == '') {
$.cargandoTransparente.visible = false;
$.table.visible = false;
} else {
var urlBusqueda = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address.replace('#', ' ') + '&sensor=true&key=' + api_key + "&region=mx";
var xhr = Titanium.Network.createHTTPClient({
onload : function(e) {
obj = this.responseText;
response = JSON.parse(obj);
if (response.status === "OK" && response.results.length > 0) {
$.table.visible = true;
var response2 = response.results[0].address_components.length;
for (var i = 0; i < response2 - 2; i++) {
var row = Titanium.UI.createTableViewRow({
height : 50,
id : i,
nombre : response.results[0].address_components[i].long_name,
width : Titanium.UI.FILL
});
var nomPlace = Ti.UI.createLabel({
left : 5,
width : 200,
height : 'auto',
color : '#000',
text : response.results[0].address_components[i].long_name + ', ' + response.results[0].address_components[i].short_name,
});
var flecha = Titanium.UI.createView({
width : 30,
height : 30,
backgroundImage : 'img/corteFlecha.png',
right : 10
});
Data.push(row);
row.add(nomPlace);
row.add(flecha);
}
if ($.cajaDireccion.value == '') {
$.table.visible = false;
$.cargandoTransparente.visible = false;
};
Data.reverse();
$.table.setData(Data);
var place = response.results[0];
var latLng = place.geometry.location;
locations['search'] = {
latitude : latLng.lat,
longitude : latLng.lng
};
}
},
onerror : function(e) {
obj = this.responseText;
var nuevojson = JSON.parse(obj);
$.cargandoTransparente.visible = false;
var a = Titanium.UI.createAlertDialog({
title : 'Apparel',
message : 'Error en buscador',
buttonNames : ['OK']
});
a.show();
},
timeout : 10000000
});
xhr.open("GET", urlBusqueda);
xhr.send();
}
};
$.cajaDireccion.addEventListener('change', buscarDireccion);
function searchPin(lat, lng, title) {
console.log('searchpin funcution');
if (ubicationPin) {
mapview.removeAnnotation(ubicationPin);
}
var dinora;
ubicationPin = MapModule.createAnnotation({
latitude : lat,
longitude : lng,
title : title || 'Lugar',
idTienda : 1,
image : 'img/pinGeo.png',
//rightButton : 'img/adelante.png'
rightButton : 'img/corteFlecha.png'
});
mapview.addAnnotation(ubicationPin);
};
function centerMap(lat, lng) {
console.log('centrar el mapa');
var region = {
latitude : lat,
longitude : lng,
latitudeDelta : 0.03,
longitudeDelta : 0.03
};
mapview.region = region;
};
function httpLocation() {
console.log('hoooola');
$.table.visible = false;
//$.table.zIndex = 1;
var address = $.cajaDireccion.value;
$.cajaDireccion.blur();
if (!address) {
var a = Titanium.UI.createAlertDialog({
title : 'Apparel',
message : 'Escriba una dirección',
ok : 'Ok'
});
a.show();
} else {
var url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address.replace('#', ' ') + '&sensor=true&key=' + api_key + "&region=mx";
var xhr = Titanium.Network.createHTTPClient({
onload : function(e) {
var response = JSON.parse(this.responseText);
if (response.status == "OK" && response.results.length > 0) {
var place = response.results[0];
var latLng = place.geometry.location;
locations['search'] = {
latitude : latLng.lat,
longitude : latLng.lng
};
mapview.removeAllAnnotations();
searchPin(latLng.lat, latLng.lng, place.formatted_address);
Ti.App.Properties.setString('latBusqueda', latLng.lat);
Ti.App.Properties.setString('lonBusqueda', latLng.lng);
centerMap(latLng.lat, latLng.lng);
$.cargandoTransparente.visible = false;
}
},
onerror : function(e) {
obj = this.responseText;
var nuevojson = JSON.parse(obj);
$.cargandoTransparente.visible = false;
var a = Titanium.UI.createAlertDialog({
title : 'Apparel',
message : 'otro error en busqueda mapa',
buttonNames : ['OK']
});
a.show();
},
timeout : 10000000
});
xhr.open("GET", url);
xhr.send();
}
};
$.table.addEventListener('click', function(e) {
$.cajaDireccion.value = e.rowData.nombre;
$.table.visible = false;
httpLocation();
var uno = Ti.App.Properties.getString('latBusqueda');
var dos = Ti.App.Properties.getString('lonBusqueda');
$.cargandoTransparente.visible = true;
$.cargandoNegro.add(actInd);
actInd.show();
});
var cambio;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment