Skip to content

Instantly share code, notes, and snippets.

@dknell
Created June 25, 2011 16:45
Show Gist options
  • Save dknell/1046648 to your computer and use it in GitHub Desktop.
Save dknell/1046648 to your computer and use it in GitHub Desktop.
Titanium mapView radius slider
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');
Ti.include('util.js');
var win = Titanium.UI.createWindow({
backgroundColor:'#fff'
});
var currentPosition = {},
radiusStartVal = 5,
radiusEndVal = 50,
radiusMultiplier = 5,
radiusStartPos = radiusStartVal / radiusMultiplier,
radiusEndPos = radiusEndVal / radiusMultiplier,
radius = radiusStartVal,
mapView = Titanium.Map.createView({
top:0,
bottom:0,
left:0,
right:0,
mapType: Titanium.Map.STANDARD_TYPE,
animate:true,
regionFit:true,
userLocation:false
}),
radiusSlider = Ti.UI.createSlider({
min:radiusStartPos,
max:radiusEndPos,
top:10,
width:150,
value:radiusStartPos
}),
headerTitle = Ti.UI.createLabel({
text:radius+' miles',
top:32,
textAlign:'center',
color:'#333',
font: {
fontFamily:'Arial',
fontSize:16
},
height:'auto'
});
win.add(mapView);
win.add(radiusSlider);
win.add(headerTitle);
win.addEventListener('focus', function(e) {
getGeoLocation({
success: function(geo) {
// save current location
currentPosition = geo;
// set the region
var deltas = getDelta(currentPosition.latitude),
region = {
latitude:currentPosition.latitude,
longitude:currentPosition.longitude,
latitudeDelta:deltas.latitudeDelta*radius,
longitudeDelta:deltas.longitudeDelta*radius
};
setTimeout(function() {
mapView.setLocation(region);
}, 400);
},
error: function(err) {
alert(err);
}
});
});
radiusSlider.addEventListener('change', function(e) {
var currRadius = radius,
val = Math.floor(e.value),
tmpRadius = val * radiusMultiplier;
if (tmpRadius !== currRadius) {
radius = tmpRadius;
// set the title
headerTitle.text = radius+' miles';
var deltas = getDelta(currentPosition.latitude),
set_region = {
latitude:currentPosition.latitude,
longitude:currentPosition.longitude,
latitudeDelta:deltas.latitudeDelta*radius,
longitudeDelta:deltas.longitudeDelta*radius
};
mapView.setLocation(set_region);
}
});
win.open();
function isIPhone3_2_Plus() {
// add iphone specific tests
if (Titanium.Platform.name == 'iPhone OS') {
var version = Titanium.Platform.version.split(".");
var major = parseInt(version[0], 10);
var minor = parseInt(version[1], 10);
// can only test this support on a 3.2+ device
if (major > 3 || (major == 3 && minor > 1)) {
return true;
}
}
return false;
};
function getGeoLocation(_args) {
var geoResult = {};
if (Titanium.Geolocation.locationServicesEnabled==false) {
Titanium.UI.createAlertDialog({title:'Geolocation Disabled', message:'Your device has geo turned off - turn it on please.'}).show();
geoResult.error = "Location services are disabled";
} else {
if (isIPhone3_2_Plus()) {
Ti.Geolocation.purpose = "Testing Geolocation stuff";
};
var geoAllowed = true;
if (Titanium.Platform.name != 'android') {
var authorization = Titanium.Geolocation.locationServicesAuthorization;
if (authorization == Titanium.Geolocation.AUTHORIZATION_DENIED) {
Ti.UI.createAlertDialog({
title:'Geo Denied',
message:'Authorization Denied.'
}).show();
geoResult.error = "Geolocation access is denied";
} else if (authorization == Titanium.Geolocation.AUTHORIZATION_RESTRICTED) {
Ti.UI.createAlertDialog({
title:'Geo Restricted',
message:'Geolocation Restricted.'
}).show();
geoResult.error = "Geolocation access is restricted";
}
}
if (!geoResult.error) {
Titanium.Geolocation.getCurrentPosition(function(e) {
if (!e.success || e.error) {
alert('error ' + JSON.stringify(e.error));
geoResult.error = e.error;
}
if (!geoResult.error) {
var longitude = e.coords.longitude,
latitude = e.coords.latitude,
altitude = e.coords.altitude,
heading = e.coords.heading,
accuracy = e.coords.accuracy,
speed = e.coords.speed,
timestamp = e.coords.timestamp,
altitudeAccuracy = e.coords.altitudeAccuracy;
geoResult.longitude = longitude;
geoResult.latitude = latitude;
if (_args.success) {
_args.success(geoResult);
};
}
});
}
}
if (geoResult.error) {
if (_args.error) {
_args.error(geoResult.error);
};
};
};
/////////////////////////////////////////////////////////////////////////
// code borrowed from http://www.csgnetwork.com/degreelenllavcalc.html //
/////////////////////////////////////////////////////////////////////////
function deg2rad(deg) {
var conv_factor = (2.0 * Math.PI)/360.0;
return(deg * conv_factor);
}
function rad2deg(rad) {
var conv_factor = 360/(2.0 * Math.PI);
return(rad * conv_factor);
}
function zerosPad(rndVal, decPlaces) {
var valStrg = rndVal.toString(); // Convert the number to a string
var decLoc = valStrg.indexOf("."); // Locate the decimal point
// check for a decimal
if (decLoc == -1) {
decPartLen = 0; // If no decimal, then all decimal places will be padded with 0s
// If decPlaces is greater than zero, add a decimal point
valStrg += decPlaces > 0 ? "." : "";
} else {
decPartLen = valStrg.length - decLoc - 1; // If there is a decimal already, only the needed decimal places will be padded with 0s
}
var totalPad = decPlaces - decPartLen; // Calculate the number of decimal places that need to be padded with 0s
if (totalPad > 0) {
// Pad the string with 0s
for (var cntrVal = 1; cntrVal <= totalPad; cntrVal++) {
valStrg += "0";
}
}
return valStrg;
}
function perRound(num, precision) {
var precision = 2; //default value if not passed from caller, change if desired
// remark if passed from caller
precision = parseInt(precision); // make certain the decimal precision is an integer
var result1 = num * Math.pow(10, precision);
var result2 = Math.round(result1);
var result3 = result2 / Math.pow(10, precision);
return zerosPad(result3, precision);
}
function getDelta (lat) {
// Convert latitude to radians
lat = deg2rad(lat);
// Set up "Constants"
m1 = 111132.92; // latitude calculation term 1
m2 = -559.82; // latitude calculation term 2
m3 = 1.175; // latitude calculation term 3
m4 = -0.0023; // latitude calculation term 4
p1 = 111412.84; // longitude calculation term 1
p2 = -93.5; // longitude calculation term 2
p3 = 0.118; // longitude calculation term 3
// Calculate the length of a degree of latitude and longitude in meters
latlen = m1 + (m2 * Math.cos(2 * lat)) + (m3 * Math.cos(4 * lat)) +
(m4 * Math.cos(6 * lat));
longlen = (p1 * Math.cos(lat)) + (p2 * Math.cos(3 * lat)) +
(p3 * Math.cos(5 * lat));
// Place values in output fields
var latmeters = (latlen);
var latfeet = (latlen * (3.280833333));
var latsm = latfeet / 5280;
var latnm = latsm / 1.15077945;
var longmeters = (longlen);
var longfeet = (longlen * (3.280833333) );
var longsm = longfeet / 5280;
var longnm = longsm / 1.15077945;
// rounded
var latmetersr = perRound(latlen);
var latfeetr = perRound(latlen * (3.280833333));
var latsmr = perRound(latfeet / 5280);
var latnmr = perRound(latsm / 1.15077945);
var longmetersr = perRound(longlen);
var longfeetr = perRound(longlen * (3.280833333));
var longsmr = perRound(longfeet / 5280);
var longnmr = perRound(longsm / 1.15077945);
var ret = {
latitudeDelta:1 / latsm,
longitudeDelta:1 / longsm
};
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment