Created
April 11, 2012 00:02
-
-
Save justintoth/2355807 to your computer and use it in GitHub Desktop.
Map.js
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
function Map() { | |
var self = this; | |
this.top = 0; | |
this.bottom = 0; | |
this.latitude = 0; | |
this.longitude = 0; | |
this.delta = 0.1; | |
this.display = "map"; | |
this.centerOn = "phone"; | |
this.init = function (annotations, latitude, longitude, top, bottom, delta, visible) { | |
if (top) | |
self.top = top; | |
if (bottom) | |
self.bottom = bottom; | |
if (delta) { | |
self.delta = delta; | |
} | |
if(!visible) | |
visible = false; | |
self.createMap(annotations, latitude, longitude, visible); | |
self.createOptions(visible); | |
return self.mapView; | |
} | |
this.createMap = function (annotations, latitude, longitude, visible) { | |
self.mapView = Ti.Map.createView({ | |
mapType: Ti.Map.STANDARD_TYPE, animate: true, regionFit: false, userLocation: true, | |
region: { latitude: latitude, longitude: longitude, latitudeDelta: self.delta, longitudeDelta: self.delta }, | |
annotations: annotations, bottom: self.bottom, top: self.top, borderWidth: 1, visible: visible | |
}); | |
if(annotations.length > 0) { | |
self.firstAnnotation = annotations[0]; | |
if (!utils.isAndroid) | |
self.mapView.addAnnotation(self.firstAnnotation); | |
self.mapView.selectAnnotation(self.firstAnnotation); | |
} | |
tt.ui.View.add(self.mapView); | |
} | |
this.createOptions = function (visible) { | |
//map/satellite displays. | |
self.mapDisplay = Ti.UI.createImageView({ image: '/images/map/satellite-view.png', width: 50, height: 50, | |
zIndex: 2, top: self.top + 5, right: 5, visible: visible }); | |
self.mapDisplay.addEventListener('click', function () { | |
if (self.display == "map") { | |
self.mapView.setMapType(Titanium.Map.SATELLITE_TYPE); | |
self.mapDisplay.image = "/images/map/map-view.png"; | |
self.display = "satellite"; | |
} | |
else { | |
self.mapView.setMapType(Titanium.Map.STANDARD_TYPE); | |
self.mapDisplay.image = "/images/map/satellite-view.png"; | |
self.display = "map"; | |
} | |
}); | |
tt.ui.View.add(self.mapDisplay); | |
//crosshairs. | |
if(geoutils.enabled) { | |
self.centerDisplay = Ti.UI.createImageView({ image: '/images/map/crosshairs.png', width: 50, height: 50, | |
zIndex: 2, top: self.top + 5, right: 60, visible: visible }); | |
self.centerDisplay.addEventListener('click', function () { | |
if(self.centerOn == "phone") { | |
if(geoutils.latitude && geoutils.latitude != 0 && geoutils.longitude && geoutils.longitude != 0) { | |
//Ti.API.info("setting user location to " + geoutils.latitude + " / " + geoutils.longitude); | |
//center self. | |
self.center(geoutils.latitude, geoutils.longitude); | |
self.centerOn = "back"; | |
self.centerDisplay.image = '/images/map/crosshairs-back.png'; | |
} | |
else { | |
Ti.API.info("Can't get user location, lat and long are 0 or null!"); | |
} | |
} | |
else if(self.latitude && self.latitude != 0 && self.longitude && self.longitude != 0) { | |
Ti.API.info("setting user location to " + self.latitude + " / " + self.longitude); | |
self.center(self.latitude, self.longitude); | |
self.centerOn = "phone"; | |
self.centerDisplay.image = '/images/map/crosshairs.png'; | |
} | |
}); | |
tt.ui.View.add(self.centerDisplay); | |
} | |
} | |
this.createAnnotation = function (title, subtitle, latitude, longitude, isLocation, addToMap) { | |
if(!self.createdAnnotations) { | |
self.createdAnnotations = true; | |
self.latitude = latitude; | |
self.longitude = longitude; | |
} | |
var mapAnnotation = Ti.Map.createAnnotation({ | |
latitude: latitude, | |
longitude: longitude, | |
title: String(title), | |
subtitle: String(subtitle), | |
animate: true | |
}); | |
if (utils.isAndroid) | |
mapAnnotation.pinImage = isLocation ? "/images/map/blue-pin.png" : "/images/map/purple-pin.png"; | |
else | |
mapAnnotation.pincolor = isLocation ? Ti.Map.ANNOTATION_PURPLE : Ti.Map.ANNOTATION_RED; | |
if (addToMap) | |
self.mapView.addAnnotation(mapAnnotation); | |
return mapAnnotation; | |
} | |
this.updateAnnotation = function (mapAnnotation, title, subtitle, latitude, longitude, isLocation) { | |
if (mapAnnotation) { | |
self.removeAnnotation(mapAnnotation); | |
mapAnnotation = self.createAnnotation(title, subtitle, latitude, longitude, isLocation); | |
self.mapView.addAnnotation(mapAnnotation); | |
self.mapView.selectAnnotation(mapAnnotation); | |
} | |
} | |
this.addAnnotation = function (mapAnnotation) { | |
self.mapView.addAnnotation(mapAnnotation); | |
} | |
this.removeAnnotation = function (mapAnnotation) { | |
self.mapView.removeAnnotation(mapAnnotation); | |
} | |
this.clearAnnotations = function() { | |
for(var k=0;k<self.mapView.annotations.length;k++) { | |
self.mapView.removeAnnotation(self.mapView.annotations[k]); | |
} | |
} | |
this.selectAnnotation = function (mapAnnotation) { | |
self.mapView.selectAnnotation(mapAnnotation); | |
} | |
this.center = function (latitude, longitude, delta) { | |
if(delta) | |
self.delta = delta; | |
var location = { | |
latitude: latitude, | |
longitude: longitude, | |
latitudeDelta: self.delta, | |
longitudeDelta: self.delta, | |
animate: true | |
}; | |
//Ti.API.info(location); | |
self.mapView.setLocation(location); | |
} | |
this.createRoute = function (name, points) { | |
var route = { | |
name: name, points: points, color: "#7c74d4", width: 4 | |
}; | |
self.mapView.addRoute(route); | |
setTimeout(function () { self.mapView.regionFit = true; }, 700); | |
} | |
this.show = function () { | |
self.mapView.visible = true; | |
self.mapDisplay.visible = true; | |
self.centerDisplay.visible = true; | |
} | |
this.hide = function () { | |
self.mapView.visible = false; | |
self.mapDisplay.visible = false; | |
self.centerDisplay.visible = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment