Created
April 25, 2019 17:39
-
-
Save Topener/0f0fb8ac71b5927fddf0f39ad3091363 to your computer and use it in GitHub Desktop.
Code demonstrated during the Titanium Livestream about Geolocation and ti.map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const imgf = require('ti.imagefactory'); | |
var map = require('ti.map'); | |
var carImages = []; | |
$.index.open(); | |
function rotateImage(blob, degrees, annotation) { | |
if (_.findIndex(carImages, {degrees: degrees}) == -1) { | |
generateCarFile(blob, degrees); | |
} | |
const newAnnotation = require('ti.map').createAnnotation({ | |
latitude: annotation.latitude, | |
longitude: annotation.longitude, | |
image: carImages[_.findIndex(carImages, {degrees: degrees})].blob | |
}); | |
$.map.addAnnotations([newAnnotation]); | |
setTimeout(() => { | |
$.map.removeAnnotation(annotation); | |
},50); | |
return newAnnotation; | |
} | |
function generateCarFile(blob, degrees) { | |
const newFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'car' + degrees + '.png'); | |
if (!newFile.exists()) { | |
newFile.write(imgf.imageWithRotation(blob, { degrees: degrees})); | |
} | |
carImages.push({degrees: degrees, blob: newFile.read()}); | |
} | |
/* | |
function animateCar(blob, fromDegrees, toDegrees, annotation) { | |
if (fromDegrees > toDegrees) return; | |
annotation = rotateImage(blob, fromDegrees, annotation); | |
setTimeout(() => { | |
animateCar(blob, fromDegrees+10, toDegrees, annotation); | |
}, 200); | |
for (let i = fromDegrees; i < toDegrees; i+=10) { | |
generateCarFile(blob, i); | |
} | |
}*/ | |
const image = Ti.UI.createImageView({image: '/images/car.png'}).toImage(); | |
let annotation = require('ti.map').createAnnotation({ | |
latitude: 52.39, | |
longitude: 4.89, | |
image: '/images/car.png' | |
}); | |
$.map.addAnnotation(annotation); | |
var lastHeading = 0; | |
function locationCallback(e) { | |
if (!e || !e.coords) return; | |
if (e.coords.heading > -1 && _.findIndex(carImages, {degrees: Math.round(e.coords.heading)}) == -1) { | |
generateCarFile(image, Math.round(e.coords.heading)); | |
lastHeading = Math.round(e.coords.heading); | |
} | |
$.map.removeAnnotation(annotation); | |
annotation = require('ti.map').createAnnotation({ | |
latitude: e.coords.latitude, | |
longitude: e.coords.longitude, | |
image: carImages[_.findIndex(carImages, {degrees: lastHeading || 0})].blob | |
}); | |
$.map.addAnnotation(annotation); | |
$.map.setLocation({ | |
latitude: e.coords.latitude, | |
longitude: e.coords.longitude, | |
animate: true | |
}); | |
} | |
function handleRegionchange(e) { | |
console.log(e); | |
$.map.addAnnotation(require('ti.map').createAnnotation({ | |
latitude: e.latitude + (e.latitudeDelta / 2), | |
longitude: e.longitude + (e.longitudeDelta / 2 ) | |
})); | |
} | |
//Ti.Geolocation.addEventListener('location', locationCallback); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment