Skip to content

Instantly share code, notes, and snippets.

@samueleastdev
Last active December 12, 2016 09:34
Show Gist options
  • Save samueleastdev/68ad679e87e1d3d433c9198f37f5bfdd to your computer and use it in GitHub Desktop.
Save samueleastdev/68ad679e87e1d3d433c9198f37f5bfdd to your computer and use it in GitHub Desktop.
Cross Platform Maps Appcelerator
var permissions = require("mapPermissions");
var RunLocation = false;
var search = {
latitude : 51.576339,
longitude : -3.216717,
heading : null
};
/**
* Runs on device compass location
* @param {object} data
* @return {null} updates the ui
* @author sam
*/
var compassHandler = function(_event) {
if (Titanium.Geolocation.hasCompass) {
if (_event.success === undefined || _event.success) {
search.heading = _event.heading.trueHeading;
}
} else {
console.log("No Compass on device");
}
};
/**
* Runs after the devices successfuly gets the users location
* @param {object} data
* @return {null} updates the ui
* @author sam
*/
var locationCallback = function(_event) {
if (RunLocation) {
if (_event.success && _event.coords) {
search.latitude = _event.coords.latitude;
//51.576339
search.longitude = _event.coords.longitude;
//-3.216717
if (OS_IOS) {
var cam = MapModule.createCamera({
altitude : 600,
centerCoordinate : {
latitude : search.latitude,
longitude : search.longitude
},
heading : search.heading,
pitch : 60
});
$.MapView.animateCamera({
camera : cam,
curve : Ti.UI.ANIMATION_CURVE_EASE_IN
});
}
if (OS_ANDROID) {
$.MapView.setRegion({
latitude : search.latitude,
longitude : search.longitude,
bearing : search.heading,
zoom : 15,
tilt : 45
});
}
// Remove the vent listener
Ti.Geolocation.removeEventListener('location', locationCallback);
$.MapView.userLocation = true;
} else {
console.log("There has been a error getting your location.....");
}
}
};
$.MapView.addEventListener('complete', function(_event) {
RunLocation = true;
});
/**
* Listeners to the window open
* @param {object} data
* @return {null} updates the ui
* @author sam
*/
function showCurrentPosition(_event) {
/**
* Checks the device has correct permissions and authorizes
* @param {object} data
* @return {null} updates the ui
* @author sam
*/
permissions.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE, function(e) {
console.log("requestLocationPermissions", e);
if (e.success) {
if (OS_IOS) {
Ti.Geolocation.showCalibration = false;
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.addEventListener("heading", compassHandler);
Ti.Geolocation.addEventListener('location', locationCallback);
}
if (OS_ANDROID) {
var providerPassive = Ti.Geolocation.Android.createLocationProvider({
name : Ti.Geolocation.PROVIDER_PASSIVE,
minUpdateDistance : 0.0,
minUpdateTime : 0
});
var providerNetwork = Ti.Geolocation.Android.createLocationProvider({
name : Ti.Geolocation.PROVIDER_NETWORK,
minUpdateDistance : 0.0,
minUpdateTime : 5
});
var providerGps = Ti.Geolocation.Android.createLocationProvider({
name : Ti.Geolocation.PROVIDER_GPS,
minUpdateDistance : 0.0,
minUpdateTime : 0
});
Ti.Geolocation.showCalibration = false;
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.Android.addLocationProvider(providerPassive);
Ti.Geolocation.Android.addLocationProvider(providerNetwork);
Ti.Geolocation.Android.addLocationProvider(providerGps);
Ti.Geolocation.Android.manualMode = true;
Ti.Geolocation.addEventListener("heading", compassHandler);
Ti.Geolocation.addEventListener('location', locationCallback);
providerPassive = null;
providerNetwork = null;
providerGps = null;
}
} else {
alert("ERROR:");
}
});
}
$.Window.open();
<Alloy>
<Window id="Window" onOpen="showCurrentPosition">
<Module id="MapView" module="ti.map" method="createView" />
</Window>
</Alloy>
/*
* function to check for all location services outcomes
*/
exports.requestLocationPermissions = function(authorizationType, callback) {
// FIXME: Always returns false on Android 6
// https://jira.appcelerator.org/browse/TIMOB-23135
if (OS_IOS && !Ti.Geolocation.locationServicesEnabled) {
return callback({
success : false,
error : 'Location Services Disabled'
});
}
// Permissions already granted
if (Ti.Geolocation.hasLocationPermissions(authorizationType)) {
return callback({
success : true
});
}
// On iOS we can determine why we do not have permission
if (OS_IOS) {
if (Ti.Geolocation.locationServicesAuthorization === Ti.Geolocation.AUTHORIZATION_RESTRICTED) {
return callback({
success : false,
error : 'Your device policy does not allow Geolocation'
});
} else if (Ti.Geolocation.locationServicesAuthorization === Ti.Geolocation.AUTHORIZATION_DENIED) {
dialogs.confirm({
title : 'You denied permission before',
message : 'Tap Yes to open the Settings app to restore permissions, then try again.',
callback : function() {
Ti.Platform.openURL(Ti.App.iOS.applicationOpenSettingsURL);
}
});
// return success:false without an error since we've informed the user already
return callback({
success : false
});
}
}
// Request permission
Ti.Geolocation.requestLocationPermissions(authorizationType, function(e) {
if (!e.success) {
return callback({
success : false,
error : e.error || 'Failed to request Location Permissions'
});
}
callback({
success : true
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment