var Map = require('ti.map');
var mapview = null;
var circle = null;
var currentLatitude = null;
var currentLongitude = null;
var puntoEvento = null;
var currentAddress = null;
//Function fired from a view where map is
function setAnnotationOnLongPress(e){
/*
* {"type":"longclick","source":{"compassEnabled":true,"animate":true,"annotations":[{"showInfoWindow":true,"pincolor":0,"longitude":9.7152357,"latitude":45.7138272,
* "title":"Tua Posizione","subtitle":"Sblind","apiName":"Ti.Proxy","bubbleParent":true,"myid":1}],
* "region":{"latitude":45.7138272,"longitude":9.7152357,"latitudeDelta":0.01,"longitudeDelta":0.01},
* "mapType":1,"userLocation":false,"minZoomLevel":2,"zoom":15.627570152282715,"maxZoomLevel":21,"hiddenBehavior":4,"enabled":true,"visible":true,"touchEnabled":true,
* "backgroundRepeat":false,"keepScreenOn":false,"children":[],"size":{"height":594,"width":360,"y":0,"x":0},"rect":{"absoluteX":0,"height":594,"width":360,"y":0,"absoluteY":0,"x":0},
* "apiName":"Ti.Map","bubbleParent":true,"regionFit":true,"soundEffectsEnabled":true,"horizontalWrap":true,"_events":{"click":{}}},"map":{"compassEnabled":true,"animate":true,
* "annotations":[{"showInfoWindow":true,"pincolor":0,"longitude":9.7152357,"latitude":45.7138272,"title":"Tua Posizione","subtitle":"Sblind","apiName":"Ti.Proxy","bubbleParent":true,"myid":1}],
* "region":{"latitude":45.7138272,"longitude":9.7152357,"latitudeDelta":0.01,"longitudeDelta":0.01},"mapType":1,"userLocation":false,"minZoomLevel":2,"zoom":15.627570152282715,
* "maxZoomLevel":21,"hiddenBehavior":4,"enabled":true,"visible":true,"touchEnabled":true,"backgroundRepeat":false,"keepScreenOn":false,"children":[],"size":{"height":594,"width":360,"y":0,"x":0},
* "rect":{"absoluteX":0,"height":594,"width":360,"y":0,"absoluteY":0,"x":0},"apiName":"Ti.Map","bubbleParent":true,"regionFit":true,"soundEffectsEnabled":true,"horizontalWrap":true,
* "_events":{"click":{}}},"cancelBubble":false,"longitude":9.713328257203102,"latitude":45.71091987188729,"bubbles":true}
*/
currentLatitude = e.latitude;
currentLongitude = e.longitude;
getGoogleGeo();
mapview.removeAnnotation(puntoEvento);
//mapview.removeAllAnnotations();
puntoEvento.latitude = e.latitude;
puntoEvento.longitude = e.longitude;
mapview.addAnnotation(puntoEvento);
mapview.region = {
latitude : e.latitude,
longitude : e.longitude,
latitudeDelta : 0.01,
longitudeDelta : 0.01
};
if(OS_ANDROID)
mapview.removeCircle(circle);
else
mapview.removeAllCircles();
circle = Map.createCircle({
center : {
latitude : e.latitude,
longitude : e.longitude
},
strokeWidth : 3,
strokeColor : 'black',
radius : parseInt(verticalSlider.value, 10), //1km
fillColor : "#20FF0000"
});
mapview.addCircle(circle);
}
function loadMap(circleExtension) {
puntoEvento = Map.createAnnotation({
latitude : currentLatitude,
longitude : currentLongitude,
title : "Your position",
subtitle : 'Sblind',
pincolor : Map.ANNOTATION_RED,
myid : 1 // Custom property to uniquely identify this annotation.
});
mapview = Map.createView({
mapType : Map.NORMAL_TYPE,
region : {
latitude : currentLatitude,
longitude : currentLongitude,
latitudeDelta : 0.01,
longitudeDelta : 0.01
},
animate : true,
regionFit : true,
userLocation : false,
annotations : [puntoEvento]
});
circle = Map.createCircle({
center : {
latitude : currentLatitude,
longitude : currentLongitude
},
strokeWidth : 3,
strokeColor : 'black',
radius : (_.isUndefined(circleExtension)) ? 50 : circleExtension, //1km
fillColor : "#20FF0000"
});
mapview.addCircle(circle);
$.mapContainer.add(mapview);
// Handle click events on any annotations on this map.
mapview.addEventListener('click', function(evt) {
Ti.API.info("Clicked " + evt.clicksource + " on " + evt.latitude + "," + evt.longitude);
});
$.main.open();
}
function loadCoordinate(){
disegnaControlli();
if (Ti.Geolocation.locationServicesEnabled) {
Ti.Geolocation.setAccuracy(Ti.Geolocation.ACCURACY_BEST);//Ti.Geolocation.ACCURACY_BEST;
if(Titanium.Platform.osname != "android")
Ti.Geolocation.setDistanceFilter(10);
Titanium.Geolocation.getCurrentPosition(function(e){//Funzione per posizione corrente
if(e.coords!=null){
Ti.API.info(JSON.stringify(e));
currentLatitude = e.coords.latitude;
currentLongitude = e.coords.longitude;
getGoogleGeo();
loadMap();
}
});
}
else{
_Utility.popUp(L('gps_disabilitato'), L('gps_disabilitato_msg'));
currentLatitude = 45.7138565;
currentLongitude = 9.7151823;
getGoogleGeo();
loadMap();
}
}
function openMap() {
if(_.isNull(Alloy.Globals.mapObj)){
if (!Titanium.Geolocation.hasLocationPermissions(Titanium.Geolocation.AUTHORIZATION_WHEN_IN_USE)) {
Titanium.Geolocation.requestLocationPermissions(Titanium.Geolocation.AUTHORIZATION_WHEN_IN_USE, function(result) {
if (result.success) {
loadCoordinate();
} else {
_Utility.popUp(L("no_permessi_title"),L("no_permessi"));
}
});
} else {
loadCoordinate();
}
}
else{
Ti.API.info(JSON.stringify(Alloy.Globals.mapObj));
currentLatitude = Alloy.Globals.mapObj.latitudine;
currentLongitude = Alloy.Globals.mapObj.longitudine;
getGoogleGeo();
disegnaControlli(Alloy.Globals.mapObj.raggio);
$.label.text = Alloy.Globals.mapObj.raggio;
loadMap(Alloy.Globals.mapObj.raggio);
}
}
var tentativiTimeout = 3;
var tentativiGoogle = 5;
var pausaTraTentativi = 200;//ms
var timeoutRichiesta = 5000;//ms
function getGoogleGeo() {
if (Titanium.Network.online) {
var addrUrl = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&latlng=" + currentLatitude + "," + currentLongitude;
/* web-service call */
Ti.API.info(addrUrl);
var addrReq = Titanium.Network.createHTTPClient();
addrReq.setTimeout(timeoutRichiesta);
addrReq.open("GET", addrUrl);
addrReq.onerror = function() {
if(tentativiTimeout > 0){ // riprovo a fare la richiesta
tentativiTimeout--;
getGoogleGeo();
}
else
currentAddress = currentLatitude + "," + currentLongitude;
};
addrReq.onload = function() {
var response = JSON.parse(this.responseText);
var street,
num,
city,
province,
cap,
country;
if (response.status == "OK") {
var resLen = response.results[0].address_components.length;
for (var i = 0; i < resLen; i++) {
switch (response.results[0].address_components[i].types[0]) {
case "street_number":
num = response.results[0].address_components[i].long_name;
break;
case "route":
street = response.results[0].address_components[i].long_name;
break;
case "locality":
city = response.results[0].address_components[i].long_name;
break;
case "administrative_area_level_2":
province = response.results[0].address_components[i].short_name;
break;
case "postal_code":
cap = response.results[0].address_components[i].long_name;
break;
case "country":
country = response.results[0].address_components[i].long_name;
break;
}
}
currentAddress = street + "," + num + "," + cap + "," + city + "," + province + "," + country;
Ti.API.info(currentAddress);
}
else{
if(tentativiGoogle > 0){ // se non ho esaurito i tentativi aspetto un attimo e riprovo
tentativiGoogle--;
setTimeout(function(e){getGoogleGeo();}, pausaTraTentativi);
}
else
currentAddress = currentLatitude + "," + currentLongitude;
}
};
addrReq.send(null);
}
else{
currentAddress = currentLatitude + "," + currentLongitude;
}
}
openMap();
}